安装
除了 windows 和 mac 都要另外安装 docker compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
需要修改权限
sudo chmod +x /usr/local/bin/docker-compose# 测试docker-compose
yml文件的编写
Services:
- 一个 service 代表一个 container, 这个 container 可以从 dockerhub 的 image 创建,或者从本地的 Dockerfile build 出来的 image 创建
- Service 的启动类似 docker run, 我们可以给其指定 network 和 volume
volumes:
定义要用到的 volume:
volumes:db-data:
networks:
定义要用到的 network
示例
用 dockerhub 的 image 构建
services:db:image: postgres:9.4volums:- "db-data:/var/lib/postgresql/data"networks:- back-tier
用本地 Dockerfile 构建:
services:worker:build: ./workerlinks:- db- redisnetworks:- back-tier
networks:front-tier:driver: bridgeback-tier:driver: bridge
构建 WordPress 的 Docker Compose:
version: '3'services:wordpress:image: wordpressports:- 8000:80environment:WORDPRESS_DB_HOST: mysqlWORDPRESS_DB_PASSWORD: rootnetworks:- my-bridgemysql:image: mysql:5.7environment:MYSQL_ROOT_PASSWORD: rootMYSQL_DATABASE: wordpressvolumes:- mysql-data:/var/lib/mysqlnetworks:- my-bridgevolumes:mysql-data:networks:my-bridge:driver: bridge
负载均衡启动多个 service:
version: '3'services:redis:image: redisweb:build:context: .dockerfile: Dockerfileenvironment:REDIS_HOST: redislb:image: dockercloud/haproxylinks:- webports:- 8080:80volumes:- /var/run/docker.sock:/var/run/docker.sock
启动三个 web service:
docker-compose up --scale web=3 -d
Docker Compose 命令
选项:
Options:-f, --file FILE 指定文件(default: docker-compose.yml)-p, --project-name NAME 指定项目名称(default: directory name)--verbose Show more output--log-level LEVEL Set log 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--tls Use TLS; implied by --tlsverify--tlscacert CA_PATH Trust certs signed only by this CA--tlscert CLIENT_CERT_PATH Path to TLS certificate file--tlskey TLS_KEY_PATH Path to TLS key file--tlsverify Use TLS and verify the remote--skip-hostname-check Don't check the daemon's hostname against thename specified in the client certificate--project-directory PATH Specify an alternate working directory(default: the path of the Compose file)--compatibility If set, Compose will attempt to convert deploykeys in v3 files to their non-Swarm equivalentCommands:build Build or rebuild servicesbundle Generate a Docker bundle from the Compose fileconfig Validate and view the Compose filecreate Create servicesdown 停止并删除yml文件定义的容器,网络,image 和 volumesevents Receive real time events from containersexec 执行 container 例如: docker-compose exec mysql bashhelp Get help on a commandimages 查看 docker compose 定义的 image 和 containerkill Kill containerslogs View output from containerspause Pause servicesport Print the public port for a port bindingps List containerspull Pull service imagespush Push service imagesrestart Restart servicesrm Remove stopped containersrun Run a one-off commandscale Set number of containers for a servicestart Start servicesstop Stop servicestop Display the running processesunpause Unpause servicesup Create and start containersversion Show the Docker-Compose version information
其他:
-d:在后台运行服务容器。但不会打印出 log,debug时可以不加- ps:查看 docker compose 启动的容器状况
