Yet another tutorial: dotnet core docker Linux |
dotnet new -all
dotnet new webapi
dotnet restore
dotnet run
http://localhost:5000/api/values
C# LinuxProgram.cs
.UseUrls("http://*:5000") // listen on port 5000 on all network interfaces
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://*:5000") // listen on port 5000 on all network interfaces
.UseStartup()
.Build();
host.Run();
}
http://localhost:5000
. , localhost loopback- . , dotnet core url , , docker- .Startup.cs publicpublic Startup(IHostingEnvironment env)
, ConfigurationBuilder
AddEnvironmentVariables()
.ConfigurationBuilder
.AddInMemoryCollection(new Dictionary
{
{"InstanseId", Guid.NewGuid().ToString()}
})
public Startup(IHostingEnvironment env)
:public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddInMemoryCollection(new Dictionary
{
{"InstanseId", Guid.NewGuid().ToString()}
})
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public class ValuesControllerSettings
{
public string MyTestParam { get; set; }
public string InstanseId { get; set; }
}
Startup.cs
ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Ionfiguration
// ValuesControllerSettings
services.Configure(Configuration);
// Add framework services.
services.AddMvc();
}
ValuesController
private readonly ValuesControllerSettings _settings;
public ValuesController(IOptions settings)
{
_settings = settings.Value;
}
using Microsoft.Extensions.Options;
.dotnet publish
./bin/Debug/[framework]/publish
Dockerfile
#
FROM microsoft/dotnet:runtime
# CMD
WORKDIR /testapp
# (, dockerfile )
COPY /bin/Debug/netcoreapp1.1/publish /testapp
# 5000, Kestrel
EXPOSE 5000
#
CMD ["dotnet",".dll"]
Dockerfile
, docker build -t my-cool-service:1.0 .
docker images
docker run -p 5000:5000 my-cool-service:1.0
http://localhost:5000/api/values
C# Linux dockerdocker images
docker ps
docker run -d
docker inspect ___
docker stop ___
# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)
docker-compose
.docker-compose.yml
, nginx ( , , ) , reverse proxy .
# docker-compose
version: '3.3'
services:
#
service1:
container_name: service1_container
#
image: my-cool-service:1.0
# ,
environment:
- MyTestParam=DBForService1
# nginx
reverse-proxy:
container_name: reverse-proxy
image: nginx
# nginx
ports:
- "777:80"
# nginx
volumes:
- ./test_nginx.conf:/etc/nginx/conf.d/default.conf
docker-compose
, docker-compose , hostname . . nginxupstream myapp1 {
server service1:5000; /* docker-compose */
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
docker-compose up
docker-compose.yml
nginx, reverse proxy . , nginx - . .dotnet core
Linux, docker-, docker-compose
. dotnet core
Linux ( docker, docker ) , , . , .