-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


Yet another tutorial: dotnet core docker Linux

, 13 2017 . 23:50 +


, 2017, , , , . dotnet core+C#, .. , C# Linux.
: docker/dotnet core , . 3 Docker Get Started Guide - english. , - . , .


Linux ( Ubuntu 16.04 VirtualBox Windows 10), dotnet core, docker, docker compose, .
. , .
, .. , - , , .
, Linux, Visual Studio Code JetBrains Rider.

Visual Studio Code
. , , , .

Rider
Idea, , IDE JetBrains. debug linux, EAP . Rider . JetBrains .



Create Project IDE c .
1.
2. ,
dotnet new -all

3. WebApi
dotnet new webapi

4.
dotnet restore

5.
dotnet run

6. http://localhost:5000/api/values C# Linux


Program.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();
}

, , .
Kestrel, , http://localhost:5000. , localhost loopback- . , dotnet core url , , docker- .

.
, , .
, .
Startup.cs publicpublic Startup(IHostingEnvironment env) , ConfigurationBuilder AddEnvironmentVariables().
DI.


Guid IoC-, . , , .
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();
}

DI
- . , , Id , , - ( MyTestParam) .
,
  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;.
Get , , , -> .


docker-
1. . ,
dotnet publish

.
. dll- ./bin/Debug/[framework]/publish

2. docker-.
Dockerfile

#     
FROM microsoft/dotnet:runtime

#        CMD
WORKDIR /testapp

#      (, dockerfile     )   
COPY /bin/Debug/netcoreapp1.1/publish /testapp

#     5000,   Kestrel
EXPOSE 5000

#      
CMD ["dotnet",".dll"]

3. , Dockerfile ,
docker build -t my-cool-service:1.0 . 

my-cool-service , 1.0

4. ,
docker images

5. , ,
docker run -p 5000:5000 my-cool-service:1.0

6. http://localhost:5000/api/values C# Linux docker

docker

docker images


docker ps

detached mode
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 . , : 3, 1 2. 1 2 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 . . nginx
upstream 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.
, - docker / dotnet core.

: - dotnet core Linux ( docker, docker ) , , . , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332582/

:  

: [1] []
 

:
: 

: ( )

:

  URL