一.前言

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

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

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

Docker Compose概述及命令使用 https://docs.docker.com/compose/reference/overview/

二.安装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.启动容器

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

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>

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

AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排的更多相关文章

  1. 研发环境容器化实施过程(docker + docker-compose + jenkins)

    目录 背景介绍 改造思路 容器构建 基础准备 中间件容器 外部依赖容器 业务应用容器 容器整合 自动构建容器 Maven相关 非Maven项目 总结 背景介绍 目前公司内部系统(代号GMS)研发团队, ...

  2. zabbix容器化安装及监控docker应用

    一.zabbix agent2 介绍 从Zabbix 4.4之后,官方推出了Zabbix Agent 2,意味着zabbix 不在只是物理机监控的代名词,现在你可以使用Go为Zabbix编写插件,来监 ...

  3. docker部署多个mysql容器,并使用java连接

    测试springboot多个数据源配置时,需要安装多个mysql容器,由于资源限制,当前只有一台虚拟机,如果在一台机器上安装多个mysql实例,是可以的,但步骤比较繁琐,使用docker来安装MySQ ...

  4. Docker部署 Mysql .Net6等容器

    Centos8安装Docker 1.更新一下yum [root@VM-24-9-centos ~]# yum -y update 2.安装containerd.io # centos8默认使用podm ...

  5. docker 实践三:操作容器

    在学习了 docker 镜像的内容后,我们在来看 docker 的另一个核心点:容器. 注:环境为 CentOS7,docker 19.03 docker 的容器是镜像的一个运行实例.docker 镜 ...

  6. 容器化-Docker-1-速查手册-Docker常用命令

    目录 备注 常用命令 Docker镜像管理(操作对象是镜像) Docker容器管理(操作对象是容器) 容器外挂目录(宿主目录映射到容器中) 这篇文章的目的就是把最常用的命令列出来,没时间看速查命令使用 ...

  7. Spring Boot 项目转容器化 K8S 部署实用经验分享

    转载自:https://cloud.tencent.com/developer/article/1477003 我们知道 Kubernetes 是 Google 开源的容器集群管理系统,它构建在目前流 ...

  8. 基于【 Docker】三 || Docker的基本操作

    一.Docker常用命令 1.搜索镜像:docker search 镜像名称 2.下载镜像:docker pull 镜像名称:版本号 3.查看镜像:docker images 4.删除镜像:docke ...

  9. docker学习(三) - docker理解及命令

    Docker 包括三个基本概念 镜像(Image) 容器(Container) 仓库(Repository) 镜像 Docker 镜像就是一个只读的模板. 例如:一个镜像可以包含一个完整的 ubunt ...

随机推荐

  1. HDU1598【最小生成树拓展】

    参考自 http://www.cnblogs.com/nanke/archive/2012/02/13/2350008.html PS: 没想到最小生成树的kruskal算法从小到大枚举边,然后MAX ...

  2. MarketServer 日志

    2014.04.29 1. 发现有时候会跳出 Exception Infomations:   用户异常信息:Socket未连接 跟踪后发现的一次情况是: 服务器根据客户端请求从后台读取数据后,写数据 ...

  3. 【NOI省选模拟】小奇的花园

    「题目背景」 小奇在家中的花园漫步时,总是会思考一些奇怪的问题. 「问题描述」 小奇的花园有n个温室,标号为1到n,温室以及以及温室间的双向道路形成一棵树. 每个温室都种植着一种花,随着季节的变换,温 ...

  4. 01 | VIM基础攻略

    启动 vim 后,vim 处于 normal 模式. Step One: "i" -> insert 模式, ESC -> normal 模式: "x&quo ...

  5. mysql 主从 binlog

    binlog: 用来记录mysql的数据更新或者潜在更新(update xxx where id=x effect row 0);文件内容存储:/var/lib/mysql mysqlbinlog - ...

  6. [筆記]catalan卡特蘭數

    前言:希望自己每個星期能發一篇文章,提升一下寫文章的能力?雖然對語文作文毫無幫助但是總比玩遊戲強 所以不務正業的東西就不放在首頁了,有興趣的可以點分類去看 來源:https://www.cnblogs ...

  7. G-华华对月月的忠诚

    链接:https://ac.nowcoder.com/acm/contest/392/G 题意: 月月要参加学校的信息学集训,晚上不能陪华华聊天了.不过为了防止华华去和别的小姐姐聊天,浪费时间影响学习 ...

  8. centos安装openldap过程

    1.下载软件如下,db是数据库 2.首先安装数据库db # tar xf db-4.8.30.tar.gz # cd db-4.8.30 # cd build_unix/ (# ../dist/con ...

  9. Centos7中查看IP命令:IP addr

    Centos的IP地址是网卡的inet 的值,很明显第一个是本地服务地址,不是我们想要的.第二个没有inet这个属性值. 接下来配置网卡,我的网卡是上图黄色方框中第一行开始的那一部分:eno16777 ...

  10. Linux scp 命令使用方法

    scp 命令: 1.将本地文件拷贝到远程:scp  文件名 用户名@计算机IP或者计算机名称:远程路径 2.从远程将文件拷回本地:scp  用户名@计算机IP或者计算机名称:文件名 本地路径 3.将本 ...