Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
Using Compose is basically a three-step process:
Define your app’s environment with a Dockerfile so it can be reproduced anywhere. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment. Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.
- Compose用来定义、运行多个容器。用作批量容器编排。
- Docker Compose需要安装
A docker-compose.yml looks like this:
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: {}
二、Compose安装
下载
curl -L https://get.daocloud.io/docker/compose/releases/download/v2.5.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
授权
chmod +x /usr/local/bin/docker-compose
验证
docker-compose version三、Compose测试 3.1 制作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)
3.2 制作requirements.txt文件
flask redis3.3 制作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"]3.3 制作docker-compose.yml文件
version: "3.9"
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
3.4 启动
docker-compose up
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e8d8c166ddfc composetest_web "flask run" 16 minutes ago Up 16 minutes 0.0.0.0:8000->5000/tcp, :::8000->5000/tcp composetest-web-1 f8b0cbdeaf68 redis:alpine "docker-entrypoint.s…" 16 minutes ago Up 16 minutes 6379/tcp composetest-redis-1
访问
[root@zhouhao composetest]# curl localhost:8000 Hello World! I have been seen 1 times.3.5 检查
查看镜像
docker images REPOSITORY TAG IMAGE ID CREATED SIZE composetest_web latest 1f6cdfc46825 23 minutes ago 185MB
查看网络
docker network ls NETWORK ID NAME DRIVER SCOPE 82beec14f573 bridge bridge local 066d5c0385f0 composetest_default bridge local
查看容器网络
docker network inspect composetest_default3.6 停止容器
docker-compose down
或者ctrl+c
4 Compose配置规则 4.1 docker-compose.yamlversion: "" #版本 services: #服务 服务1:web #服务配置 images build network ... 服务2:redis ... 服务3:redis #其它配置 网络/卷挂载 全局规则 volumes: networks: configs:4.2 depends_on 4.3 deploy 5 Compose部署wordpress
官方网址:https://docs.docker.com/samples/wordpress/
5.1 创建docker-compose.yml文件version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
5.2 启动
docker-compose up
查看启动的容器
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 509cb320cbad wordpress:latest "docker-entrypoint.s…" 52 seconds ago Up 50 seconds 0.0.0.0:8000->80/tcp, :::8000->80/tcp wordpress-wordpress-1 b4be5fefd510 mysql:5.7 "docker-entrypoint.s…" 53 seconds ago Up 52 seconds 3306/tcp, 33060/tcp wordpress-db-1
5.3 后台启动访问测试
docker-compose up -d
停止容器
docker-compose down



