简体   繁体   中英

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM