1.Docker Compose介绍
2.Docker Compose安装
2.1下载使用国内地址进行瞎子啊
2.2授权
2.3测试
3.开始使用Docker Compose
3.1创建测试目录
3.2创建app.py文件
3.3创建requirements.txt
3.4创建Dockerfile
3.5在Compose文件中定义服务
3.6使用Compose构建并运行程序
3.7验证
3.8编辑Compose文件以添加绑定挂载
3.9重新构建
3.10更新应用程序
3.11其它命令
4.compose配置编写规则
4.1配置规则
4.2官网地址
5.一键部署WordPress(官方实例)
5.1定义项目
5.2创建docker-compose.yml
5.3构建项目
定义、运行多个容器(YAML文件)
作用:批量容器编排
Docker Compose是Docke官方的开源项目。需要单独安装
(1)服务services:容器、应用(例如:单个web、redis、redis)
(2)项目project:一组关联的容器。(例如:博客:web,mysql)
2.1下载使用国内地址进行瞎子啊
(1)官网地址(不推荐 国外地址 太慢了)
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
(2)国内地址
curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2.2授权
chmod +x /usr/local/bin/docker-compose
2.3测试
docker-compose --version
官方实例:https://docs.docker.com/compose/gettingstarted/)
3.1创建测试目录
pwd
mkdir composetest
cd composetest/
pwd
3.2创建app.py文件
vim app.py
1 import time
2
3 import redis
4 from flask import Flask
5
6 app = Flask(__name__)
7 cache = redis.Redis(host='redis', port=6379)
8
9 def get_hit_count():
10 retries = 5
11 while True:
12 try:
13 return cache.incr('hits')
14 except redis.exceptions.ConnectionError as exc:
15 if retries == 0:
16 raise exc
17 retries -= 1
18 time.sleep(0.5)
19
20 @app.route('/')
21 def hello():
22 count = get_hit_count()
23 return 'Hello World! I have been seen {} times.\n'.format(count)
View Code
3.3创建requirements.txt
vim requirements.txt
3.4创建Dockerfile
vim Dockerfile
1 # syntax=docker/dockerfile:1
2 FROM python:3.7-alpine
3 WORKDIR /code
4 ENV FLASK_APP=app.py
5 ENV FLASK_RUN_HOST=0.0.0.0
6 RUN apk add --no-cache gcc musl-dev linux-headers
7 COPY requirements.txt requirements.txt
8 RUN pip install -r requirements.txt
9 EXPOSE 5000
10 COPY . .
11 CMD ["flask", "run"]
View Code
3.5在Compose文件中定义服务
vim docker-compose.yml
1 version: "3.9"
2 services:
3 web:
4 build: .
5 ports:
6 - "5000:5000"
7 redis:
8 image: "redis:alpine"
View Code
3.6使用Compose构建并运行程序
docker-compose up
3.7验证
查看localhost:5000
刷新页面
docker image ls
3.8编辑Compose文件以添加绑定挂载
vim docker-compose.yml
1 version: "3.2"
2 services:
3 web:
4 build: .
5 ports:
6 - "5000:5000"
7 volumes:
8 - .:/code
9 environment:
10 FLASK_ENV: development
11 redis:
12 image: "redis:alpine"
View Code
3.9重新构建
docker-compose up
3.10更新应用程序
vim app.py
1 import time
2
3 import redis
4 from flask import Flask
5
6 app = Flask(__name__)
7 cache = redis.Redis(host='redis', port=6379)
8
9 def get_hit_count():
10 retries = 5
11 while True:
12 try:
13 return cache.incr('hits')
14 except redis.exceptions.ConnectionError as exc:
15 if retries == 0:
16 raise exc
17 retries -= 1
18 time.sleep(0.5)
19
20 @app.route('/')
21 def hello():
22 count = get_hit_count()
23 #return 'Hello World! I have been seen {} times.\n'.format(count)
24 return 'Hello from Docker! I have been seen {} times.\n'.format(count)
View Code
3.11其它命令
docker-compose up -d 用于“分离”模式
docker-compose ps 查看当前正在运行的内容
docker-compose run 允许服务运行一次性命令
docker-compose run web env 查看web服务可用的环境变量
docker-compose stop 停止服务
docker-compose down --volumes 关闭所有内容,完全删除容器
4.1配置规则
version: '' #版本
services: #服务
服务1:web
#服务配置
images
build
network
...
服务2:redis
...
服务3:mysql
...
#其它配置 网络/卷、全局规则
volumes:
networks:
configs:
4.2官网地址
https://docs.docker.com/compose/compose-file/compose-file-v3/
官方地址:https://docs.docker.com/samples/wordpress/
5.1定义项目
pwd
mkdir my_wordpress
cd my_wordpress/
5.2创建docker-compose.yml
vim docker-compose.yml
1 version: "3.9"
2
3 services:
4 db:
5 image: mysql:5.7
6 volumes:
7 - db_data:/var/lib/mysql
8 restart: always
9 environment:
10 MYSQL_ROOT_PASSWORD: somewordpress
11 MYSQL_DATABASE: wordpress
12 MYSQL_USER: wordpress
13 MYSQL_PASSWORD: wordpress
14
15 wordpress:
16 depends_on:
17 - db
18 image: wordpress:latest
19 volumes:
20 - wordpress_data:/var/www/html
21 ports:
22 - "8000:80"
23 restart: always
24 environment:
25 WORDPRESS_DB_HOST: db:3306
26 WORDPRESS_DB_USER: wordpress
27 WORDPRESS_DB_PASSWORD: wordpress
28 WORDPRESS_DB_NAME: wordpress
29 volumes:
30 db_data: {}
31 wordpress_data: {}
View Code
5.3构建项目
docker-compose up -d
访问localhost:8000