github地址:https://github.com/docker/compose/releases
要注意版本对应
安装
mac
# 直接创建连接即可
ln -sf /Applications/Docker.app/Contents/Resources/bin/docker-compose/docker-compose /usr/local/bin/docker-compose
linux
sudo curl -L https://github.com/docker/compose/releases/download/1.23.0-rc2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#添加可执行权限
chmod +x /usr/local/bin/docker-compose
#查看安装版本
docker-compose --version
卸载
rm /usr/local/bin/docker-compose
windows
自带
常用命令
# 测试
docker-compose up --build
# 正式启动
docker-compose up -d
# 关闭删除
docker-compose down
docker-compose ps 显示所有容器
docker-compose restart nginx 重新启动nginx容器
规则
- depends_on 控制启动顺序
- build: ./dir 自动构建镜像,就不需要image了
- command 开机执行的命令,一般不用,因为镜像内一般都配置好了。
- network_mode: “host” 设置网络模式,部署多机器集群的时候,可以考虑
脚本示例
WordPress
docker-compose.yml
version: "3"
services:
db:
image: mysql:8.0
command:
- --default_authentication_plugin=mysql_native_password
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: