11.docker-compose

发表于 未分类 分类,标签:
------------------------------------------------
vim coredns
services:
  coredns:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/coredns:1.12.4
    container_name: coredns
    restart: unless-stopped
    volumes:
      - /data/docker-compose/coredns/Corefile:/Corefile
    ports:
      - "53:53/tcp"
      - "53:53/udp"
    command: -conf /Corefile
    networks:
      lab_net:
        ipv4_address: 10.20.0.3

networks:
  lab_net:
    external: true

vim Corefile
.:53 {
    hosts {
        219.151.183.95 git.yaozh.com
        10.64.1.7 jenkins.yaozh.com
        fallthrough
    }
    forward . 61.128.128.68 8.8.8.8
    cache 30
    log
    errors
    reload
    health
}


------------------------------------------------
vim redis7
services:
  redis:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/redis:7.2-alpine   # 使用较新、轻量的 Redis 版本
    container_name: redis7
    restart: unless-stopped
    ports:
      - "6379:6379"   # 映射到本地 6379 端口
    volumes:
      - /db/redis7:/data  # 数据持久化
    command: ["redis-server", "--appendonly", "yes", "--requirepass", "shimujiuxia"]

    networks:
      lbu_net:
        ipv4_address: 172.20.0.23
    mem_limit: 8g
    cpus: 4.0

networks:
  lbu_net:
    external: true
    
    
------------------------------------------------
vim nginx1.24
services:
  nginx:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/nginx:1.24
    container_name: nginx1.24
    restart: always
    ports:
      - "80:80"  # 宿主机端口映射到容器
      - "443:443"  # 宿主机端口映射到容器
    volumes:
#      - /data/www_nginx/dist:/usr/share/nginx/html
      - /data/www_nginx/nginx.conf:/etc/nginx/nginx.conf
      - /data/www_nginx/vhost:/etc/nginx/vhost
      - /data/www_nginx/ssl:/etc/nginx/ssl
      - /data/www_nginx/logs:/var/log/nginx


------------------------------------------------
创建默认vue项目
docker run -it --rm \
  -v /data/wwwroot/qunr_vue:/app \
  -w /app \
  registry.cn-hangzhou.aliyuncs.com/leiuvn/node:20 \
  bash -c "npm create vite@8.2.0 . -- --template vue && npm install"
  #bash -c "npm create vite@latest . -- --template vue && npm install"
阿里云源
docker run -it --rm \
  --name vite-init \
  -v /data/wwwroot/qunr_vue:/app \
  -w /app \
  registry.cn-hangzhou.aliyuncs.com/leiuvn/node:20 \
  bash -c "\
    npm config set registry https://registry.npmmirror.com && \
    npm config set disturl https://npmmirror.com/dist && \
    npm config set sass_binary_site https://npmmirror.com/mirrors/node-sass && \
    npm config set electron_mirror https://npmmirror.com/mirrors/electron/ && \
    npm config set puppeteer_download_host https://npmmirror.com/mirrors && \
    echo '已切换阿里云 npm 超快源' && \
    npm create vite@8.2.0 . -- --template vue && \
    npm install && \
    echo 'Vite + Vue 项目创建完成!' && \
    npx vite --version"

创建默认django项目
docker run -it --rm \
  -v /data/wwwroot/qunr_api:/app \
  -w /app \
  registry.cn-hangzhou.aliyuncs.com/leiuvn/python:3.12 \
  bash -c "\
    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
    pip config set install.trusted-host mirrors.aliyun.com && \
    pip install --upgrade pip && \
    pip install Django==5.1.5 && \
    django-admin startproject qunr_api . && \
    echo '恭喜!Django 5.1.5 项目已创建成功' && \
    python -c \"import django; print('Django版本:', django.get_version())\""


------------------------------------------------
vim lbu_vue
services:
  vue-app:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/node:20-zijian
    container_name: lbu_vue
    working_dir: /app
    volumes:
      - /data/wwwroot/lbu_vue:/app  # 挂载当前目录到容器
      - /app/node_modules  # 避免 node_modules 被覆盖
    ports:
      - "5173:5173"  # 让外部可以访问 Vue 开发服务器
#    command: ["npm", "run", "dev", "--", "--host", "0.0.0.0"]
    command: ["/bin/sh", "-c", "npm run build && npm run dev -- --host 0.0.0.0"]


