欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排

程序员文章站 2022-12-09 23:34:35
一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来。 如果该应用继续引入mysql、redis、job等若干服务,到时候发布一次得工作量之大就可想而知了,这时候就需要用到Docker Compose。 Docker Compose是一个用于 ......

一.前言

上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来。

如果该应用继续引入mysql、redis、job等若干服务,到时候发布一次得工作量之大就可想而知了,这时候就需要用到docker compose。

docker compose是一个用于定义和运行多容器docker应用程序的工具。使用compose,可以使用yaml文件来配置应用程序的服务,然后使用一条命令就可以从配置中创建并启动所有服务。

docker compose概述及命令使用 

 

二.安装compose

windows下安装docker desktop时已经附带安装了docker compose

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

 

linux下需要自行安装

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

 

三.使用compose

1.编排服务

在解决方案下创建docker-compose.yml文件

version: '3.4'

services:
  helloworld:
    image: helloworld:v2.0
    build: #镜像构建
      context: . #工作目录
      dockerfile: helloworld/dockerfile #dockerfile位置
    environment: #环境变量
      - aspnetcore_environment=development
    ports: #端口映射
      - "81:80"
    container_name: netcore_helloworld #容器名
    deploy:
      restart_policy: #重启策略
        condition: on-failure
        delay: 5s
        max_attempts: 3
    networks: #指定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: #定义一个docker中已存在的network
    external: 
      name: mybridge
  newbridge: #新的network
    #name: newbridge  #compose版本3.5开始才支持自定义名称

 

2.启动容器

docker-compose up [options] [--scale service=num...] [service...]

docker-compose up指令包含了docker-compose build,当yml文件services中配置的image(helloworld:v2.0和mynginx:v2.0)不存在时会先build这两个镜像,再创建container,

如果image已存在,则直接创建container

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.删除容器

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   #外部的bridge不会被删除,直接跳过
removing network aspnetcore_docker_newbridge

 

四.远程镜像仓库

docker官方的只能创建一个免费私有仓库,国内各大云服务器商都有提供免费的、无限量的镜像仓库。


1.登录到远程registry

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

ps c:\users\administrator> docker login --username=你的用户名 ccr.ccs.tencentyun.com
password:
login succeeded

 

2.上传镜像

docker tag [imageid] ccr.ccs.tencentyun.com/[namespace]/[imagename]:[镜像版本号]

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]:[镜像版本号]

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>

AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排

 

2.通过远程镜像启动容器

在原yml文件基础上移除build项,修改image地址。

docker compose up会从远程仓库pull镜像并启动容器,如果是私有仓库,需要提前登录到远程registry。

version: '3.4'

services:
  helloworld:
    image: ccr.ccs.tencentyun.com/wuuu/helloworld
    environment: #环境变量
      - aspnetcore_environment=development
    ports: #端口映射
      - "81:80"
    container_name: netcore_helloworld #容器名
    deploy:
      restart_policy: #重启策略
        condition: on-failure
        delay: 5s
        max_attempts: 3
    networks: #指定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: #定义一个docker中已存在的network
    external: 
      name: mybridge
  newbridge: #新的network
    #name: newbridge  #compose版本3.5开始才支持自定义名称

 

示例代码github地址https://github.com/wwwu/aspnetcore_docker