AspNetCore Containerized (Docker) Deployment - Docker Compose Container Arrangement

Keywords: Docker network Nginx OpenSSL

I. Preface

The last one deployed the most basic helloworld application, created two containers and a network, and it worked.

If the app continues to introduce services such as mysql, redis, job, and so on, the amount of work it takes to publish at one time is conceivable, and Docker Compose is needed.

Docker Compose is a tool for defining and running multi-container Docker applications.Using Compose, you can configure the services of your application using a YAML file, then use a single command to create and start all the services from the configuration.

Overview of Docker Compose and Command Use https://docs.docker.com/compose/reference/overview/

 

2. Install Compose

Docker Compose was installed with Docker Desktop installed under Windows

PS C:\Users\Administrator> docker-compose version
docker-compose version 1.23.2, build 1110ad01
docker-py version: 3.6.0
CPython version: 3.6.6
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018

 

Need to install on your own under Linux

root@VM-16-9-ubuntu:~# apt install docker-compose

 

3. Using Compose

1. Layout Service

Create docker-compose.yml file under solution

version: '3.4'

services:
  helloworld:
    image: helloworld:v2.0
    build: #Mirror building
      context: . #working directory
      dockerfile: HelloWorld/Dockerfile #Dockerfile location
    environment: #environment variable
      - ASPNETCORE_ENVIRONMENT=Development
    ports: #Port Mapping
      - "81:80"
    container_name: netcore_helloworld #Container name
    deploy:
      restart_policy: #Restart Policy
        condition: on-failure
        delay: 5s
        max_attempts: 3
    networks: #Specify the network
      - default
      - newbridge
      
  mynginx:
    image: mynginx:v2.0
    build:
      context: MyNginx
      dockerfile: Dockerfile
    ports:
      - "80:80"
      - "801:801"
    container_name: mynginx
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
    networks:
      - default

networks:
  default: #Define a network that already exists in the docker
    external: 
      name: mybridge
  newbridge: #New network
    #name: newbridge  #Custom names are not supported until compose version 3.5

 

2. Start Container

https://docs.docker.com/compose/reference/up/

docker-compose up [options] [--scale SERVICE=NUM...] [SERVICE...]

The docker-compose up directive contains docker-compose build, which builds the images (helloworld:v2.0 and mynginx:v2.0) configured in the yml file services before creating the container.

If the image already exists, create the container directly

PS C:\Users\Administrator> cd C:\Users\Administrator\source\repos\AspNetCore_Docker
PS C:\Users\Administrator\source\repos\AspNetCore_Docker> docker-compose up -d
WARNING: Some services (helloworld, mynginx) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
Creating network "aspnetcore_docker_newbridge" with the default driver
Creating netcore_helloworld ... done
Creating mynginx            ... done
PS C:\Users\Administrator\source\repos\AspNetCore_Docker> docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
mynginx                                v2.0                9c18561d7ab3        27 minutes ago      109MB
helloworld                             v2.0                c42e9f575fc4        24 hours ago        265MB
nginx                                  latest              62c261073ecf        9 days ago          109MB
mcr.microsoft.com/dotnet/core/sdk      2.2-stretch         e4747ec2aaff        3 weeks ago         1.74GB
mcr.microsoft.com/dotnet/core/aspnet   2.2-stretch-slim    f6d51449c477        3 weeks ago         260MB
docker4w/nsenter-dockerd               latest              2f1c802f322f        8 months ago        187kB
PS C:\Users\Administrator\source\repos\AspNetCore_Docker> docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                      NAMES
66ff08eda2ae        helloworld:v2.0     "dotnet HelloWorld.d..."   11 minutes ago      Up 11 minutes       0.0.0.0:81->80/tcp                         netcore_helloworld
5357b641a7b1        mynginx:v2.0        "nginx -g 'daemon of..."   11 minutes ago      Up 11 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:801->801/tcp   mynginx

 

3. Delete Containers