------------------------------------------------
vim lbu.cn
services:
  nginx:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/nginx:1.24
    container_name: lbu.cn
    restart: always
    ports:
      - "80:80"  # 宿主机端口映射到容器
    volumes:
      - /data/wwwroot/lbu.cn/dist:/usr/share/nginx/html   # 挂载 Vue `dist` 目录
      - /data/wwwroot/lbu.cn/nginx.conf:/etc/nginx/nginx.conf  # 挂载 Nginx 配置
      - /data/wwwroot/lbu.cn/vhost:/etc/nginx/vhost  # 挂载 Nginx 配置
      - /data/wwwroot/lbu.cn/logs:/var/log/nginx    # 挂载日志目录(可选)


------------------------------------------------
vim lbu_api
services:
  django:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/django:v4.2_zijian
    container_name: lbu_api
    ports:
      - "8000:8000"  # 绑定宿主机端口到容器端口
    volumes:
      - /data/wwwroot/lbu_api:/app
    environment:
      - DJANGO_SETTINGS_MODULE=lbu_api.settings  # 指定 Django 配置
      - PYTHONUNBUFFERED=1
      - DJANGO_SUPERUSER_USERNAME=admin
      - DJANGO_SUPERUSER_EMAIL=lmor@qq.com
      - DJANGO_SUPERUSER_PASSWORD=Shimujiuxia@2028
    working_dir: /app  # 设置工作目录

    command: ["/app/entrypoint.sh", "gunicorn", "--reload", "-b", "0.0.0.0:8000", "lbu_api.wsgi:application"]

项目目录配置
vim entrypoint.sh
#!/bin/bash

# 等待数据库启动(适用于 PostgreSQL / MySQL)
echo "Waiting for database..."
sleep 5  # 可根据情况调整等待时间

# 执行 collectstatic 收集静态文件
python manage.py collectstatic --noinput

# 运行数据库迁移
echo "Applying database migrations..."
python manage.py migrate

# 创建超级用户(如果不存在)
echo "Creating superuser..."
python manage.py shell <<EOF
import os
from django.contrib.auth import get_user_model

User = get_user_model()
username = os.getenv("DJANGO_SUPERUSER_USERNAME", "admin")
email = os.getenv("DJANGO_SUPERUSER_EMAIL", "admin@example.com")
password = os.getenv("DJANGO_SUPERUSER_PASSWORD", "admin123")

if not User.objects.filter(username=username).exists():
    User.objects.create_superuser(username, email, password)
    print(f"Superuser {username} created!")
else:
    print(f"Superuser {username} already exists.")
EOF

# 启动 Django 服务器(或 Gunicorn)
echo "Starting Django server..."
exec "$@"

使用模块显示css,在项目seeting添加
from django.conf import settings
from django.conf.urls.static import static

# 在开发环境中提供静态文件
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


------------------------------------------------
vim s.lbu.cn
services:
  nginx:
    container_name: s.lbu.cn
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/shilei:v1
    ports:
      - "8001:80"
    volumes:
      - /data/wwwroot/s.lbu.cn/:/var/www/html/
    restart: always


------------------------------------------------
vim s.lbu.cn.conf
server {
    listen 80;
    server_name s.lbu.cn;

    return 301 https://$host$request_uri;
}

# 监听 443 端口,反向代理到本地 8080
server {
    listen 443 ssl;
    server_name s.lbu.cn;

    ssl_certificate /etc/nginx/ssl/s.lbu.cn.pem;
    ssl_certificate_key /etc/nginx/ssl/s.lbu.cn.key;

    location / {
        proxy_pass http://192.168.1.212:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        add_header Content-Security-Policy upgrade-insecure-requests;
    }
}

vim lbu.cn.conf
server {
    listen 80;
    server_name lbu.cn;

    root /usr/share/nginx/html;  # 或你的 dist 目录路径
    index index.html;

    location / {
        index index.html;
        try_files $uri $uri/ /index.html;
    }
    location /assets/ {
        root /usr/share/nginx/html;
    }

}


