[Docker] 04 - Docker Compose
基本概念
Ref: Docker(四):Docker 三剑客之 Docker Compose
Ref: Docker Compose 【菜鸟教程】
Ref: Docker Compose 配置文件详解【关键字详解】
通过 Docker-Compose 用户可以很容易地用一个配置文件定义一个多容器的应用,然后使用一条指令安装这个应用的所有依赖,完成构建。Docker-Compose 解决了容器与容器之间如何管理编排的问题。
Compose 中有两个重要的概念:
-
- 服务 (service) :一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
- 项目 (project) :由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。
安装
#下载 sudo curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose #安装 chmod +x /usr/local/bin/docker-compose #查看版本 docker-compose version
实践笔记
实验视频:https://www.youtube.com/watch?v=Qw9zlE3t8Ko
实验代码:https://github.com/jakewright/tutorials/tree/master/docker/02-docker-compose
-
容器一
先启动一个container,提供了简单的“raw content json"的服务。
$ docker-compose up
$ docker-compose up Creating network "02-docker-compose_default" with the default driver Building product-service Step 1/3 : FROM python:3-onbuild 3-onbuild: Pulling from library/python 1c7fe136a31e: Pulling fs layer ece825d3308b: Downloading [> ] 179ece825d3308b: Downloading [==> ] 7311c7fe136a31e: Downloading [> ] 539.6kB/54.25MB: Waiting ece825d3308b: Downloading [====> ] 1.637MB/17.59MB: Pulling fs layer ece825d3308b: Downloading [=====> ] 1.8ece825d3308b: Downloading [=====> ] 1.997MB/17.59MB 06854774e2f3: Downloading [=> ] 8831c7fe136a31e: Downloading [> ] 1.067MB/54.25MB ece825d3308b: Downloading [======> ] 2.358MB/17.59MB 06854774e2f3: Downloading [==> ] 1.7ece825d3308b: Downloading [=======> ] 2.722MB/17.59MB 1c7fe136a31e: Downloading [=> ] 1.608MB/54.25MB ece825d3308b: Downloading [========> ] 3.083MB/17.59MB ece825d3308b: Downloading [=========> ] 3.263MB/17.59MB 1c7fe136a31e: Downloading [=> ] 2.136MB/54.25MB: Downloading [=====> ] 4.4ece825d3308b: Download1c7fe11c7fe136a31e: Pull complete ece825d3308b: Pull complete 06854774e2f3: Pull complete f0db43b9b8da: Pull complete 2d21c767035c: Pull complete e10b68fb77e6: Pull complete e2c0fb34dff3: Pull complete e96bc319bce0: Pull complete 490280dbcfb7: Pull complete Digest: sha256:76df62c122c910751d8cd3101f8e3da39efd4ee828686b7ff0b5a5b1d967553f Status: Downloaded newer image for python:3-onbuild # Executing 3 build triggers ---> Running in e6cbf14651bf Collecting Flask==0.12 (from -r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/0e/e9/37ee66dde483dceefe45bb5e92b387f990d4f097df40c400cf816dcebaa4/Flask-0.12-py2.py3-none-any.whl (82kB) Collecting flask-restful==0.3.5 (from -r requirements.txt (line 2)) Downloading https://files.pythonhosted.org/packages/15/2e/41485f3d74103412266f3ce07675adf4bd5b4da16f5f3599b2d114374631/Flask_RESTful-0.3.5-py2.py3-none-any.whl Collecting Werkzeug>=0.7 (from Flask==0.12->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB) Collecting Jinja2>=2.4 (from Flask==0.12->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB) Collecting click>=2.0 (from Flask==0.12->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB) Collecting itsdangerous>=0.21 (from Flask==0.12->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl Collecting aniso8601>=0.82 (from flask-restful==0.3.5->-r requirements.txt (line 2)) Downloading https://files.pythonhosted.org/packages/eb/e4/787e104b58eadc1a710738d4e418d7e599e4e778e52cb8e5d5ef6ddd5833/aniso8601-8.0.0-py2.py3-none-any.whl (43kB) Collecting pytz (from flask-restful==0.3.5->-r requirements.txt (line 2)) Downloading https://files.pythonhosted.org/packages/4f/a4/879454d49688e2fad93e59d7d4efda580b783c745fd2ec2a3adf87b0808d/pytz-2020.1-py2.py3-none-any.whl (510kB) Collecting six>=1.3.0 (from flask-restful==0.3.5->-r requirements.txt (line 2)) Downloading https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask==0.12->-r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl Installing collected packages: Werkzeug, MarkupSafe, Jinja2, click, itsdangerous, Flask, aniso8601, pytz, six, flask-restful Successfully installed Flask-0.12 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 aniso8601-8.0.0 click-7.1.2 flask-restful-0.3.5 itsdangerous-1.1.0 pytz-2020.1 six-1.15.0 You are using pip version 10.0.1, however version 20.2.4 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Removing intermediate container e6cbf14651bf ---> 530f22b70da2 Step 2/3 : COPY . /usr/src/app ---> 81053facd47c Step 3/3 : CMD ["python", "api.py"] ---> Running in a958c1b9b964 Removing intermediate container a958c1b9b964 ---> 829920eb1e56 Successfully built 829920eb1e56 Successfully tagged 02-docker-compose_product-service:latest WARNING: Image for service product-service was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Pulling website (php:apache)... apache: Pulling from library/php bb79b6b2107f: Pull complete 80f7a64e4b25: Pull complete da391f3e81f0: Pull complete 8199ae3052e1: Pull complete 284fd0f314b2: Pull complete f38db365cd8a: Pull complete 1416a501db13: Pull complete 1a45b5b978cd: Pull complete c662caa8d2ec: Pull complete 2db216a7247d: Pull complete c3a7647076e8: Pull complete e40fcea67f94: Pull complete 7f3f9920f7b8: Pull complete Digest: sha256:270a13d277cc9fa716d6d668be84298abc7598bfbf468be92416b99de912d1c5 Status: Downloaded newer image for php:apache Creating 02-docker-compose_product-service_1 ... done Creating 02-docker-compose_website_1 ... done Attaching to 02-docker-compose_product-service_1, 02-docker-compose_website_1 product-service_1 | * Running on http://0.0.0.0:80/ (Press CTRL+C to quit) product-service_1 | * Restarting with stat website_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message website_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message website_1 | [Tue Oct 27 23:15:30.865245 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.11 configured -- resuming normal operations website_1 | [Tue Oct 27 23:15:30.865281 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' product-service_1 | * Debugger is active! product-service_1 | * Debugger PIN: 170-685-141 product-service_1 | 172.19.0.1 - - [27/Oct/2020 23:19:39] "GET / HTTP/1.1" 200 - product-service_1 | 172.19.0.1 - - [27/Oct/2020 23:20:00] "GET / HTTP/1.1" 200 - product-service_1 | 172.19.0.3 - - [27/Oct/2020 23:20:00] "GET / HTTP/1.0" 200 - website_1 | 172.19.0.1 - - [27/Oct/2020:23:19:47 +0000] "GET / HTTP/1.1" 200 426 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36" product-service_1 | 172.19.0.3 - - [27/Oct/2020 23:20:00] "GET / HTTP/1.0" 200 - website_1 | 172.19.0.1 - - [27/Oct/2020:23:20:00 +0000] "GET / HTTP/1.1" 200 426 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36" website_1 | 172.19.0.1 - - [27/Oct/2020:23:20:00 +0000] "GET /favicon.ico HTTP/1.1" 404 490 "http://172.19.0.1:5000/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36" website_1 | 172.19.0.1 - - [27/Oct/2020:23:20:39 +0000] "-" 408 0 "-" "-" website_1 | 172.19.0.1 - - [27/Oct/2020:23:20:51 +0000] "-" 408 0 "-" "-" product-service_1 | 172.19.0.3 - - [27/Oct/2020 23:20:54] "GET / HTTP/1.0" 200 - website_1 | 172.19.0.1 - - [27/Oct/2020:23:20:54 +0000] "GET / HTTP/1.1" 200 426 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
实验结果:
-
容器二
docker-compose文件中定义了服务http url。
从中获取内容。
<html> <head> <title>My Shop</title> </head> <body> <h1>Welcome to my shop</h1> <ul> <?php $json = file_get_contents('http://product-service/'); $obj = json_decode($json); $products = $obj->products; foreach ($products as $product) { echo "<li>$product</li>"; } ?> </ul> </body> </html>
美化后,得到html内容,类似前端。
-
配置:docker-compose.yml
.
├── docker-compose.yml
├── product
│ ├── api.py
│ ├── Dockerfile
│ └── requirements.txt
├── README.md
└── website
└── index.php
定义了两个服务。
version: '3'
services:
product-service:
build: ./product
volumes:
- ./product:/usr/src/app
ports:
- 5001:80
website:
image: php:apache
volumes:
- ./website:/var/www/html
ports:
- 5000:80
depends_on:
- product-service
常见命令
-
Docker Compose 常用命令
使用docker-compose up -d
在后台启动服务
[root@localhost composetest]# docker-compose up -d
Starting composetest_web_1 ...
Starting composetest_web_1 ... done
-----------------------------------------------------------------------------------
使用docker-compose ps
命令查看启动的服务
[root@localhost composetest]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
composetest_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composetest_web_1 python app.py Up 0.0.0.0:5000->5000/tcp
-----------------------------------------------------------------------------------
使用docker-compose stop
停止服务。
[root@localhost composetest]# docker-compose stop
Stopping composetest_web_1 ... done
Stopping composetest_redis_1 ... done
-----------------------------------------------------------------------------------
其它常用命令
#查看帮助
docker-compose -h
# -f 指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定。
docker-compose -f docker-compose.yml up -d
#启动所有容器,-d 将会在后台启动并运行所有的容器
docker-compose up -d
#停用移除所有容器以及网络相关
docker-compose down
#查看服务容器的输出
docker-compose logs
#列出项目中目前的所有容器
docker-compose ps
#构建(重新构建)项目中的服务容器。服务容器一旦构建后,将会带上一个标记名,例如对于 web 项目中的一个 db 容器,可能是 web_db。可以随时在项目目录下运行 docker-compose build 来重新构建服务
docker-compose build
#拉取服务依赖的镜像
docker-compose pull
#重启项目中的服务
docker-compose restart
#删除所有(停止状态的)服务容器。推荐先执行 docker-compose stop 命令来停止容器。
docker-compose rm
#在指定服务上执行一个命令。
docker-compose run ubuntu ping docker.com
#设置指定服务运行的容器个数。通过 service=num 的参数来设置数量
docker-compose scale web=3 db=2
#启动已经存在的服务容器。
docker-compose start
#停止已经处于运行状态的容器,但不删除它。通过 docker-compose start 可以再次启动这些容器。
docker-compose stop
End.