docker compose 只适用于开发/测试环境

安装

除了 windows 和 mac 都要另外安装 docker compose

  1. 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

需要修改权限

  1. sudo chmod +x /usr/local/bin/docker-compose
  2. # 测试
  3. docker-compose

yml文件的编写

Services:

  • 一个 service 代表一个 container, 这个 container 可以从 dockerhub 的 image 创建,或者从本地的 Dockerfile build 出来的 image 创建
  • Service 的启动类似 docker run, 我们可以给其指定 network 和 volume

volumes:

定义要用到的 volume:

  1. volumes:
  2. db-data:

networks:

定义要用到的 network

示例

用 dockerhub 的 image 构建

  1. services:
  2. db:
  3. image: postgres:9.4
  4. volums:
  5. - "db-data:/var/lib/postgresql/data"
  6. networks:
  7. - back-tier

用本地 Dockerfile 构建:

  1. services:
  2. worker:
  3. build: ./worker
  4. links:
  5. - db
  6. - redis
  7. networks:
  8. - back-tier
  1. networks:
  2. front-tier:
  3. driver: bridge
  4. back-tier:
  5. driver: bridge

构建 WordPress 的 Docker Compose:

  1. version: '3'
  2. services:
  3. wordpress:
  4. image: wordpress
  5. ports:
  6. - 8000:80
  7. environment:
  8. WORDPRESS_DB_HOST: mysql
  9. WORDPRESS_DB_PASSWORD: root
  10. networks:
  11. - my-bridge
  12. mysql:
  13. image: mysql:5.7
  14. environment:
  15. MYSQL_ROOT_PASSWORD: root
  16. MYSQL_DATABASE: wordpress
  17. volumes:
  18. - mysql-data:/var/lib/mysql
  19. networks:
  20. - my-bridge
  21. volumes:
  22. mysql-data:
  23. networks:
  24. my-bridge:
  25. driver: bridge

负载均衡启动多个 service:

  1. version: '3'
  2. services:
  3. redis:
  4. image: redis
  5. web:
  6. build:
  7. context: .
  8. dockerfile: Dockerfile
  9. environment:
  10. REDIS_HOST: redis
  11. lb:
  12. image: dockercloud/haproxy
  13. links:
  14. - web
  15. ports:
  16. - 8080:80
  17. volumes:
  18. - /var/run/docker.sock:/var/run/docker.sock

启动三个 web service:

  1. docker-compose up --scale web=3 -d

Docker Compose 命令

选项:

  1. Options:
  2. -f, --file FILE 指定文件
  3. (default: docker-compose.yml)
  4. -p, --project-name NAME 指定项目名称
  5. (default: directory name)
  6. --verbose Show more output
  7. --log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  8. --no-ansi Do not print ANSI control characters
  9. -v, --version Print version and exit
  10. -H, --host HOST Daemon socket to connect to
  11. --tls Use TLS; implied by --tlsverify
  12. --tlscacert CA_PATH Trust certs signed only by this CA
  13. --tlscert CLIENT_CERT_PATH Path to TLS certificate file
  14. --tlskey TLS_KEY_PATH Path to TLS key file
  15. --tlsverify Use TLS and verify the remote
  16. --skip-hostname-check Don't check the daemon's hostname against the
  17. name specified in the client certificate
  18. --project-directory PATH Specify an alternate working directory
  19. (default: the path of the Compose file)
  20. --compatibility If set, Compose will attempt to convert deploy
  21. keys in v3 files to their non-Swarm equivalent
  22. Commands:
  23. build Build or rebuild services
  24. bundle Generate a Docker bundle from the Compose file
  25. config Validate and view the Compose file
  26. create Create services
  27. down 停止并删除yml文件定义的容器,网络,image volumes
  28. events Receive real time events from containers
  29. exec 执行 container 例如: docker-compose exec mysql bash
  30. help Get help on a command
  31. images 查看 docker compose 定义的 image container
  32. kill Kill containers
  33. logs View output from containers
  34. pause Pause services
  35. port Print the public port for a port binding
  36. ps List containers
  37. pull Pull service images
  38. push Push service images
  39. restart Restart services
  40. rm Remove stopped containers
  41. run Run a one-off command
  42. scale Set number of containers for a service
  43. start Start services
  44. stop Stop services
  45. top Display the running processes
  46. unpause Unpause services
  47. up Create and start containers
  48. version Show the Docker-Compose version information

其他:

  • -d :在后台运行服务容器。但不会打印出 log,debug时可以不加
  • ps:查看 docker compose 启动的容器状况