PS C:\Users\Administrator\source\repos\AspNetCore_Docker> docker-compose down
WARNING: Some services (helloworld, mynginx) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
Stopping mynginx            ... done
Stopping netcore_helloworld ... done
Removing mynginx            ... done
Removing netcore_helloworld ... done
Network mybridge is external, skipping   #External bridge s will not be deleted, skip directly
Removing network aspnetcore_docker_newbridge

 

4. Remote Mirror Warehouse

The official docker can only create a free private warehouse, and all major cloud server providers in China have free and unlimited mirror warehouses.


1. Log on to remote registry

docker login --username=[username] ccr.ccs.tencentyun.com

PS C:\Users\Administrator> docker login --username=username ccr.ccs.tencentyun.com
Password:
Login Succeeded

 

2. Upload Mirror

docker tag [ImageId] ccr.ccs.tencentyun.com/[namespace]/[ImageName]: [Mirror version number]

PS C:\Users\Administrator> docker tag c42e9f575fc4 ccr.ccs.tencentyun.com/wuuu/helloworld
PS C:\Users\Administrator> docker tag 9c18561d7ab3 ccr.ccs.tencentyun.com/wuuu/mynginx
PS C:\Users\Administrator> docker images
REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
ccr.ccs.tencentyun.com/wuuu/mynginx      latest              9c18561d7ab3        2 days ago          109MB
mynginx                                  v2.0                9c18561d7ab3        2 days ago          109MB
ccr.ccs.tencentyun.com/wuuu/helloworld   latest              c42e9f575fc4        3 days ago          265MB
helloworld                               v2.0                c42e9f575fc4        3 days ago          265MB
nginx                                    latest              62c261073ecf        12 days ago         109MB
mcr.microsoft.com/dotnet/core/sdk        2.2-stretch         e4747ec2aaff        3 weeks ago         1.74GB
mcr.microsoft.com/dotnet/core/aspnet     2.2-stretch-slim    f6d51449c477        3 weeks ago         260MB
docker4w/nsenter-dockerd                 latest              2f1c802f322f        8 months ago        187kB
PS C:\Users\Administrator>

 
docker push ccr.ccs.tencentyun.com/[namespace]/[ImageName]: [Mirror version number]

PS C:\Users\Administrator> docker push ccr.ccs.tencentyun.com/wuuu/helloworld
The push refers to repository [ccr.ccs.tencentyun.com/wuuu/helloworld]
... latest: digest: sha256:d991fe759257905f727593cc09d8299462e20e31ada3a92023a48fbc130f7484 size:
1581 PS C:\Users\Administrator> docker push ccr.ccs.tencentyun.com/wuuu/mynginx The push refers to repository [ccr.ccs.tencentyun.com/wuuu/mynginx]
...

latest: digest: sha256:0eda000278411f5b6e034944993f6f5b94825125124f67cc7caf4e684aad5a85 size: 1155 PS C:\Users\Administrator>

 

2. Start the container by remote mirroring

Remove the build item from the original yml file and modify the image address.

The docker compose up pull s the image from the remote repository and starts the container. If it is a private repository, you need to log in to the remote registry ahead of time.

version: '3.4'

services:
  helloworld:
    image: ccr.ccs.tencentyun.com/wuuu/helloworld
    environment: #environment variable
      - ASPNETCORE_ENVIRONMENT=Development
    ports: #Port Mapping
      - "81:80"
    container_name: netcore_helloworld #Container name
    deploy:
      restart_policy: #Restart Policy
        condition: on-failure
        delay: 5s
        max_attempts: 3
    networks: #Specify the network
      - default
      - newbridge
      
  mynginx:
    image: ccr.ccs.tencentyun.com/wuuu/mynginx
    ports:
      - "80:80"
      - "801:801"
    container_name: mynginx
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
    networks:
      - default

networks:
  default: #Define a network that already exists in the docker
    external: 
      name: mybridge
  newbridge: #New network
    #name: newbridge  #Custom names are not supported until compose version 3.5

 

Sample code Github address: https://github.com/wwwu/AspNetCore_Docker

 

Posted by ondi on Fri, 10 Apr 2020 17:19:11 -0700