Docker-compose 简介
docker compose是什么?为什么需要docker compose?
Docker-Compose 是用来管理容器的,类似用户容器管家,我们有N多台容器或者应用需要启动的时候,如果手动去操作,是非常耗费时间的,如果有了 Docker-Compose 只需要一个配置文件就可以帮我们搞定,但是 Docker-Compose 只能管理当前主机上的 Docker,不能去管理其他服务器上的服务。意思就是单机环境。
Docker-compose 安装
macOS/Windows
macOS/Windows系统使用的Docker Desktop默认已经安装。
Linux
github下载:https://github.com/docker/compose/releases
将下载好的docker compose移动至/usr/bin 并添加可执行权限
提示:用curl下载注意检查包大小
[root@centos Downloads]# ll
total 11940
-rw-r--r-- 1 root root 12218968 Nov 21 11:07 docker-compose-Linux-x86_64
[root@centos Downloads]# ll -rlht
total 12M
-rw-r--r-- 1 root root 12M Nov 21 11:07 docker-compose-Linux-x86_64
[root@centos Downloads]# mv docker-compose-Linux-x86_64 /usr/bin/docker-compose
[root@centos Downloads]# chmod +x /usr/bin/docker-compose
[root@centos Downloads]# docker-compose version
docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
Docker-compose 演示
通过一个python web小实例来演示Docker-compose的功能。
准备代码
进入存放代码目录:
[root@centos flask_dc]# ls
app.py docker-compose.yml Dockerfile
[root@centos flask_dc]# pwd
/tmp/flask_dc
docker-compose config可查看yaml是否解析成功:
[root@centos flask_dc]# docker-compose config
services:
redis:
image: redis:alpine
restart: always
web:
build:
context: /tmp/flask_dc
depends_on:
redis:
condition: service_started
ports:
- published: 5000
target: 5000
version: '3'
docker-compose.yml:
version: '3'
services:
web:
build:
context: .
depends_on:
- redis
ports:
- "5000:5000"
redis:
image: ${REDIS_VERSION}
restart: always
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)
Dockerfile:
FROM python:3.7-alpine
LABEL maintainer="Zuan"
# 工作路径设定为/code
WORKDIR /code
# 创建环境变量给Flask使用
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
# 复制app.py到容器/code目录
COPY app.py /code
#安装gcc工具、python的依赖
RUN pip install flask && pip install redis
# 映射端口
EXPOSE 5000
STOPSIGNAL SIGTERM
# 为容器设置默认启动命令
CMD ["flask", "run"]
.env:
REDIS_VERSION=redis:alpine
下载镜像并启动
[root@centos flask_dc]# docker-compose up
Creating network "flask_dc_default" with the default driver
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
188c0c94c7c5: Pull complete
fb6015f7c791: Pull complete
f8890a096979: Pull complete
...
Successfully built 266681e8cc63
Successfully tagged flask_dc_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating flask_dc_redis_1 ... done
Creating flask_dc_web_1 ... done
Attaching to flask_dc_redis_1, flask_dc_web_1
redis_1 | 1:C 21 Nov 2020 04:11:21.036 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 21 Nov 2020 04:11:21.036 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 21 Nov 2020 04:11:21.036 # 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 21 Nov 2020 04:11:21.037 * Running mode=standalone, port=6379.
redis_1 | 1:M 21 Nov 2020 04:11:21.037 # 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 21 Nov 2020 04:11:21.037 # Server initialized
redis_1 | 1:M 21 Nov 2020 04:11:21.037 # 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 21 Nov 2020 04:11:21.037 # 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 madvise > /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 (set to 'madvise' or 'never').
redis_1 | 1:M 21 Nov 2020 04:11:21.037 * Ready to accept connections
web_1 | * Serving Flask app "app.py"
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: off
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
浏览器访问
Docker-compose 常用命令
查看配置
docker-compose config
后台启动
docker-compose up -d
构建镜像
只是根据dockerfile构建镜像,不会启动镜像
docker-compose build
下载镜像
docker-compose pull
查看运行状态
docker-compose ps
查看docker-compose启动的进程
docker-compose top
启动
docker-compose start
停止
将容器全部退出
docker-compose stop
[root@centos flask_dc]# docker-compose stop
Stopping flask_dc_web_1 ...
Stopping flask_dc_web_1 ... done
Stopping flask_dc_redis_1 ... done
[root@centos flask_dc]#
[root@centos flask_dc]# docker-compose ps -a
Name Command State Ports
--------------------------------------------------------------------
flask_dc_redis_1 docker-entrypoint.sh redis ... Exit 0
flask_dc_web_1 flask run Exit 137
[root@centos flask_dc]#
暂停
docker-compose pause
docker-compose unpause