安装compose
[root@localhost ~]# curl -L "https://get.daocloud.io/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 把当前文件放到compose中
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 423 100 423 0 0 109 0 0:00:03 0:00:03 --:--:-- 109
100 11.6M 100 11.6M 0 0 910k 0 0:00:13 0:00:13 --:--:-- 1280k
[root@localhost ~]# chmod 755 !$ 修改权限
chmod 755 /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose --version 查看版本信息
docker-compose version 1.27.3, build 4092ae5d
[root@localhost ~]# vi docker-compose.yml 编辑docker-compose.yml文件
version: '2' 声明版本
services:
xd_db:
image: mariadb:latest
restart: always 相对于开机自启
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp_password
xd_wp:
image: wordpress:latest
restart: always
depends_on:
- xd_db
ports:
- "8888:80"
environment:
WORDPRESS_DB_HOST: xd_db:3306
WORDPRESS_DB_PASSWORD: wp_password
volumes:
db_data:
[root@localhost ~]# vi /etc/docker/daemon.json 修改配置文件
{
“insecure-registries”:[“192.168.200.90:5000”],
“registry-mirrors”: [“https://dhq9bx4f.mirror.aliyuncs.com”,“http://hub.c.163.com”]
}
[root@localhost ~]# systemctl restart docker 重启容器
[root@localhost ~]# ll
total 3412580
-rw-------. 1 root root 1079 Nov 26 16:23 anaconda-ks.cfg
-rw-r--r--. 1 root root 145639219 Dec 3 2020 centos-7-x86_64-minimal.tar.gz
-rw-r--r--. 1 root root 403 Nov 26 23:32 docker-compose.yml
-rw-r--r--. 1 root root 499 Nov 26 22:42 Dockerfile
-rw-r--r--. 1 root root 3314069073 Nov 28 2019 Docker.tar.gz
drwxr-xr-x. 9 1001 1001 4096 Nov 26 21:00 nginx-1.8.0
-rw-r--r--. 1 root root 832104 Apr 21 2015 nginx-1.8.0.tar.gz
-rw-r--r--. 1 root root 33918976 Dec 14 2019 registry_latest.tar
[root@localhost ~]# docker-compose up -d 启动容器(报错)
ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid top-level property "xd_wp". Valid top-level sections for this Compose file are: version, services, networks, volumes, secrets, configs, and extensions starting with "x-".
You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
解决办法:
[root@localhost ~]# vi docker-compose.yml
version: "2"
services:
xd_db:
image: mariadb:latest
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp_password
xd_wp:(在service内部,不可单独成列)
image: wordpress:latest
restart: always
depends_on:
- xd_db
ports:
- "8888:80"
environment:
WORDPRESS_DB_HOST: xd_db:3306
WORDPRESS_DB_PASSWORD: wp_password
volumes:
db_data:
启动容器
[root@localhost ~]# docker-compose up -d 启动容器
Creating network "root_default" with the default driver
Creating volume "root_db_data" with default driver
Pulling xd_db (mariadb:latest)...
latest: Pulling from library/mariadb
da7391352a9b: Pull complete
14428a6d4bcd: Pull complete
2c2d948710f2: Pull complete
22776aa82430: Pull complete
90e64230d63d: Pull complete
f30861f14a10: Pull complete
e8e9e6a3da24: Pull complete
420a23f08c41: Pull complete
bd73f23de482: Pull complete
a8690a3260b7: Pull complete
4202ba90333a: Pull complete
a33f860b4aa6: Pull complete
Digest: sha256:cdc553f0515a8d41264f0855120874e86761f7c69407b5cfbe49283dc195bea8
Status: Downloaded newer image for mariadb:latest
Pulling xd_wp (wordpress:latest)...
latest: Pulling from library/wordpress (拖到虚拟机中)
6ec7b7d162b2: Pull complete
db606474d60c: Pull complete
afb30f0cd8e0: Pull complete
3bb2e8051594: Pull complete
4c761b44e2cc: Pull complete
c2199db96575: Pull complete
1b9a9381eea8: Pull complete
50450ffc67ee: Pull complete
4d1e5a768e83: Pull complete
5e8be0d1df16: Pull complete
7a6395859d40: Pull complete
7306499d3dce: Pull complete
fa6f0ba15ac6: Pull complete
308a9ead128f: Pull complete
2db781a8732e: Pull complete
63d3161e9e46: Pull complete
a08dd591ed8a: Pull complete
931a26282f2a: Pull complete
f5c6b405e809: Pull complete
caf2bb847f73: Pull complete
Digest: sha256:dadd8e9c2ef6dc2fe146cbc5f2edc0ed8ae1026ae252b52f25791be4d7d16600
Status: Downloaded newer image for wordpress:latest
Creating root_xd_db_1 ... done
Creating root_xd_wp_1 ... done
[root@localhost ~]# docker-compose --help 查看帮助文档(与docker --help 内容一样)
Commands:
build Build or rebuild services
config Validate and view the Compose file
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 Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show version information and quit
[root@localhost ~]# docker-compose ps -a 查看容器
Name Command State Ports
----------------------------------------------------------------------------
root_xd_db_1 docker-entrypoint.sh mysqld Up 3306/tcp
root_xd_wp_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8888->80/tcp
打开浏览器,输入网址192.168.200.90:8888,进入网页
[root@localhost ~]# docker-compose images 列出所有镜像
Container Repository Tag Image Id Size
------------------------------------------------------------
root_xd_db_1 mariadb latest 3a348a04a815 406.5 MB
root_xd_wp_1 wordpress latest bc5f6567b763 549.8 MB
[root@localhost ~]# docker-compose down 关闭所有容器
Stopping root_xd_wp_1 … done
Stopping root_xd_db_1 … done
Removing root_xd_wp_1 … done
Removing root_xd_db_1 … done
Removing network root_default
[root@localhost ~]# docker-compose up -d 开启所有服务
Creating network “root_default” with the default driver
Creating root_xd_db_1 … done
Creating root_xd_wp_1 … done
[root@localhost ~]# docker-compose stop 关机/挂起容器
[root@localhost ~]# docker-compose rm -s 删除所有容器
关于docker-compose语法的参考文档
http://www.web3.xin/index/article/182.html