【问题标题】:Github Actions db service container not reachableGithub Actions 数据库服务容器无法访问
【发布时间】:2021-12-03 17:58:18
【问题描述】:

我有以下 Github Actions 管道:

name: Elixir CI

on:
  push:
    branches:
      - '*'

  pull_request:
    branches:
        - '*'

jobs:
  build:
    name: Build and test
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_PORT: 5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
    - uses: actions/checkout@v2
    - name: Docker Setup Buildx
      uses: docker/setup-buildx-action@v1.6.0
      with:
        install: true
    - name: building image
      env:
        DATABASE_HOST: postgres
        DATABASE_PORT: 5432
      run: |
        docker build --build-arg DATABASE_HOST=$DATABASE_HOST -t backend:test -f Dockerfile.ci .

我有一个 Elixir 应用程序的构建步骤:dockerfile 是一个多阶段的,第一阶段运行测试并构建生产应用程序,第二阶段复制应用程序文件夹/tar。

DATABASE_HOST 是我的 Elixir 应用寻找连接到测试环境的变量。

我需要针对 Postgres 运行测试,因此我使用它生成了一个容器服务。我已经在容器中和容器外执行了构建,但总是出现以下错误:


...

#19 195.9 14:10:58.624 [error] GenServer #PID<0.9316.0> terminating
#19 195.9 ** (DBConnection.ConnectionError) tcp connect (postgres:5432): non-existing domain - :nxdomain
#19 195.9     (db_connection 2.4.1) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
#19 195.9     (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
#19 195.9     (stdlib 3.14.2.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
#19 195.9 Last message: nil
...

显然postgres:5432 无法访问,我错过了什么吗?

【问题讨论】:

  • Longshot:把DATABASE_HOST改成localhost还能用吗?

标签: docker github elixir github-actions


【解决方案1】:

我认为问题出在DATABASE_HOST: postgres

服务容器将5432端口导出到主机,因此对于docker build,它应该使用host's ip address访问postgres service,如下所示:

- name: building image
  env:
    DATABASE_PORT: 5432
  run: |
    DATABASE_HOST=$(ifconfig -a eth0|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:")
    docker build --build-arg DATABASE_HOST=$DATABASE_HOST -t backend:test -f Dockerfile.ci .

上面会先用ifconfig获取虚拟机的ip(docker host's ip),然后传给docker build让build容器访问postgres。

【讨论】:

    猜你喜欢
    • 2022-12-21
    • 2013-05-12
    • 2019-07-01
    • 1970-01-01
    • 2022-08-08
    • 2019-12-29
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多