简单的python web项目的docker-compose.yml 示例
一个简单的 python web项目, 包含redis, mysql, nginx, 定时业务调度等
其中web启动注册了自定义命令 flask create-db && flask init-db && uwsgi /web/uwsgi.ini
version: '3.5'
services:
db:
image: mysql
container_name: yeping_mysql
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: yeping9527
MYSQL_DATABASE: yeping9527
LANG: C.UTF-8
volumes:
- ./db_data:/var/lib/mysql
- ./mysql_files:/var/lib/mysql-files
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
# ports:
# - "127.0.0.1:3309:3306"
restart: unless-stopped
privileged: true
logging:
driver: "json-file"
options:
max-size: "10m"
redis:
image: redis:alpine
container_name: yeping_redis
volumes:
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
restart: unless-stopped
privileged: true
logging:
driver: "json-file"
options:
max-size: "10m"
web:
image: yeping_web:v106
container_name: yeping_web
command: bash -c "wait-for-it -t 0 db:3306; wait-for-it -t 0 redis:6379; cd /web/主目录 && export FLASK_APP=sites && flask create-db && flask init-db && uwsgi /web/uwsgi.ini"
volumes:
- /root/.ssh/:/root/.ssh
- .:/web
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
- ./db_data:/data/db_data
- ./mysql_files:/data/mysql_files
depends_on:
- db
- redis
#ports:
# - 8112:8000
restart: unless-stopped
privileged: true
logging:
driver: "json-file"
options:
max-size: "10m"
nginx:
image: nginx:alpine
container_name: yeping_nginx
environment:
LANG: C.UTF-8
ports:
- "443:443"
- "80:80"
volumes:
- /opt/work/yeping/config/nginx:/nginx
- /opt/work/yeping:/opt/work/yeping
- /opt/work/yeping/config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- /var/log/yeping/nginx/:/var/log/yeping/nginx/
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
restart: unless-stopped
privileged: true
stdin_open: true
tty: true
logging:
driver: "json-file"
options:
max-size: "10m"
schedulers:
image: yeping:v106
container_name: yeping_schedulers
command: sh -c 'cd /web/主目录 && python -u -m sites.scripts.schedulers'
volumes:
- .:/web
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
- /root/.ssh:/root/.ssh
- ./db_data:/data/db_data
- ./mysql_files:/data/mysql_files
depends_on:
- web
restart: unless-stopped
privileged: true
logging:
driver: "json-file"
options:
max-size: "10m"
自定义清库和数据库初始化命令
import click
@app.cli.command(help="Create all tables form models")
@click.option("--drop", is_flag=True, help="Create after drop.")
def create_db(drop):
# 文件锁
lock_file = "init_create.lock"
if os.path.exists(lock_file):
logging.warning("不是第一次启动,无需创建表。。。")
return
if drop:
db.drop_all()
db.create_all()
os.mknod(lock_file)
@app.cli.command(help="Initialization app data")
def init_db():
# 数据库初始化操作