Skip to content

docker 及 docker-compose 常用配置 #3

@wuyuedefeng

Description

@wuyuedefeng

连接宿主机服务

连接宿主机mysql的地址: host.docker.internal

容器启动后一直处于运行状态, 方便进入容器调试

entrypoint: ["sleep", "infinity"]


eg:

version: '3.8'  # 补充版本号(语法必需)

services:
  backend:
    build:
      context: ./apps/backend
      dockerfile: Dockerfile
      args:
        - TZ=Asia/Shanghai
    container_name: backend
    extra_hosts:
      - "github.com:140.82.113.3"  # 格式:"域名:IP"
    environment:
      - RAILS_ENV=production
      - REDIS_URL=redis://cache:6379/0
      - DB_HOST=host.docker.internal
      - DB_PORT=3306
    volumes:
      - ./apps/backend:/rails
    depends_on:
      - cache
    restart: always
    # entrypoint: ["sleep", "infinity"]
    # tty: true  # 保持终端连接(可选,增强交互性)
    # ports:
    #   - "9999:3000"
    networks:
      - my_network
      
  cache:
    image: redis:alpine
    container_name: cache
    restart: always
    # 禁用 RDB 持久化,避免磁盘写入操作
    command: redis-server --save "" --stop-writes-on-bgsave-error no --appendonly yes
    volumes:
      - ./redis-data:/data  # 挂载数据卷,持久化 Redis 数据
    networks:
      - my_network

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions