概述

使用 Docker 的时候,定义 Dockerfile 文件,然后使用 docker builddocker run 等命令操作容器。

然而微服务架构的应用系统一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,这样效率很低,也不方便管理。

使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具。

yaml 官方示例

https://docs.docker.com/compose/compose-file/compose-file-v3/#compose-file-structure-and-examples

version: "3.9"
services: redis:
image: redis:alpine
ports:
- "6379"
networks:
- frontend
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
max_replicas_per_node: 1
constraints:
- "node.role==manager" vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- "5000:80"
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure result:
image: dockersamples/examplevotingapp_result:before
ports:
- "5001:80"
networks:
- backend
depends_on:
- db
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
placement:
constraints:
- "node.role==manager" visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints:
- "node.role==manager" networks:
frontend:
backend: volumes:
db-data:

depends_on:依赖关系,如 web 依赖 redis 和 db,通过 depends_on 表明关系。

version: "3.9"
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres

安装

Docker Compose 是 Docker 的一个开源项目,目前托管到了 GitHub,需要前往 GitHub 下载。

sudo curl -L "https://github.com/docker/compose/releases/download/2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

由于存放在 GitHub,国内网络限制导致不太稳定,不推荐使用

推荐使用 道客 提供的 Docker 极速下载 进行安装:

curl -L https://get.daocloud.io/docker/compose/releases/download/v2.2.3/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

安装:

[root@sail ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/v2.2.3/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 423 100 423 0 0 394 0 0:00:01 0:00:01 --:--:-- 394
100 23.5M 100 23.5M 0 0 8670k 0 0:00:02 0:00:02 --:--:-- 20.3M
[root@sail ~]# cd /usr/local/bin/
[root@sail bin]# ls
docker-compose

这样即表示安装成功。

授权:

[root@sail ~]# chmod +x /usr/local/bin/docker-compose
[root@sail ~]#

查看版本

docker-compose version

[root@sail bin]# docker-compose version
Docker Compose version v2.2.3

显示了版本即代表 Docker Compose 安装完成。

卸载

rm /usr/local/bin/docker-compose

由于 Linux 一切皆文件,删除此文件夹即可完成 Docker Compose 的卸载。

使用

构建

1、创建项目目录

[root@sail sail]# mkdir docker-compose
[root@sail sail]# cd docker-compose
[root@sail docker-compose]#

2、创建 app.py

[root@sail docker-compose]# vim app.py
import time

import redis
from flask import Flask app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379) def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5) @app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)

redis 是应用容器中 redis 容器的主机名,在同一网络下可以通过服务名访问,端口默认 6379。

3、创建 requirements.txt

[root@sail docker-compose]# vim requirements.txt
[root@sail docker-compose]# cat requirements.txt
flask
redis

4、创建 Dockerfile

[root@sail docker-compose]# vim Dockerfile
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

5、创建 docker-compose.yml

[root@sail docker-compose]# vim docker-compose.yml
version: "3.3"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"

这个文件定义了两个服务:web 和 redis。

启动

docker-compose up

在项目目录中,运行 docker-compose up 来启动应用程序。

第一次启动需要安装很多环境,比较缓慢:

[root@sail docker-compose]# docker-compose up
[+] Running 7/7
⠿ redis Pulled 14.1s
⠿ 59bf1c3509f3 Pull complete 3.0s
⠿ 719adce26c52 Pull complete 3.1s
⠿ b8f35e378c31 Pull complete 3.3s
⠿ d034517f789c Pull complete 9.0s
⠿ 3772d4d76753 Pull complete 9.0s
⠿ 211a7f52febb Pull complete 9.1s
Sending build context to Docker daemon 725B
Step 1/10 : FROM python:3.7-alpine
3.7-alpine: Pulling from library/python
59bf1c3509f3: Already exists
8786870f2876: Pull complete
45d4696938d0: Pull complete
ef84af58b2c5: Pull complete
c3c9b71b9a69: Pull complete
Digest: sha256:d64e0124674d64e78cc9d7378a1130499ced66a7a00db0521d0120a2e88ac9e4
Status: Downloaded newer image for python:3.7-alpine
---> a1034fd13493
Step 2/10 : WORKDIR /code
---> Running in e23e4b173abf
Removing intermediate container e23e4b173abf
---> 41eb64157cfc
Step 3/10 : ENV FLASK_APP=app.py
---> Running in cdefb769398d
Removing intermediate container cdefb769398d
---> ab741ac5cb17
Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0
---> Running in 4976c1da428c
Removing intermediate container 4976c1da428c
---> 5a5c24d67db6
Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers
---> Running in 53043bd38e33
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
(1/13) Installing libgcc (10.3.1_git20211027-r0)
(2/13) Installing libstdc++ (10.3.1_git20211027-r0)
(3/13) Installing binutils (2.37-r3)
(4/13) Installing libgomp (10.3.1_git20211027-r0)
(5/13) Installing libatomic (10.3.1_git20211027-r0)
(6/13) Installing libgphobos (10.3.1_git20211027-r0)
(7/13) Installing gmp (6.2.1-r0)
(8/13) Installing isl22 (0.22-r0)
(9/13) Installing mpfr4 (4.1.0-r0)
(10/13) Installing mpc1 (1.2.1-r0)
(11/13) Installing gcc (10.3.1_git20211027-r0)
(12/13) Installing linux-headers (5.10.41-r0)
(13/13) Installing musl-dev (1.2.2-r7)
Executing busybox-1.34.1-r3.trigger
OK: 139 MiB in 48 packages
Removing intermediate container 53043bd38e33
---> 73e9550df596
Step 6/10 : COPY requirements.txt requirements.txt
---> c5a73d6f1fe1
Step 7/10 : RUN pip install -r requirements.txt
---> Running in 826790d0bfbb
Collecting flask
Downloading Flask-2.0.2-py3-none-any.whl (95 kB)
Collecting redis
Downloading redis-4.1.0-py3-none-any.whl (171 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=7.1.2
Downloading click-8.0.3-py3-none-any.whl (97 kB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
Collecting packaging>=21.3
Downloading packaging-21.3-py3-none-any.whl (40 kB)
Collecting deprecated>=1.2.3
Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting importlib-metadata>=1.0
Downloading importlib_metadata-4.10.0-py3-none-any.whl (17 kB)
Collecting wrapt<2,>=1.10
Downloading wrapt-1.13.3-cp37-cp37m-musllinux_1_1_x86_64.whl (78 kB)
Collecting zipp>=0.5
Downloading zipp-3.7.0-py3-none-any.whl (5.3 kB)
Collecting typing-extensions>=3.6.4
Downloading typing_extensions-4.0.1-py3-none-any.whl (22 kB)
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl (30 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
Downloading pyparsing-3.0.6-py3-none-any.whl (97 kB)
Installing collected packages: zipp, typing-extensions, wrapt, pyparsing, MarkupSafe, importlib-metadata, Werkzeug, packaging, Jinja2, itsdangerous, deprecated, click, redis, flask
Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 deprecated-1.2.13 flask-2.0.2 importlib-metadata-4.10.0 itsdangerous-2.0.1 packaging-21.3 pyparsing-3.0.6 redis-4.1.0 typing-extensions-4.0.1 wrapt-1.13.3 zipp-3.7.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 826790d0bfbb
---> c1483947fad2
Step 8/10 : EXPOSE 5000
---> Running in cd59e0408b47
Removing intermediate container cd59e0408b47
---> 05c632dea80d
Step 9/10 : COPY . .
---> 8c89b910e366
Step 10/10 : CMD ["flask", "run"]
---> Running in 1d9d071fee96
Removing intermediate container 1d9d071fee96
---> de4639486b50
Successfully built de4639486b50
Successfully tagged docker-compose_web:latest Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/3
⠿ Network docker-compose_default Created 0.3s
⠿ Container docker-compose-redis-1 Created 0.0s
⠿ Container docker-compose-web-1 Created 0.0s
Attaching to docker-compose-redis-1, docker-compose-web-1
docker-compose-redis-1 | 1:C 07 Jan 2022 08:36:26.687 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
docker-compose-redis-1 | 1:C 07 Jan 2022 08:36:26.687 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
docker-compose-redis-1 | 1:C 07 Jan 2022 08:36:26.687 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
docker-compose-redis-1 | 1:M 07 Jan 2022 08:36:26.687 * monotonic clock: POSIX clock_gettime
docker-compose-redis-1 | 1:M 07 Jan 2022 08:36:26.688 * Running mode=standalone, port=6379.
docker-compose-redis-1 | 1:M 07 Jan 2022 08:36:26.688 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
docker-compose-redis-1 | 1:M 07 Jan 2022 08:36:26.688 # Server initialized
docker-compose-redis-1 | 1:M 07 Jan 2022 08:36:26.688 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
docker-compose-redis-1 | 1:M 07 Jan 2022 08:36:26.688 * Ready to accept connections
docker-compose-web-1 | * Serving Flask app 'app.py' (lazy loading)
docker-compose-web-1 | * Environment: production
docker-compose-web-1 | WARNING: This is a development server. Do not use it in a production deployment.
docker-compose-web-1 | Use a production WSGI server instead.
docker-compose-web-1 | * Debug mode: off
docker-compose-web-1 | * Running on all addresses.
docker-compose-web-1 | WARNING: This is a development server. Do not use it in a production deployment.
docker-compose-web-1 | * Running on http://172.18.0.2:5000/ (Press CTRL+C to quit)

查看镜像:

[root@sail docker-compose]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-compose_web latest 6df8b78250a1 44 seconds ago 185MB
redis alpine 3900abf41552 5 weeks ago 32.4MB
python 3.7-alpine a1034fd13493 5 weeks ago 41.8MB

启动 Docker Compose 时,会自动拉取需要的镜像。

查看容器:

[root@sail docker-compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78a6f8b03a49 docker-compose_web "flask run" 8 minutes ago Up 8 minutes 0.0.0.0:5000->5000/tcp docker-compose-web-1
b4da6da4364f redis:alpine "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 6379/tcp docker-compose-redis-1

可以看到容器命名都带有数字,是因为需要集群管理,数字代表副本序号。

查看网络:

[root@sail docker-compose]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b89f719e94e0 bridge bridge local
619a5845a105 docker-compose_default bridge local
28d77e958643 host host local
801fbbe1b38c mynet bridge local
c3ff850e96f0 none null local

项目中的内容都在同个网络下。

访问测试:

[root@sail docker-compose]# curl localhost:5000
Hello World! I have been seen 1 times.
[root@sail docker-compose]# curl localhost:5000
Hello World! I have been seen 2 times.
[root@sail docker-compose]# curl localhost:5000
Hello World! I have been seen 3 times.

Docker Compose 启动完成。

停止

docker-compose stop

示例:

[root@sail docker-compose]# docker-compose stop
[+] Running 2/2
⠿ Container docker-compose-redis-1 Stopped 0.2s
⠿ Container docker-compose-web-1 Stopped

停止并删除容器和网络

docker-compose down

示例:

[root@sail docker-compose]# docker-compose down
[+] Running 3/3
⠿ Container docker-compose-web-1 Removed 10.2s
⠿ Container docker-compose-redis-1 Removed 0.2s
⠿ Network docker-compose_default Removed
[root@sail docker-compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@sail docker-compose]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b89f719e94e0 bridge bridge local
28d77e958643 host host local
801fbbe1b38c mynet bridge local
c3ff850e96f0 none null local

可以看出,容器网络都被删除了。

更多配置

https://docs.docker.com/compose/compose-file/compose-file-v3/#compose-file-structure-and-examples


参考

https://www.bilibili.com/video/BV1og4y1q7M4?spm_id_from=333.999.0.0

https://www.bilibili.com/video/BV1kv411q7Qc?spm_id_from=333.999.0.0

版本

CentOS 7.6

Docker 20.10.11

Docker 14 Docker Compose的更多相关文章

  1. docker和docker compose常用操作命令

    首先区分一下docker中几个概念 Image:镜像,相当于一个root文件系统,不包含任何动态数据 Container:容器,镜像运行时的实体,实质是进程,容器进程运行于属于自己的独立的命名空间 d ...

  2. Dockerfile & Docker Swarm & Docker Stack & Docker Compose

    Dockerfile 通俗地讲,它是为了指导单个镜像从无到有的构建过程.如果你镜像是从Docker registry上面拉下来的,那就用不到这个文件:如果你是自己的应用,想打包成镜像,那就需要这个文件 ...

  3. .NET遇上Docker - 使用Docker Compose组织Ngnix和.NETCore运行

    本文工具准备: Docker for Windows Visual Studio 2015 与 Visual Studio Tools for Docker 或 Visual Studio 2017 ...

  4. Docker,Docker Compose,Docker Swarm,Kubernetes之间的区别

    Dcoker Docker 这个东西所扮演的角色,容易理解,它是一个容器引擎,也就是说实际上我们的容器最终是由Docker创建,运行在Docker中,其他相关的容器技术都是以Docker为基础,它是我 ...

  5. 物联网架构成长之路(24)-Docker练习之Compose容器编排

    0.前言 一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂.因此学习东西还是要循序渐进,慢慢来.先了解单机编排技术Docker Compo ...

  6. Docker 0x13: Docker 构建集群/服务/Compose/分布式服务栈

    目录 Docker 构建集群/服务/Compose/分布式服务栈 集群 初始化集群服务 安装docker-machine 管理节点和工作节点 docker集群构建完成 集群中部署应用 集群服务访问特性 ...

  7. docker swarm和compose 的使用(阿里)

    基本的docker使用参考:Docker 入门 到部署Web 程序- (阿里面试常用的docker命令和优点) 昨天去阿里面试 问我如果给你5台服务器 如何部署docker,我说一个个拷贝,面试官听了 ...

  8. linux安装docker和docker compose

    运行 sudo -s 切换到root用户. 1.卸载旧版本Docker(如果系统之前没安装过Docker,可以跳过): yum remove docker \ docker-client \ dock ...

  9. docker和docker compose安装使用、入门进阶案例

    一.前言 现在可谓是容器化的时代,云原生的袭来,导致go的崛起,作为一名java开发,现在慌得一批.作为知识储备,小编也是一直学关于docker的东西,还有一些持续继承jenkins. 提到docke ...

  10. 小而精的 Docker 项目,为什么要使用 Docker? Docker 容器

    前言 为什么要使用 Docker? Docker 容器的启动在秒级 Docker 对系统资源利用率高,一台主机上可以同时运行数千个 Docker 容器. Docker 基本不消耗系统资源,使得运行在 ...

随机推荐

  1. PRINCE2系列一基于项目情境自定义解决方案

    PRINCE2(PRojects IN Controlled Environments,受控环境下的项目管理) 对项目进行了如下定义:项目是按照一个被批准的商业论证,为了交付一个或多个商业产品而创建的 ...

  2. DataGear 制作基于Vue2、Element UI前端框架的数据可视化看板

    DataGear 数据可视化看板内置了一些基本.简单的页面交互组件,当它们无法满足实际看板需求时,可以引入更流行和强大的前端框架. 本文以Vue2.Element UI前端框架为例,介绍如何制作具有更 ...

  3. MindSponge分子动力学模拟——使用MDAnalysis工具进行后分析(2024.02)

    技术背景 分子动力学模拟(Molecule Dynamics Simulation,MD),本质上是一门采样技术.通过配置力场参数.拓扑结构和积分器,对一个给定的体系不断的采样,最终得到一系列的轨迹. ...

  4. 【Azure 环境】中国区Azure是否可以根据资源组的模板,生成一个可视化的架构图呢?

    问题描述 这是一个国际版链接(https://docs.microsoft.com/en-us/answers/questions/370410/how-to-generate-architectur ...

  5. 【Azure Redis Cache】对StackExchange.Redis IOCP错误消息的解读

    问题描述 在使用StackExchange.Redis连接到Azure Redis服务时,时常出现StackExchange.Redis.RedisTimeoutException异常. 全部错误消息 ...

  6. [Linux] ubuntu系统使用zfs记录

    关于zfs 之前从B站视频里面了解到了btrfs这个文件系统,说是能够实现cow,然后我第一时间觉得这是个极好的特性,结果后来才发现,使用的时候并不多,而且只能通过cp --reflink的方式使用, ...

  7. SpringCloud Sentinel使用

    1. 简介 Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流.流量整形.熔断降级.系统负载保护.热点防护等多个维度来帮助开发者保障微服务的稳定性.替换原先Hystrix ...

  8. centos 7与8修改主机名的各种方法

    hostname 查看当前系统主机名,知道当前主机名为localhost 当然在centos7特地添加了hostnamectl命令查看,修改主机名 使用hostnamectl set-hostname ...

  9. 在Linux下开启指定端口号

    1.查看某个端口是否已开启,如果提示no表示未开启 #8888表示要查询的端口号firewall-cmd --query-port=8888/tcp 2.永久开启端口号,提示 success 表示成功 ...

  10. 本地部署FastGPT使用在线大语言模型

    FastGPT 是一个基于 LLM 大语言模型的知识库问答系统,提供开箱即用的数据处理.模型调用等能力,它背后依赖OneApi开源项目来访问各种大语言模型提供的能力.各大语言模型提供的访问接口规范不尽 ...