mcpField
D
docker-vps-2026

docker-vps-2026

github.com/krishamaze云与 DevOps038 次安装29 周前更新
代码仓库

关于 docker-vps-2026

docker-vps-2026 是面向在 VPS 上部署 Docker 服务的最佳实践指南,涵盖 Compose、容器配置、安全和运维。内容包括 Python 与浏览器自动化容器示例,以及防火墙和数据备份注意事项。

主要功能

  1. 01Docker Compose v2 命令示例
  2. 02基于 uv 的 Python 容器配置
  3. 03浏览器自动化容器配置示例
  4. 04服务暴露与防火墙安全建议
  5. 05卷持久化、备份和健康检查

适用场景

  1. 01在 VPS 上构建并启动 Docker 服务
  2. 02配置 Python 或浏览器自动化容器
  3. 03检查端口暴露和防火墙规则
  4. 04为容器数据规划持久化与备份

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/krishamaze/skills --skill docker-vps-2026

安装后在 Claude Code 中输入/docker-vps-2026确认已加载。

详细介绍

Docker + VPS 2026 — 最佳实践

Docker Compose v2(2026 标准)

# ✅ 2026 — Docker Compose v2 (built into Docker)
docker compose up -d
docker compose build
docker compose logs -f service-name

# ❌ Legacy v1 (deprecated, removed in 2024)
docker-compose up   # hyphenated — never use

Python 容器(基于 uv,2026)

FROM python:3.12-slim-bookworm

# Copy uv from official image — always pin the version tag
COPY --from=ghcr.io/astral-sh/uv:0.10.7 /uv /uvx /bin/

WORKDIR /app

# Layer cache: install deps before copying app code
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-install-project --no-dev

COPY . .
RUN uv sync --frozen --no-dev

ENV PATH="/app/.venv/bin:$PATH"
# Never run as root
RUN useradd -m appuser && chown -R appuser:appuser /app
USER appuser

CMD ["python", "-m", "api.main"]

浏览器自动化容器(Xvfb + Camoufox)

FROM python:3.12-slim-bookworm

COPY --from=ghcr.io/astral-sh/uv:0.10.7 /uv /uvx /bin/

# System deps for Firefox + virtual display
RUN apt-get update && apt-get install -y --no-install-recommends \
    xvfb x11vnc \
    libgtk-3-0 libdbus-glib-1-2 libxt6 libx11-xcb1 \
    libxcomposite1 libxdamage1 libxfixes3 libxrandr2 \
    libasound2 libpangocairo-1.0-0 libatk1.0-0 \
    libatk-bridge2.0-0 libcups2 libdrm2 libgbm1 \
    curl procps \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-install-project --no-dev

# Download Camoufox Firefox binary
# ⚠️  Verify the expected version in pyproject.toml; camoufox fetch downloads
#    a pre-built Firefox binary — pin the camoufox package version in uv.lock
#    and validate the download hash when possible.
RUN uv run python -m camoufox fetch

COPY . .
RUN uv sync --frozen --no-dev

ENV PATH="/app/.venv/bin:$PATH"
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

Docker Compose — 完整技术栈

# docker-compose.yml
services:
  browser:
    build:
      context: ./browser
      dockerfile: Dockerfile
    container_name: threads-browser
    restart: unless-stopped
    environment:
      DISPLAY: ":99"
      SCREEN_RES: "1920x1080x24"
    ports:
      - "127.0.0.1:5900:5900"  # VNC — bind to localhost only!
    volumes:
      - ./sessions:/sessions
      - /dev/shm:/dev/shm     # shared memory for Firefox
    shm_size: "2gb"

  api:
    build:
      context: ./api
    container_name: threads-api
    restart: unless-stopped
    environment:
      SESSION_DIR: /sessions
    ports:
      - "127.0.0.1:8000:8000"  # bind to localhost only — Nginx handles external
    volumes:
      - ./sessions:/sessions
    depends_on:
      - browser

  dashboard:
    build:
      context: ./dashboard
    container_name: threads-dashboard
    restart: unless-stopped
    environment:
      NEXT_PUBLIC_API_URL: http://localhost:8000
    ports:
      - "127.0.0.1:3000:3000"

  nginx:
    image: nginx:1.28.2-alpine3.23  # pin stable — never use bare :alpine/:latest
    container_name: threads-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/ssl:ro

安全规则(2026)

# ✅ Never expose internal services directly to internet
ports:
  - "127.0.0.1:8000:8000"  # only localhost — Nginx proxies externally

# ❌ Never do this
ports:
  - "8000:8000"  # exposes to all interfaces

注意

以下命令会修改主机级防火墙规则,并且需要 root 权限。切勿在 CI 或自动化脚本中无人值守地运行。请先检查, 并仅在目标 VPS 上手动执行。

# UFW rules on VPS (manual — requires human review)
sudo ufw default deny incoming
sudo ufw allow 22/tcp   # SSH
sudo ufw allow 80/tcp   # HTTP
sudo ufw allow 443/tcp  # HTTPS
sudo ufw deny 5900      # Block VNC from outside after first use
sudo ufw enable

卷策略

volumes:
  - ./sessions:/sessions      # named bind mount — survives container rebuild
  - /dev/shm:/dev/shm         # shared memory — required for Firefox stability
# Never lose sessions: backup before any docker operations
tar -czf sessions-backup-$(date +%Y%m%d).tar.gz ./sessions/

实用命令(2026)

# Build and start all services
docker compose up -d --build

# Tail logs for a specific service
docker compose logs -f browser

# Shell into running container
docker exec -it threads-browser bash

# Check resource usage
docker stats

# Restart single service without rebuilding
docker compose restart api

# Full teardown (preserves volumes)
docker compose down

# Full teardown including volumes — DANGER, deletes sessions
docker compose down -v

健康检查

services:
  api:
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

.dockerignore

.venv/
__pycache__/
*.pyc
.env
.git/
node_modules/
sessions/
*.log

反模式

# ❌ Never run as root in production
CMD ["python", "app.py"]  # without USER directive — runs as root

# ❌ Never use latest tag for base images in production
FROM python:latest

# ❌ Never install pip packages globally in 2026 Dockerfiles
RUN pip install fastapi  # use uv instead

# ❌ Never copy all files before installing deps (kills layer cache)
COPY . .
RUN uv sync  # every code change invalidates dep cache

参考资料

评论

支持 Markdown