Docker Compse介绍
当在宿主机启动较多的容器时候,如果每一个都要手动操作来启动的话会非常麻烦而且容易出错,
并且不同的容器互相配合使用的时候有一定的依赖关系,需要按一定的次序来启动才行。
每次启动容器前还得想一想先启动哪一个,这就更加麻烦了。而docker-compose正是用来解决这一问题的工具。
Docker Compose是docker容器的一种单机编排服务,docker-compose 是一个管理多个容器的工具,它有点类似于ansible-playbook命令,可以自动的批量执行容器操作,从而实现自动化的容器管理,使用提前写好的docker-compose.yml文件,按一定的次序来启动,删除,操作容器。
Docker Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排,
docker-compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)。Docker Compose运行目录下的所有文件(docker-compose.yml)组成一个工程,一个工程包含多个服务,每个服务中又定义了容器运行的镜像、参数、依赖,一个服务还可以包括多个容器实例。
Docker Compse官方地址
安装方法
- 方法1:pip安装
- Ubuntu:
[root@ubuntu2004:~]#apt update
[root@ubuntu2004:~]#apt install -y python3-pip
[root@ubuntu2004:~]#pip3 install docker-compose
[root@ubuntu2004:~]# docker-compose --version
docker-compose version 1.27.4, build unknown
- Centos:
# 需要提前配好epel源
[root@centos8 ~]#dnf install -y python3-pip
[root@centos8 ~]#pip3 install docker-compose
- 方法2:从github直接下载
github下载链接比较推荐这种方法,可以自行选择版本。就是可能下载速度有点慢。
[root@ubuntu2004:~]# curl -L https://github.com/docker/compose/releases/download/1.27.4/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 651 100 651 0 0 179 0 0:00:03 0:00:03 --:--:-- 179
100 11.6M 100 11.6M 0 0 483k 0 0:00:24 0:00:24 --:--:-- 171k
[root@ubuntu2004:~]# chmod +x /usr/local/bin/docker-compose
[root@ubuntu2004:~]# docker-compose --version
docker-compose version 1.27.4, build 40524192
- 方法3:直接从包仓库下载
仓库自带的版本比较旧,不是很推荐使用此种方法
- Ubuntu:
[root@ubuntu2004:~]#apt list docker-compose
Listing... Done
docker-compose/focal 1.25.0-1 all
[root@ubuntu2004:~]#apt -y install docker-compose
[root@ubuntu2004 ~]#docker-compose --version
docker-compose version 1.25.0-1, build unknown
- Centos:
# 需要提前配好epel源
#CentOS7安装,依赖EPEL源
[root@centos7 ~]#yum -y install docker-compose
[root@centos7 ~]#docker-compose --version
docker-compose version 1.18.0, buil 8dd22a9
#Centos8的epel仓库中目前还没有docker-compose包
docker-compose命令的使用
[root@ubuntu2004:~]# docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [--] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE 指定Compose文件,默认为当前目录下的docker-compose.yml
-p, --project-name NAME 指定项目名称,默认将使用当前目录名称作为项目名
-c, --context NAME Specify a context name
--verbose Show more output
--log-level LEVEL 设置日志级别(DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
...省略...
Commands:
build Build or rebuild services
config 检查当前.yml文件配置,如果文件格式有错误则报错,没有错误则打印.yml信息
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm 删除已停止的容器
run Run a one-off command
scale Set number of containers for a service
start 启动容器服务
stop 停止容器服务
top Display the running processes
unpause Unpause services
up 创建并启动容器
version Show version information and quit
# 还可以docker-compose 子命令 --help 查看更详细的帮助信息
[root@ubuntu2004:~]# docker-compose config --help
Validate and view the Compose file.
Usage: config [options]
Options:
--resolve-image-digests Pin image tags to digests.
--no-interpolate Don't interpolate environment variables.
-q, --quiet Only validate the configuration, don't print
anything.
--services Print the service names, one per line.
--volumes Print the volume names, one per line.
--hash="*" Print the service config hash, one per line.
Set "service1,service2" for a list of specified services
or use the wildcard symbol to display all services.
docker-compse.yml文件格式
docker-compose文件的格式很多不同版本,版本不同,语法和格式有所不同。详见官方文档。
下面给出几个范例,演示版本为最新的3.9版本。
范例一:nginx
[root@ubuntu2004:/data/docker-compose]# cat docker-compose.yml
version: "3.9"
services:
nginx1:
image: nginx:alpine
restart: always
expose:
- 80
ports:
- "8080:80"
# 检查.yml文件有没有错误
[root@ubuntu2004:/data/docker-compose]# docker-compose config
services:
nginx1:
expose:
- 80
image: nginx:alpine
ports:
- published: 8080
target: 80
restart: always
version: '3.9'
# up是前台启动,一般会加上-d后台启动
[root@ubuntu2004:/data/docker-compose]# docker-compose up
Recreating docker-compose_nginx_1 ... done
Attaching to docker-compose_nginx_1
nginx1_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx1_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx1_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx1_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx1_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx1_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx1_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
^CGracefully stopping... (press Ctrl+C again to force)
Stopping docker-compose_nginx1_1 ... done
[root@ubuntu2004:/data/docker-compose]# docker-compose up -d
Starting docker-compose_nginx_1 ... done
# 查看容器已经启动了,并且可以访问
[root@ubuntu2004:/data/docker-compose]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
docker-compose_nginx1_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8080->80/tcp
[root@ubuntu2004:/data/docker-compose]# curl 10.0.0.202:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 进入容器,类似于docker exec -it nginx sh
[root@ubuntu2004:/data/docker-compose]# docker-compose exec nginx1 sh
/ # ls
bin home proc sys
dev lib root tmp
docker-entrypoint.d media run usr
docker-entrypoint.sh mnt sbin var
etc opt srv
# docker-compose exec nginx1 sh这里使用的nginx1是.yml中的服务名称nginx1
# docker-compose停止所有容器
[root@ubuntu2004:/data/docker-compose]# docker-compose stop
Stopping docker-compose_nginx1_1 ... done
范例二:mysql
[root@ubuntu2004:/data/docker-compose]# cat docker-compose.yml
version: "3.9"
services:
db:
image: mysql:5.7.32
container_name: mysql1
network_mode: "bridge"
expose:
- 3306
ports:
- "3306:3306"
volumes:
- /data/mysql:/var/lib/mysql
#environment:
#- MYSQL_ROOT_PASSWORD=744123
env_file: env_mysql.list
restart: always
# env文件的相对路径为.yml所在目录
[root@ubuntu2004:/data/docker-compose]# cat env_mysql.list
MYSQL_ROOT_PASSWORD=744123
[root@ubuntu2004:/data/docker-compose]# docker-compose config
services:
db:
container_name: mysql1
environment:
MYSQL_ROOT_PASSWORD: '744123'
expose:
- 3306
image: mysql:5.7.32
network_mode: bridge
ports:
- published: 3306
target: 3306
restart: always
volumes:
- /data/mysql:/var/lib/mysql:rw
version: '3.9'
# 这里我们把docker-compose.yml改名为mysql.yml,并切换目录然后试着使用docker-compose -f命令来启动容器
[root@ubuntu2004:/data/docker-compose]# ls
docker-compose.yml env_mysql.list
[root@ubuntu2004:/data/docker-compose]# cp docker-compose.yml mysql.yml
[root@ubuntu2004:/data/docker-compose]# ls
docker-compose.yml env_mysql.list mysql.yml
[root@ubuntu2004:/data/docker-compose]# cd
[root@ubuntu2004:~]#
[root@ubuntu2004:~]# docker-compose -f /data/docker-compose/mysql.yml up -d
Creating mysql1 ... done
# 使用docker ps命令查看,没问题,启动成功了并且端口和名称都和我们指定的一样
[root@ubuntu2004:~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
800990401cc8 mysql:5.7.32 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql1
# 但是使用docker-compose ps好像失败了
[root@ubuntu2004:~]# docker-compose ps
ERROR:
Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?
Supported filenames: docker-compose.yml, docker-compose.yaml
# 我们切回到.yml目录再次执行下docker-compose ps命令
[root@ubuntu2004:~]# cd /data/docker-compose/
[root@ubuntu2004:/data/docker-compose]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
mysql1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
# 居然没问题了,是不是很神奇,所以最好一般还是不要修改默认的docker-compose.yml文件名,也不要切出去目录执行docker-compose命令,否则可能会有很多莫名其妙的问题
范例三:Wordpress + Mysql 快速搭建博客网站
# mysql配置文件
[root@ubuntu2004:/data/docker-compose]# cat /data/mysql_conf/mysql_test.cnf
[mysqld]
server-id=201
log-bin=mysql-bin
# mysql环境变量,创建一个数据库和用户
[root@ubuntu2004:/data/docker-compose]# cat /data/docker-compose/env_mysql.list
MYSQL_ROOT_PASSWORD=744123
MYSQL_DATABASE=wordpress
MYSQL_USER=wpuser
MYSQL_PASSWORD=123456
# wordpress环境变量,指定数据库相关信息
[root@ubuntu2004:/data/docker-compose]# cat /data/docker-compose/env_wordpress.list
WORDPRESS_DB_HOST=mysql:3306 <<== 这里使用容器名称来代替IP
WORDPRESS_DB_NAME=wordpress
WORDPRESS_DB_USER=wpuser
WORDPRESS_DB_PASSWORD=123456
WORDPRESS_TABLE_PREFIX=wp_
# compose文件
[root@ubuntu2004:/data/docker-compose]# cat docker-compose.yml
version: "3.9"
services:
db:
image: mysql:5.7.32
container_name: mysql
expose:
- 3306
ports:
- "3306:3306"
volumes:
- /data/mysql/:/var/lib/mysql/
- /data/mysql_conf/:/etc/mysql/conf.d/
env_file: env_mysql.list
restart: always
wordpress:
image: wordpress
container_name: wordpress
ports:
- 80:80
volumes:
- /data/wp_html/:/var/www/html/
env_file: env_wordpress.list
links:
- "db:mysql"
restart: always
[root@ubuntu2004:/data/docker-compose]# docker-compose config
services:
db:
container_name: mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_PASSWORD: '123456'
MYSQL_ROOT_PASSWORD: '744123'
MYSQL_USER: wpuser
expose:
- 3306
image: mysql:5.7.32
ports:
- published: 3306
target: 3306
restart: always
volumes:
- /data/mysql:/var/lib/mysql:rw
- /data/mysql_conf:/etc/mysql/conf.d:rw
wordpress:
container_name: wordpress
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_PASSWORD: '123456'
WORDPRESS_DB_USRE: wpuser
WORDPRESS_TABLE_PREFIX: wp_
image: wordpress
links:
- db:mysql
ports:
- published: 80
target: 80
restart: always
volumes:
- /data/wp_html:/var/www/html:rw
version: '3.9'
# 使用docker-compose启动服务
[root@ubuntu2004:/data/docker-compose]# docker-compose up -d
Creating network "docker-compose_default" with the default driver
Pulling wordpress (wordpress:)...
latest: Pulling from library/wordpress
852e50cd189d: Already exists
0266fc315b01: Pull complete
4c8a5fa787a1: Pull complete
46fc127c1884: Pull complete
f768b7fadf16: Pull complete
345b578c1a78: Pull complete
90aafe41e78d: Pull complete
af01fae4e5fc: Pull complete
9e463236c8bc: Pull complete
cd12b0a220f2: Pull complete
d9f76220cef4: Pull complete
c09cf9a96ea9: Pull complete
6dda8bec068c: Pull complete
fd36a10c28fe: Pull complete
e11e483ab28e: Pull complete
ab40cb22800e: Pull complete
b08636db59dd: Pull complete
d2c7678bc02b: Pull complete
f903e014412a: Pull complete
114ff1eb7cb0: Pull complete
Digest: sha256:92e97d9b3147038e3cc541a224cc951bef597061827e23a208a24c36bff1c1fe
Status: Downloaded newer image for wordpress:latest
Creating mysql ... done
Creating wordpress ... done
# 查看端口,发现80端口已经打开了
[root@ubuntu2004:/data/docker-compose]# ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6011 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 [::1]:6011 [::]:*
LISTEN 0 4096 *:3306 *:*
LISTEN 0 4096 [::]:111 [::]:*
LISTEN 0 4096 *:80 *:*
web端测试访问一下,哎,成功了。
稍加设置,一个wordpress博客就这样简单的搭好了!是不是很轻松愉快啊@_@ ~