docker 在 GitHub 操作中构建网络

Mic*_*rts 8 docker docker-compose github-actions

因此,我正在尝试复制一个用于设置我在本地运行良好的 docker 堆栈的流程,在 Github 操作中在尽可能接近生产/现实世界的场景下执行一些测试。

但是,我在 GitHub 操作工作流程上遇到以下问题,导致失败:

psycopg2.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
Run Code Online (Sandbox Code Playgroud)

本质上,在本地网络中有效的方法在 Github 操作中不起作用。唯一看起来不同的可能是我在 Github 操作中使用的 docker 版本。

这是我的workflow/ci.yml文件:

psycopg2.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
Run Code Online (Sandbox Code Playgroud)

本质上,所有步骤都在进行,直到服务需要通过网络与服务api进行通信,例如,dbdb:5432

我的local.yml文件如下:

version: '3.8'

services:
  traefik:
    image: traefik:latest
    container_name: traefik_proxy
    restart: always
    security_opt:
      - no-new-privileges:true
    command:
        ## API Settings - https://docs.traefik.io/operations/api/, endpoints - https://docs.traefik.io/operations/api/#endpoints ##
        - --api.insecure=true # <== Enabling insecure api, NOT RECOMMENDED FOR PRODUCTION
        - --api.dashboard=true # <== Enabling the dashboard to view services, middlewares, routers, etc...
        - --api.debug=true # <== Enabling additional endpoints for debugging and profiling
        ## Log Settings (options: ERROR, DEBUG, PANIC, FATAL, WARN, INFO) - https://docs.traefik.io/observability/logs/ ##
        - --log.level=ERROR # <== Setting the level of the logs from traefik
        ## Provider Settings - https://docs.traefik.io/providers/docker/#provider-configuration ##
    labels:
        # Enable traefik on itself to view dashboard and assign subdomain to view it
        - traefik.enable=false
        # Setting the domain for the dashboard
        - traefik.http.routers.api.rule=Host("traefik.docker.localhost")
        # Enabling the api to be a service to access
        - traefik.http.routers.api.service=api@internal
    ports:
        # HTTP  
        - 80:80
        # HTTPS / SSL port
        - 443:443
    volumes:
        # Volume for docker admin
        - /var/run/docker.sock:/var/run/docker.sock:ro
        # Map the static configuration into the container
        - ./traefik/traefik.yml:/etc/traefik/traefik.yml:ro
        # Map the configuration into the container
        - ./traefik/config.yml:/etc/traefik/config.yml:ro
        # Map the certificats into the container
        - ./certs:/etc/certs:ro
    networks:
      - web

  api:
    build: .
    command: uvicorn app.main:app --host 0.0.0.0 --port 5000 --reload --workers 1 --ssl-keyfile "./certs/local-key.pem" --ssl-certfile "./certs/local-cert.pem" --ssl-cert-reqs 1
    container_name: perseus_api
    restart: always
    ports:
      - 8001:5000
    volumes: 
      - .:/app
    depends_on:
      - db
    links:
      - db:db
    env_file:
      - .env
    labels:
      # The following labels define the behavior and rules of the traefik proxy for this container 
      # For more information, see: https://docs.traefik.io/providers/docker/#exposedbydefault
      # Enable this container to be mapped by traefik:
      - traefik.enable=true
      # URL to reach this container:
      - traefik.http.routers.web.rule=Host("perseus.docker.localhost")
      # URL to reach this container for secure traffic:
      - traefik.http.routers.websecured.rule=Host("perseus.docker.localhost")
      # Defining entrypoint for https:
      - traefik.http.routers.websecured.entrypoints=websecured
    networks:
      - web
      - api

  db:
    image: postgres:14-alpine
    container_name: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
      - ./scripts/init_pgtrgm_extension.sql:/docker-entrypoint-initdb.d/init_pgtrgm_extension.sql
    ports:
      - 5432:5432
    env_file:
      - .env
    networks:
      - api

volumes:
    postgres_data:

networks:
  web:
    name: web
  api:
    name: api
    driver: bridge
Run Code Online (Sandbox Code Playgroud)

有没有任何网络技巧或 GitHub 操作技巧可以帮助我克服这个看似很小的障碍?

我已经对这个问题做了尽可能多的研究 - 但我似乎看不出解决方案是什么......

Pac*_*ace 3

Github 操作应该允许 docker compose 网络正常工作。我遇到了这个错误,问题最终是我的测试应用程序没有完全等待我的数据库准备好。这depends_on往往是不够的。您需要添加健康检查,然后配置depends_on等待依赖服务健康:

向依赖服务添加健康检查:

  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: mydb
      POSTGRES_PASSWORD: postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
Run Code Online (Sandbox Code Playgroud)

等待依赖服务正常运行:

  app:
    image: myapp/tester
    build:
      context: ../..
      dockerfile: ./ci/docker/tester.Dockerfile
    command: /usr/bin/python -mpytest myapp/tests/test_postgres.py
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      POSTGRES_HOST: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
Run Code Online (Sandbox Code Playgroud)