在 Debian 上用 Docker 落地微服务架构
一 环境准备与 Docker 安装
二 单体到微服务的落地步骤
三 关键文件示例
目录结构
示例一 Node.js 用户服务 Dockerfile(多阶段 + 非 root)
示例二 docker-compose.yml(含健康检查与网络隔离)
version: “3.8” services: user-service: build: ./services/user-service environment: NODE_ENV: production PORT: 3000 DB_URL: postgres://user:pass@postgres:5432/userdb ports: - “3000:3000” networks: - backend healthcheck: test: [“CMD”, “curl”, “-f”, “http://localhost:3000/health”] interval: 30s timeout: 10s retries: 3
order-service: build: ./services/order-service environment: NODE_ENV: production PORT: 3001 DB_URL: postgres://user:pass@postgres:5432/orderdb depends_on: postgres: condition: service_healthy networks: - backend healthcheck: test: [“CMD”, “curl”, “-f”, “http://localhost:3001/health”] interval: 30s timeout: 10s retries: 3
postgres: image: postgres:15 environment: POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: userdb volumes: - pgdata:/var/lib/postgresql/data networks: - backend healthcheck: test: [“CMD-SHELL”, “pg_isready -U user -d userdb”] interval: 10s timeout: 5s retries: 5
gateway: image: nginx:1.25-alpine ports: - “80:80” volumes: - ./gateway/nginx.conf:/etc/nginx/nginx.conf:ro depends_on: user-service: condition: service_healthy order-service: condition: service_healthy networks: - backend
volumes: pgdata:
networks: backend: driver: bridge
说明
四 运维与扩展
五 一键验证与常见问题