Contents
  1. 1. 安装Docekr Compose
  2. 2. Docker compose常用命令
  3. 3. docker compose实例应用

Docker Compose可以用一个yaml文件定义一组要启动的容器,以及容器运行时的属性,Docker compose将这些称为属性

安装Docekr Compose

Docker Compose目前可以在Linux、Windows和OS X上使用,可以通过直接安装执行包的方式来安装,也可以通过Docker Toolbox安装,也可以通过Python pip包来安装

在Linux上安装Docker Compose

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

该命令从Github下载docker-compose可执行程序安装到/usr/local/bin目录中,之后使用chmod命令确保可执行程序docker-compose可以执行

通过pip安装Docker Compose

设置pip源

1
pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

安装Docker-compose

1
pip install docker-compose

测试Docker Compose 是否安装成功

1
2
[root@ubuntu]# docker-compose --version
docker-compose version 1.21.2, build a133471

docker-compose.yml文件

1
2
3
4
5
6
7
8
9
10
11
web:
image: flag0/composeapp
command: python app.py
ports:
- "5000:5000"
volumes:
- .:/composeapp
links:
- redis
redis:
image: redis
  • image: 指定要使用的镜像

  • build:要生成镜像的Dockerfile路径

    1
    2
    3
    web:
    build:/home/flag0/compose
    ...
  • command:指定服务启动时要执行的命令

  • ports:指定要映射的端口 宿主机:docker

  • volumes:指定要映射的卷

  • links:指定要连接到额外服务的其他服务redis:dbdb即为redis的别名

Docker compose常用命令

启动应用服务

1
docker-compose up

以守护进程(后台)的形式运行Compose

1
docker-compose up -d

查看服务运行状态

1
docker-compose ps

显示Docker Compose服务的日志

1
docker-compose logs

该命令会追踪服务的日志文件,类似于tail -f 命令,退出使用ctrl + c

停止正在运行的服务

1
2
docker-compose stop
docker-compose kill

启动停止的服务

1
docker-compose start

删除Docker Compose服务

1
docker-compose rm

docker compose实例应用

使用Python Flask应用作为例子,该例子使用了以下两个容器

  • 应用容器,运行Python实例程序
  • Redis容器,运行Redis数据库

目录结构

1
2
3
4
5
compose
- app.py
- docker-compose.yml
- Dockerfile
- requirements.txt

创建composeapp目录

1
2
3
mkdir composeapp
cd composeapp
touch Dockerfile

添加程序源代码 app.py文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from flask import Flask
from redis import Redis
import os

app = Flask(__name__)

redis = Redis(host="redis_1",port=6379)

@app.route('/')
def heelo():
redis.incr('hits')
return 'Hello Docker Book redis! I have been seen {0} times'.format(redis.get('hits'))

if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True)

创建 reqiurements.txt文件,来保存应用程序的依赖程序

1
2
flask
redis

composeapp的Dockerfile

1
2
3
4
5
6
7
8
FROM python:2.7
MAINTAINER flag0 <root@flag0.com>

ADD . /composeapp

WORKDIR /composeapp

RUN pip install -r requirements.txt

构建composeapp镜像

1
docker build -t flag0/composeapp .

编写docker-compose.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
vim docker-compose.yml

web:
image: flag0/composeapp
command: python app.py
ports:
- "5000:5000"
volumes:
- .:/composeapp
links:
- redis
redis:
image: redis

使用docker-compose up启动示例应用服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@ubuntu composeapp]# docker-compose up
Creating composeapp_redis_1 ... done
Creating composeapp_web_1 ... done
Attaching to composeapp_redis_1, composeapp_web_1
redis_1 | 1:C 11 Sep 2019 12:54:25.359 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 11 Sep 2019 12:54:25.359 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 11 Sep 2019 12:54:25.359 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 11 Sep 2019 12:54:25.360 * Running mode=standalone, port=6379.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # Server initialized
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # 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.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # 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.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 * Ready to accept connections
web_1 | * Serving Flask app "app" (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: on
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger PIN: 172-456-919

为保证服务是唯一的,compose将docker-compose.yml文件中指定的服务名字加上了目录名作为前缀,并分别使用数字作为后缀

日志输出的每一行都使用算短的服务名字作为前缀,交替输出在一起

查看运行的所有服务

1
2
3
4
5
[root@ubuntu composeapp]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
composeapp_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composeapp_web_1 python app.py Up 0.0.0.0:5000->5000/tcp

查看Docker Compose服务的日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@ubuntu composeapp]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
composeapp_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composeapp_web_1 python app.py Up 0.0.0.0:5000->5000/tcp
[root@iz0201z3qoku45z composeapp]# docker-compose logs
Attaching to composeapp_web_1, composeapp_redis_1
web_1 | * Serving Flask app "app" (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: on
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger PIN: 172-456-919
redis_1 | 1:C 11 Sep 2019 12:54:25.359 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 11 Sep 2019 12:54:25.359 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 11 Sep 2019 12:54:25.359 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 11 Sep 2019 12:54:25.360 * Running mode=standalone, port=6379.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # Server initialized
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # 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.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 # 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.
redis_1 | 1:M 11 Sep 2019 12:54:25.361 * Ready to accept connections

停止正在运行的服务

1
2
3
[root@ubuntu composeapp]# docker-compose stop
Stopping composeapp_web_1 ... done
Stopping composeapp_redis_1 ... done

删除Docker Compose服务

1
2
3
4
5
[root@ubuntu composeapp]# docker-compose rm
Going to remove composeapp_web_1, composeapp_redis_1
Are you sure? [yN] y
Removing composeapp_web_1 ... done
Removing composeapp_redis_1 ... done
Contents
  1. 1. 安装Docekr Compose
  2. 2. Docker compose常用命令
  3. 3. docker compose实例应用