簡單的python web專案的docker-compose.yml 示例

干炸小黄鱼發表於2024-08-12

一個簡單的 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():
    # 資料庫初始化操作

相關文章