|
|
|
|
<h1><center>Docker-compose</center></h1>
|
|
|
|
|
|
|
|
|
|
**作者:行癫(盗版必究)**
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
## 一:Docker-compose概述
|
|
|
|
|
|
|
|
|
|
Compose 是一个用于定义和运行多容器 Docker 应用程序的工具。使用 Compose,您可以使用 YAML 文件来配置应用程序的服务。然后,使用一个命令,您可以从您的配置中创建并启动所有服务
|
|
|
|
|
|
|
|
|
|
Compose 适用于所有环境:生产、登台、开发、测试以及 CI 工作流程
|
|
|
|
|
|
|
|
|
|
使用 Compose 基本上是一个三步过程:
|
|
|
|
|
|
|
|
|
|
使用定义您的应用程序的环境,`Dockerfile`以便可以在任何地方复制它
|
|
|
|
|
|
|
|
|
|
定义构成您的应用程序的服务,`docker-compose.yml` 以便它们可以在隔离环境中一起运行
|
|
|
|
|
|
|
|
|
|
运行`docker compose up`,Docker compose 命令启动并运行您的整个应用程序
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
version: "3.9" # optional since v1.27.0
|
|
|
|
|
services:
|
|
|
|
|
web:
|
|
|
|
|
build: .
|
|
|
|
|
ports:
|
|
|
|
|
- "8000:5000"
|
|
|
|
|
volumes:
|
|
|
|
|
- .:/code
|
|
|
|
|
- logvolume01:/var/log
|
|
|
|
|
links:
|
|
|
|
|
- redis
|
|
|
|
|
redis:
|
|
|
|
|
image: redis
|
|
|
|
|
volumes:
|
|
|
|
|
logvolume01: {}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 二:Docker-compose安装
|
|
|
|
|
|
|
|
|
|
#### 1.下载
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian ~]# curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 2.创建软连接
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 3.添加执行权限
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian ~]# chmod a+x /usr/bin/docker-compose
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 4.验证版本
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian ~]# docker-compose --version
|
|
|
|
|
Docker Compose version v2.6.0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 三:Docker-compose入门
|
|
|
|
|
|
|
|
|
|
#### 1.设置
|
|
|
|
|
|
|
|
|
|
定义应用程序依赖项:
|
|
|
|
|
|
|
|
|
|
1.为项目创建一个目录:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian ~]# mkdir composetest
|
|
|
|
|
[root@xingdian ~]# cd composetest
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2.在项目目录中创建一个名为的文件`app.py`并将其粘贴到:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian composetest]# cat 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 容器的主机名。我们使用 Redis 的默认端口,`6379`
|
|
|
|
|
|
|
|
|
|
3.在您的项目目录中创建另一个名为的文件requirements.txt并将其粘贴到:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
flask
|
|
|
|
|
redis
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 2.创建 Dockerfile
|
|
|
|
|
|
|
|
|
|
编写一个构建 Docker 映像的 Dockerfile。该映像包含 Python 应用程序所需的所有依赖项,包括 Python 本身
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
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"]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
从 Python 3.7 映像开始构建映像
|
|
|
|
|
|
|
|
|
|
将工作目录设置为`/code`
|
|
|
|
|
|
|
|
|
|
设置命令使用的环境变量`flask`
|
|
|
|
|
|
|
|
|
|
安装 gcc 和其他依赖项
|
|
|
|
|
|
|
|
|
|
复制`requirements.txt`并安装 Python 依赖项
|
|
|
|
|
|
|
|
|
|
向镜像添加元数据以描述容器正在侦听端口 5000
|
|
|
|
|
|
|
|
|
|
将项目中的当前目录复制`.`到镜像中的workdir `.`
|
|
|
|
|
|
|
|
|
|
将容器的默认命令设置为`flask run`
|
|
|
|
|
|
|
|
|
|
#### 3.Compose 文件中定义服务
|
|
|
|
|
|
|
|
|
|
在项目目录中创建一个名为的文件`docker-compose.yml`并粘贴以下内容:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
version: "3.9"
|
|
|
|
|
services:
|
|
|
|
|
web:
|
|
|
|
|
build: .
|
|
|
|
|
ports:
|
|
|
|
|
- "8000:5000"
|
|
|
|
|
redis:
|
|
|
|
|
image: "redis:alpine"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这个 Compose 文件定义了两个服务:`web`和`redis`
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
该服务使用从当前目录中`web`构建的图像。`Dockerfile`然后它将容器和主机绑定到暴露的端口,`8000`. 此示例服务使用 Flask Web 服务器的默认端口,`5000`
|
|
|
|
|
|
|
|
|
|
该`redis`服务使用 从 Docker Hub 注册表中提取的公共Redis映像
|
|
|
|
|
|
|
|
|
|
#### 4.使用 Compose 构建并运行您的应用程序
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian composetest]# docker-compose up
|
|
|
|
|
[+] Building 95.0s (6/10)
|
|
|
|
|
=> [internal] load build definition from Dockerfile 0.0s
|
|
|
|
|
=> => transferring dockerfile: 291B 0.0s
|
|
|
|
|
[+] Building 481.8s (6/10)
|
|
|
|
|
=> [internal] load build definition from Dockerfile 0.0s
|
|
|
|
|
=> => transferring dockerfile: 291B 0.0s
|
|
|
|
|
=> [internal] load .dockerignore 0.0s
|
|
|
|
|
=> => transferring context: 2B 0.0s
|
|
|
|
|
=> [internal] load metadata for docker.io/library/python:3.7-alpine 4.3s
|
|
|
|
|
=> [1/6] FROM docker.io/library/python:3.7-alpine@sha256:a03bd8ebf621e25cc51ee80cb82d04ca 0.0s
|
|
|
|
|
=> [internal] load build context 0.0s
|
|
|
|
|
=> => transferring context: 1.06kB 0.0s
|
|
|
|
|
=> CACHED [2/6] WORKDIR /code 0.0s
|
|
|
|
|
=> [3/6] RUN apk add --no-cache gcc musl-dev linux-headers 477.5s
|
|
|
|
|
=> => # ERROR: libgomp-11.2.1_git20220219-r2: temporary error (try again later)
|
|
|
|
|
=> => # (5/12) Installing libatomic (11.2.1_git20220219-r2)
|
|
|
|
|
.............
|
|
|
|
|
composetest-web-1 | Use a production WSGI server instead.
|
|
|
|
|
composetest-web-1 | * Debug mode: off
|
|
|
|
|
composetest-web-1 | * Running on all addresses (0.0.0.0)
|
|
|
|
|
composetest-web-1 | WARNING: This is a development server. Do not use it in a production deployment.
|
|
|
|
|
composetest-web-1 | * Running on http://127.0.0.1:5000
|
|
|
|
|
composetest-web-1 | * Running on http://172.19.0.2:5000 (Press CTRL+C to quit)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 5.测试访问
|
|
|
|
|
|
|
|
|
|
在浏览器中输入 http://localhost:8000/ 以查看正在运行的应用程序
|
|
|
|
|
|
|
|
|
|
![image-20220621164601764](Docker-compose.assets/image-20220621164601764.png)
|
|
|
|
|
|
|
|
|
|
#### 6.查看docker镜像
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian composetest]# docker images
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![image-20220621171235416](Docker-compose.assets/image-20220621171235416.png)
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
停止应用程序,方法是`docker-compose down` 在第二个终端的项目目录中运行,或者在启动应用程序的原始终端中按 CTRL+C
|
|
|
|
|
|
|
|
|
|
#### 7.编辑 Compose 文件以添加绑定挂载
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
version: "3.9"
|
|
|
|
|
services:
|
|
|
|
|
web:
|
|
|
|
|
build: .
|
|
|
|
|
ports:
|
|
|
|
|
- "8000:5000"
|
|
|
|
|
volumes:
|
|
|
|
|
- .:/code
|
|
|
|
|
environment:
|
|
|
|
|
FLASK_ENV: development
|
|
|
|
|
redis:
|
|
|
|
|
image: "redis:alpine"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
新volumes密钥将主机上的项目目录(当前目录)挂载到/code容器内,允许您即时修改代码,而无需重建映像。environment键设置 FLASK_ENV环境变量,它告诉flask run在开发模式下运行并在更改时重新加载代码。这种模式应该只在开发中使用
|
|
|
|
|
|
|
|
|
|
#### 8.更新应用程序
|
|
|
|
|
|
|
|
|
|
由于应用程序代码现在使用卷安装到容器中,因此您可以对其代码进行更改并立即查看更改,而无需重建映像
|
|
|
|
|
|
|
|
|
|
更改问候语app.py并保存。例如,将Hello World! 消息更改为Hello from Docker!:
|
|
|
|
|
|
|
|
|
|
![image-20220621172737014](Docker-compose.assets/image-20220621172737014.png)
|
|
|
|
|
|
|
|
|
|
#### 9.在浏览器中刷新应用程序
|
|
|
|
|
|
|
|
|
|
![image-20220621173011640](Docker-compose.assets/image-20220621173011640.png)
|
|
|
|
|
|
|
|
|
|
#### 10.其他命令
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian composetest]# docker-compose up -d //后台运行
|
|
|
|
|
[root@xingdian composetest]# docker-compose ps //列出容器
|
|
|
|
|
[root@xingdian composetest]# docker-compose pause //暂停服务
|
|
|
|
|
[root@xingdian composetest]# docker-compose unpause //取消暂停服务
|
|
|
|
|
[root@xingdian composetest]# docker-compose down //停止并移除容器、网络
|
|
|
|
|
[root@xingdian composetest]# docker-compose logs //查看容器的输出
|
|
|
|
|
[root@xingdian composetest]# docker-compose start //启动服务
|
|
|
|
|
[root@xingdian composetest]# docker-compose stop //停止服务
|
|
|
|
|
[root@xingdian composetest]# docker-compose stop [SERVICE...]
|
|
|
|
|
[root@xingdian composetest]# docker-compose cp //在服务容器和本地文件系统之间复制文件/文件夹
|
|
|
|
|
[root@xingdian composetest]# docker-compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH
|
|
|
|
|
[root@xingdian composetest]# docker-compose run web env //为您的服务运行一次性命令,查看 web服务可以使用哪些环境变量
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|