docker compose 主要是应用在开发场景下,方便使用,是本地开发工具,能在本机上部署
1. 使用单个容器部署wordpress
1. 创建MySQL容器
1. sudo docker run -d --name mysql -v mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wordpress mysql:5.7
2. 创建WordPress容器
1. sudo docker run -d -e WORDPRESS_DB_HOST=mysql:3306 --link mysql -p 8080:80 wordpress
2. docker compose语法
2.1 Service
- 一个service代表一个container, 这个container可以从dockerhub的image来创建,或者从本地的Dockerfile build 出来的image来创建
- Service 的启动类似docker run , 我们可以给其指定 network 和 volume, 所以可以给service指定network 和 Volume 的引用
2.1.1 选用docker hub image
[外链图片转存失败(img-wQEXYsyI-1563492635697)(https://i.imgur.com/rLtH5VW.png)]
2.1.2 选用本地Dockerfile
2.2 Volumes
2.3 Network
2.4 docker compose 例子 部署worldpress
version: '3'
services:
wordpress:
image: wordpress
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_PASSWORD: root
networks:
- my-bridge
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
volumes:
- mysql-data:/var/lib/mysql
networks:
- my-bridge
volumes:
mysql-data:
networks:
my-bridge:
driver: bridge
2.5 docker compose 例子 部署flask web + redis
-
app.py
from flask import Flask from redis import Redis import os import socket app = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname()) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)
-
Dockerfile
FROM python:2.7 LABEL maintaner="Peng Xiao xiaoquwl@gmail.com" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 5000 CMD [ "python", "app.py" ]
-
docker-compose.yaml
version: "3" services: redis: image: redis web: build: context: . dockerfile: Dockerfile ports: - 8080:5000 environment: REDIS_HOST: redis
2.6 水平扩展和负载均衡
1. docker-compose scale 命令 # set number of container for a service
up [options] [--scale SERVICE=NUM...] [SERVICE...]
1. docker-compose up --scale web=3 -d # 同时开启3个web container
2. HAProxy + scale 负载均衡的部署服务
1. 先执行docker-compose.yml 启动服务, 再启动多个APP,实验环境chapter6/lb-scale
1. docker-compose up -d
2. docker-compose up --scale web=3 -d # 可以通过scale增加、减少web容器的数量
例子:
-
app.py
from flask import Flask from redis import Redis import os import socket app = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname()) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)
-
Dockerfile
FROM python:2.7 LABEL maintaner="Peng Xiao xiaoquwl@gmail.com" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 80 CMD [ "python", "app.py" ]
-
docker-compose.yaml
version: "3" services: redis: image: redis web: build: context: . dockerfile: Dockerfile environment: REDIS_HOST: redis lb: image: dockercloud/haproxy links: - web ports: - 8080:80 volumes: - /var/run/docker.sock:/var/run/docker.sock
3. docker compose 命令
3.1 安装 docker compose : https://docs.docker.com/compose/install/
Centos 操作如下
1. sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2. sudo chmod +x /usr/local/bin/docker-compose
3.2 docker-compose 常用命令
1. docker-compose -f docker-compose.yml up -d # 启动yml文件中container
2. docker-compose ps # container list
3. docker-compose stop # stop services
4. docker-compose down # stop services and remove
5. docker-compose start # start services
6. docker-compose images # docker-compose 中的使用的 list images
7. docker-compose exec [options] [-e KEY=VAL...] SERVICE COMMAND [ARGS...]
1. eg: docker-compose exec mysql bash
2. eg: docker-compose exec wordpress bash
8. docker-compose build
1. 可以先将Dockerfile进行编译成image,再使用 docker-compose up 来启动,当然直接使用docker-compose up 也是可以的
2. 当Dockerfile更改后也可以先docker-compose build,再docker-compose up
4 部署一个较为复制的应用
docker-compose.yaml
version: "3"
services:
voting-app:
build: ./voting-app/.
volumes:
- ./voting-app:/app
ports:
- "5000:80"
links:
- redis
networks:
- front-tier
- back-tier
result-app:
build: ./result-app/.
volumes:
- ./result-app:/app
ports:
- "5001:80"
links:
- db
networks:
- front-tier
- back-tier
worker:
build: ./worker
links:
- db
- redis
networks:
- back-tier
redis:
image: redis
ports: ["6379"]
networks:
- back-tier
db:
image: postgres:9.4
volumes:
- "db-data:/var/lib/postgresql/data"
networks:
- back-tier
volumes:
db-data:
networks:
front-tier:
back-tier:
代码见chapter6 -> example-voting-app