------------------------------------------------
mysql8
services:
  mysql8:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/mysql:8.0
    container_name: mysql8
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: Shimujiuxia@2028
    ports:
      - "3306:3306"
    volumes:
      - /data/mysql8_data:/var/lib/mysql
      # 将宿主机写好的 my.cnf 挂载到容器内的配置扩展目录
      - /data/mysql8_data/my.cnf:/etc/mysql/conf.d/my.cnf:ro
    networks:
      lbu_net:
        ipv4_address: 172.20.0.2
    deploy:
      resources:
        limits:
          cpus: '8.0'
          memory: 16G
        reservations:
          memory: 4G

networks:
  lbu_net:
    external: true

[mysqld]
# ----------------------------------------------------------------------
# 1. 基础与字符集配置
# ----------------------------------------------------------------------
user                    = mysql
pid-file                = /var/run/mysqld/mysqld.pid
socket                  = /var/run/mysqld/mysqld.sock
datadir                 = /var/lib/mysql
default-time-zone       = '+08:00'
character-set-server    = utf8mb4
collation-server        = utf8mb4_unicode_ci
skip-name-resolve       = 1                 # 禁用 DNS 反向解析,提升连接速度

# ----------------------------------------------------------------------
# 2. 连接与线程配置
# ----------------------------------------------------------------------
max_connections         = 1000              # 最大允许连接数
max_connect_errors      = 100000            # 容忍的最大打断连接次数
back_log                = 512               # 监听队列深度
thread_cache_size       = 64                # 线程缓存池大小

# ----------------------------------------------------------------------
# 3. 核心内存配置 (针对 16G 容器限制优化)
# ----------------------------------------------------------------------
# 缓冲池设为 10G(约占容器 16G 限制的 60%~65%),预留内存给连接和系统 overhead
innodb_buffer_pool_size = 10G
innodb_buffer_pool_instances = 8           # 将 buffer pool 拆分为 8 个实例,减少锁竞争

# 临时表与排序缓冲区 (按连接/线程分配,不宜设置过大)
tmp_table_size          = 128M
max_heap_table_size     = 128M
sort_buffer_size        = 4M
join_buffer_size        = 4M
read_buffer_size        = 2M
read_rnd_buffer_size    = 4M

# ----------------------------------------------------------------------
# 4. InnoDB 存储引擎与 I/O 优化
# ----------------------------------------------------------------------
innodb_file_per_table   = 1                 # 独立表空间
innodb_log_file_size    = 1G                # 重做日志文件大小
innodb_log_buffer_size  = 64M               # 日志缓冲区
innodb_flush_log_at_trx_commit = 1          # 1: 最安全 (ACID 强一致);性能要求极高可调为 2
innodb_flush_method     = O_DIRECT          # 避免双重缓存
innodb_max_dirty_pages_pct = 75             # 脏页刷盘阈值
max_allowed_packet      = 64M               # 单个包最大值 (适合大文本/大 SQL)

# ----------------------------------------------------------------------
# 5. 慢查询日志配置
# ----------------------------------------------------------------------
slow_query_log          = 1
slow_query_log_file     = /var/lib/mysql/slow.log
long_query_time         = 1.0               # 超过 1 秒的查询记录慢日志
log_queries_not_using_indexes = 0

[client]
default-character-set   = utf8mb4

[mysql]
default-character-set   = utf8mb4


------------------------------------------------
gitlab
services:
  gitlab:
    image: registry.cn-hangzhou.aliyuncs.com/leiuvn/lab:11.8.3
    container_name: gitlab
    restart: always
    hostname: gitlab
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://l.lbu.cn'
    ports:
      - "8003:80"
    volumes:
      - /db/gitlab/config:/etc/gitlab
      - /db/gitlab/logs:/var/log/gitlab
      - /db/gitlab/data:/var/opt/gitlab
    networks:
      lbu_net:
        ipv4_address: 172.20.0.11
    mem_limit: 16g
    cpus: 4.0

networks:
  lbu_net:
    external: true

容器内部手动备份 gitlab-rake gitlab:backup:create
容器内 /var/opt/gitlab/backups
宿主机 /data/gitlab/data/backups/1749835027_2025_06_14_11.8.3_gitlab_backup.tar

恢复 gitlab-rake gitlab:backup:restore BACKUP=id
拷贝备份文件到目录 /db/gitlab/data/backups

------------------------------------------------



0 篇评论

发表我的评论