Docker Compose
Docker Compose 进行批量容器编排,无需单独启动多个容器
安装
#官方下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#授权
chmod +x /usr/local/bin/docker-compose
# 加速下载
curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#授权
chmod +x /usr/local/bin/docker-compose
Compose配置变写规则
- docker-compose.yaml核心(三层)
#1、版本号
version: "3.9"
#2、服务名
services:
#可以通过dockerfile构建镜像
web:
build: .
ports:
- "5000:5000"
#可以通过官方镜像引用
redis:
image: "redis:alpine"
#3、其他配置 网络/卷、全局规则
volumes:
networks:
...
使用例子
-
1、创建对应文件夹并进入
mkdir composetest cd composetest
2、编写
app.py
import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): count = get_hit_count() return 'Hello World! I have been seen {} times.\n'.format(count)
3、编写python需导入的包名的
requirements.txt
flask redis
4、配置
Dockerfile
文件将app.py
打包成镜像# syntax=docker/dockerfile:1 FROM python:3.7-alpine #配置国内pip镜像源 RUN echo -e http://mirrors.ustc.edu.cn/alpine/v3.7/main/ > /etc/apk/repositories WORKDIR /code ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 COPY requirements.txt requirements.txt RUN pip install -r requirements.txt EXPOSE 5000 COPY . . CMD ["flask", "run"]
5、编写docker-compose.yaml进行容器编排
version: "3.9" services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine"
6、
docker-compose up
启动,docker-compose up -d
后台启动 -
使用docker-compose部署wp博客
具体步骤同官方快速开始例子
[root@localhost home]# mkdir my_wordpress [root@localhost home]# cd my_wordpress/ [root@localhost my_wordpress]# vi docker-compose.yml [root@localhost my_wordpress]# vi docker-compose.yml
version: '3.3' services: db: image: mysql:5.7 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 WORDPRESS_DB_NAME: wordpress volumes: db_data: {}
docker-compose up