Docker Compose - Quick Start

Docker Compose Quick Start

对 官网 Docker Compose Quick Start 的一些简单记录

用 Docker Compose 来解决什么问题 ?

比如:使用 docker 来构建 redis 哨兵模式来学习,构建 ELK ,或者提供一个当前系统的最小可用版本提供其他组测试对接用

例子

创建测试用的项目

1
2
mkdir composetest
cd composetest

新建 app.py 到文件夹中

文件位置:composetest/app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 f'Hello World! I have been seen {count} times.\n'

连接同一个 network 环境下名为 redis 的redis容器服务,git_hit_count() 是对 redis 服务的重试连接

新建另外一个文件 requirements.txt

文件位置:composetest/requirements.txt
1
2
flask
redis

新建 Dockerfile 文件

文件位置:composetest/Dockerfile
1
2
3
4
5
6
7
8
9
10
11
# syntax=docker/dockerfile:1
FROM python:3.10-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", "--debug"]

新建 compose.yaml

文件位置:composetest/compose.yaml
1
2
3
4
5
6
7
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"

以上定义了2个服务:

  1. web 服务来自由 Dockerfile 构建镜像。
  2. redis 服务

build and run compose

在当前目录下执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
docker compose up

Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
web_1 | * Restarting with stat
redis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
web_1 | * Debugger is active!
redis_1 | 1:M 17 Aug 22:11:10.483 # Server initialized
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
web_1 | * Debugger PIN: 330-787-903
redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections

检验服务情况,访问 localhost:8000

1
Hello World! I have been seen 1 times.

停止应用程序,方法是在第二个终端中的项目目录中运行 docker compose down,或者在启动应用程序的原始终端中按 CTRL+C 停止应用程序。

使用 Compose Watch

修改 compose.yaml 文件,添加 watch 用来预览正在运行的 Compose 服务,在编辑和保存代码时会自动更新:

文件位置:composetest/compose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
services:
web:
build: .
ports:
- "8000:5000"
develop:
watch:
- action: sync
path: .
target: /code
redis:
image: "redis:alpine"

每当文件发生更改时,Compose 都会将文件同步到容器内的相应位置。复制完成后,捆绑器会更新正在运行的应用程序,而无需重新启动。

输入 docker compose watchdocker compose up --watch 来构建和启动应用程序并启动文件监视模式

修改 app.py 并重新访问 localhost:8000

1
2
3
4
5
@app.route('/')
def hello():
count = get_hit_count()
- return f'Hello World! I have been seen {count} times.\n'
+ return f'Hello from Docker! I have been seen {count} times.\n'

拆分服务

将 redis 服务定义从 compose.yaml 中拆分出来

新建 infra.yaml

文件位置:composetest/infra.yaml
1
2
3
services:
redis:
image: "redis:alpine"

在 compose.yaml 中引入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+ include:
+ - infra.yaml
services:
web:
build: .
ports:
- "8000:5000"
develop:
watch:
- action: sync
path: .
target: /code
- redis:
- image: "redis:alpine"

运行 docker compose up 命令,使用更新后的 Compose 文件构建应用程序并运行它。你应该会在浏览器中看到 Hello world 消息

其他命令

运行 docker compose up 命令,使用更新后的 Compose 文件构建应用程序并运行它。你应该会在浏览器中看到 Hello world 消息

1
2
3
4
5
6
7
8
9
10
11
docker compose up -d

Starting composetest_redis_1...
Starting composetest_web_1...

docker compose ps

Name Command State Ports
-------------------------------------------------------------------------------------
composetest_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composetest_web_1 flask run Up 0.0.0.0:8000->5000/tcp

使用 docker compose up -d 启动了 Compose,请在使用完后停止服务

1
docker compose stop

可以使用 docker compose down 命令关闭所有内容,彻底删除容器

- the End -
0%