Docker学习(9) 使用Docker Compose部署应用
- docker compose:可以在docker节点上,以单引擎模式进行多容器的部署与管理
使用Docker Compose部署应用——简介
- 现在大多是的应用都是由更多小的服务相互协作组成的应用
- 比如:
- Web前端
- 订单管理
- 品类管理
- 后台数据库
- docker compose可以通过声明式的配置文件来描述整个应用,使用一条命令部署完成
- 部署成功之后,通过命令对其完整声明周期的管理
- 配置 文件还可以置于版本控制系统执行存储和管理
使用Docker Compose部署应用——详解
- 详细步骤
- Docker Compose的背景
- 安装Docker Compose
- Compose 文件
- 使用Docker Compose部署应用
- 使用Docker Compose管理应用
Docker Compose的背景
- Docker Compose 的前身是Fig,Fig是基于docker的python工具
- 2014年docker公司收购了Orchard公司,将Fig更名为Docker Compose
安装Docker Compose
lhf@lhf-virtual-machine:~$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 326 0 --:--:-- 0:00:01 --:--:-- 326
100 15.4M 100 15.4M 0 0 874k 0 0:00:18 0:00:18 --:--:-- 1358k
lhf@lhf-virtual-machine:~$ sudo chmod +x /usr/local/bin/docker-compose
lhf@lhf-virtual-machine:~$ docker-compose --version
docker-compose version 1.24.1, build 4667896b
Compose文件
- Docker Compose 使用YAML文件定义多服务的应用,YAML是JSON的子集,也可以使用JSON
- Dokcer Compose 默认使用文件名docker-compose.yml,使用-f参数指定具体文件
- 使用compose文件,定义一个包含两个服务(web-fe和redis)的应用
lhf@lhf-virtual-machine:~/docker/counter-app-master$ cat docker-compose.yml
version: "3.5"
services:
web-fe:
build: .
command: python app.py
ports:
- target: 5000
published: 5000
networks:
- counter-net
volumes:
- type: volume
source: counter-vol
target: /code
redis:
image: "redis:alpine"
networks:
counter-net:
networks:
counter-net:
volumes:
counter-vol:
-
这个YAML的结构,首先包含4个一级key:version、services、networks、volumes
- version:必须指定,位于文件第一行,定义compose文件格式的版本。
- services:用于定义不同的应用服务,案例中定义了两个服务(web-fe的web前端和redis的内存数据库服务)
- networks:用于指引docker创建新的网络。默认创建bridge网络:一种单机网络——实现同一主机上容器的连接。
- volumes:指引docker来创建新的卷。
-
services部分定义两个二级:web-fe和redis
- web-fe服务定义的指令:
- build: .指定docker基于当前目录下的Dockerfile定义指令构建新的镜像,该镜像用于启动该服务的容器。
- command: python app.py指定Docker在容器中执行名为app.py 的python脚本ports: 指定docker将容器内(target)的5000端口映射到主机的(pulished)
- networks:是docker可以将服务连接到指定的网络
- volumes:指定docker将counter-vol卷(source:)挂载到容器的/code目录下(target:)
-
redis服务定义的指令:
- image:使docker基于redis:alpine镜像启动一个名为redis 的容器。
- networks:配置redis容器连接到counter-net网络。
使用Docker Compose部署应用
- 检查必要的文件
lhf@lhf-virtual-machine:~/docker/counter-app-master$ ls -l
总用量 20
-rw-rw-r-- 1 lhf lhf 599 10月 23 23:11 app.py
-rw-rw-r-- 1 lhf lhf 367 10月 23 23:11 docker-compose.yml
-rw-rw-r-- 1 lhf lhf 109 10月 23 23:11 Dockerfile
-rw-rw-r-- 1 lhf lhf 128 10月 23 23:11 README.md
-rw-rw-r-- 1 lhf lhf 11 10月 23 23:11 requirements.txt
- 使用Docker Compose将应用启动
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose up -d prod-equus-bass.yml -d
[1] 2141
lhf@lhf-virtual-machine:~/docker/counter-app-master$ Creating network "counter-app-master_counter-net" with the default driver
Creating volume "counter-app-master_counter-vol" with default driver
Building web-fe
Step 1/5 : FROM python:3.4-alpine
- docker-compose up命令,构建所需的镜像,创建网络和卷,并启动容器。
- 查看构建的镜像
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
counter-app-master_web-fe latest 5378f5a84df1 29 minutes ago 84.5MB
multi stage 8b69a716c1a2 5 hours ago 210MB
<none> <none> ffd654cfccfe 5 hours ago 757MB
<none> <none> 639a8715b042 6 hours ago 1.1GB
lhfdocker/web latest 983497a6f68f 32 hours ago 71.4MB
web latest 983497a6f68f 32 hours ago 71.4MB
node latest 4ac0e1872789 4 days ago 933MB
redis alpine 6f63d037b592 6 days ago 29.3MB
alpine latest 965ea09ff2eb 6 days ago 5.55MB
maven latest 3b2476ab3d10 9 days ago 616MB
ubuntu latest cf0f3ca922e0 9 days ago 64.2MB
python 3.4-alpine c06adcf62f6e 7 months ago 72.9MB
java 8-jdk-alpine 3fd9dd82815c 2 years ago 145MB
- 查看构建的容器
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca03b3e55ec2 counter-app-master_web-fe "python app.py" 31 minutes ago Up 31 minutes 0.0.0.0:5000->5000/tcp counter-app-master_web-fe_1
e3a7f618d62c redis:alpine "docker-entrypoint.s…" 31 minutes ago Up 31 minutes 6379/tcp counter-app-master_redis_1
- 浏览器查看服务
- 刷新页面,计数会累加
lhf@lhf-virtual-machine:~/docker/counter-app-master$ firefox localhost:5000
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
web-fe_1 | 172.18.0.1 - - [28/Oct/2019 15:02:10] "GET / HTTP/1.1" 200 -
web-fe_1 | 172.18.0.1 - - [28/Oct/2019 15:02:11] "GET /favicon.ico HTTP/1.1" 404 -
web-fe_1 | 172.18.0.1 - - [28/Oct/2019 15:03:56] "GET / HTTP/1.1" 200 -
web-fe_1 | 172.18.0.1 - - [28/Oct/2019 15:04:03] "GET / HTTP/1.1" 200 -
13.png
使用Dokcer Compose管理应用
-
使用Docker Compose启动、停下和删除应用,以及获取应用的状态。
-
关闭应用
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose down
Stopping counter-app-master_web-fe_1 ...
Stopping counter-app-master_redis_1 ...
redis_1 | 1:signal-handler (1572275254) Received SIGTERM scheduling shutdown...
redis_1 | 1:M 28 Oct 2019 15:07:34.876 # User requested shutdown...
redis_1 | 1:M 28 Oct 2019 15:07:35.029 * Saving the final RDB snapshot before exiting.
redis_1 | 1:M 28 Oct 2019 15:07:35.031 * DB saved on disk
Stopping counter-app-master_web-fe_1 ... done
counter-app-master_redis_1 exited with code 0
counter-app-master_web-fe_1 exited with code 0
Removing counter-app-master_web-fe_1 ... done
Removing counter-app-master_redis_1 ... done
Removing network counter-app-master_counter-net
[1]+ 已完成 docker-compose up
- 特别注意 :counter-vol卷并没有删除,因为卷应该用于数据的长期持久化存储。卷的生命周期与容器是完全解耦的。
- 查看卷
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker volume ls
DRIVER VOLUME NAME
local 1c7824e5eccb82556e165af9b773cb7a10b4aa3fdebec6ac767b1591cb292692
local counter-app-master_counter-vol
- 再次启动应用
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose up -d
Creating network "counter-app-master_counter-net" with the default driver
Creating counter-app-master_redis_1 ... done
Creating counter-app-master_web-fe_1 ... done
- 再次启动的时间很快,因为counter-vol卷已存在。
- 查看应用的状态
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
counter-app-master_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
counter-app-master_web-fe_1 python app.py Up 0.0.0.0:5000->5000/tcp
- 列出各个服务容器内的进程
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose top
counter-app-master_redis_1
UID PID PPID C STIME TTY TIME CMD
-------------------------------------------------------------
999 3750 3712 0 23:11 ? 00:00:00 redis-server
counter-app-master_web-fe_1
UID PID PPID C STIME TTY TIME CMD
------------------------------------------------------------------------------------
root 3787 3743 0 23:11 ? 00:00:01 python app.py
root 3939 3787 1 23:11 ? 00:00:01 /usr/local/bin/python /code/app.py
- 停止应用。不删除资源,并查看状态
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose top
counter-app-master_redis_1
UID PID PPID C STIME TTY TIME CMD
-------------------------------------------------------------
999 3750 3712 0 23:11 ? 00:00:00 redis-server
counter-app-master_web-fe_1
UID PID PPID C STIME TTY TIME CMD
------------------------------------------------------------------------------------
root 3787 3743 0 23:11 ? 00:00:01 python app.py
root 3939 3787 1 23:11 ? 00:00:01 /usr/local/bin/python /code/app.py
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose stop
Stopping counter-app-master_web-fe_1 ... done
Stopping counter-app-master_redis_1 ... done
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------
counter-app-master_redis_1 docker-entrypoint.sh redis ... Exit 0
counter-app-master_web-fe_1 python app.py Exit 0
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
728c56f892d0 counter-app-master_web-fe "python app.py" 5 minutes ago Exited (0) About a minute ago counter-app-master_web-fe_1
321c0e64e8bb redis:alpine "docker-entrypoint.s…" 5 minutes ago Exited (0) About a minute ago counter-app-master_redis_1
eefa35f2eba5 lhfdocker/web:latest "node ./app.js" 32 hours ago Exited (255) 9 hours ago 0.0.0.0:80->8080/tcp c1
- 停止compose应用并不会再系统中删除对应用的定义,仅是对应用的容器停止
- 再次启动应用
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose restart
Restarting counter-app-master_web-fe_1 ... done
Restarting counter-app-master_redis_1 ... done
lhf@lhf-virtual-machine:~/docker/counter-app-master$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
counter-app-master_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
counter-app-master_web-fe_1 python app.py Up 0.0.0.0:5000->5000/tcp
使用Docker Compose部署应用的——命令
-
docker-compose up :部署一个compose应用。默认读取docker-compose.yml文件,可以使有-f参数指定文件,-d 参数在后台启动
-
docker-compose stop:停止compose应用的相关容器。可以通过docker-compose restart重新启动
-
docker-compose rm:删除已停止的compose应用的容器,会删除容器和网络,不会删除卷和镜像。
-
docker-compose 重启compose应用
- 如果compose应用进行了变更,需要重启才能生效
-
docker-compose ps:列出compose应用的容器
- 输出内容包括:状态、容器的运行命令,已经网络端口
-
docker-compose down:停止并删除运行中compose 的应用。会删除容器和网络,不会删除卷和镜像
网友评论