How can I connect to a docker container in github actions?

huangapple go评论105阅读模式
英文:

How can I connect to a docker container in github actions?

问题

我正在尝试使用GitHub Actions设置单元测试工作流程。

单元测试必须使用一个Docker容器。

在我的工作流程中,我拉取并运行Docker镜像(在后台),然后运行一个本地的Go应用程序,通过与其交互来运行测试。

docker pull xxxx.dkr.ecr.us-west-2.amazonaws.com/my-container
docker run -d xxxx.dkr.ecr.us-west-2.amazonaws.com/my-container
sleep 10 # 确保容器设置完成
docker exec my-container apk add curl
docker exec my-container curl http://localhost:1234 -I

go test .

我能够通过docker exec进入容器并检查它是否成功运行,所以我知道容器实际上是工作的。

单元测试在本地工作,但是在GitHub Actions中,当尝试连接到Docker容器时,我的Go应用程序会报错,无论主机名是什么:

Post "http://127.0.0.1:1234": dial tcp 127.0.0.1:1234: connect: connection refused

Post "http://localhost:1234": dial tcp [::1]:1234: connect: connection refused

即使在使用名称运行容器并尝试连接后,我仍然会收到错误:

Post "http://my-container:1234": dial tcp: lookup my-container: Temporary failure in name resolution

我相信更简单的解决方案是使用GitHub Actions的services来运行容器,但是我无法弄清楚如何从私有ECR中拉取。

感谢任何帮助!

英文:

I'm trying to setup a unit test workflow with github actions.

There is a docker container that the unit test must use.

In my workflow, I pull and run the docker image (in the background), then run a local go application that runs tests by interacting with it.

          docker pull xxxx.dkr.ecr.us-west-2.amazonaws.com/my-container
          docker run -d xxxx.dkr.ecr.us-west-2.amazonaws.com/my-container
          sleep 10 # make sure container setup finishes
          docker exec my-container apk add curl
          docker exec my-container curl http://localhost:1234 -I

          go test .

I'm able to docker exec into the container and check if it's running successfully, so I know the container is actually working.

The unit test works locally, however in github actions I receive an error from my go app when trying to connect to the docker container, regardless of host name:

Post "http://127.0.0.1:1234": dial tcp 127.0.0.1:1234: connect: connection refused

Post "http://localhost:1234": dial tcp [::1]:1234: connect: connection refused

Even after running the container with a name and trying to connect I get an error:

Post "http://my-container:1234": dial tcp: lookup my-container: Temporary failure in name resolution

I believe the simpler solution is using github actions services to run the container, however I couldn't figure out how to pull from a private ECR.

Appreciate any help!

答案1

得分: 3

我解决了我的问题,尽管我在 Dockerfile 中使用了 EXPOSE,但我还需要使用端口标志 docker run -p 1234:1234 ... 在主机上访问它。

英文:

I figured out my problem, even though I had EXPOSE in the Dockerfile, I needed to run the container with the ports flag docker run -p 1234:1234 ... to access it from host.

答案2

得分: 0

你好!以下是你要翻译的内容:

jobs:
  dump-database:
    runs-on: ubuntu-latest
    container: ubuntu
    services:
      anonymizer:
        image: anonymizer:latest
        env:
          POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
        ports:
          - 5432:5432
    steps:
      - name: Dump database
        run: |
          curl anonymizer:5432/dump > dump.sql
          head -n 150 dump.sql

请稍等片刻,我会为你进行翻译。

英文:
jobs:
  dump-database:
    runs-on: ubuntu-latest
    container: ubuntu
    services:
      anonymizer:
        image: anonymizer:latest
        env:
          POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
        ports:
          - 5432:5432
     steps:
       - name: Dump database
         run: |
           curl anonymizer:5432/dump > dump.sql
           head -n 150 dump.sql

huangapple
  • 本文由 发表于 2022年8月11日 01:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/73310451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定