Accessing a port on a Docker container launched from the job

Hello,

I’m using the docker executor and setup_remote_docker, running some steps that build a few Docker images, then launch each of them (one at a time) and try to connect to them from Python. There’s some setup and fiddling involved: a volume needs to be prepared, then fed into each container, so I don’t think I can set them up statically in config.yml.

I can give an example using the standard postgres container. If I try something basic like this when logged in over SSH, it doesn’t work:

docker run --name some-postgres -p 55432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres

circleci@1658bba58a0b:~$ netcat -vz localhost 55432
netcat: connect to localhost (127.0.0.1) port 55432 (tcp) failed: Connection refused
netcat: connect to localhost (127.0.0.1) port 55432 (tcp) failed: Connection refused
netcat: connect to localhost (::1) port 55432 (tcp) failed: Cannot assign requested address

I noticed in docker ps that my cimg/python:3.11 is running with network container:..., and if I copy that over, it sort of works with an implicit port mapping:

docker run --network container:1658bba58a0bdccd6ae511c180831b8068027077a8da73765c8c6f28ca06247f  --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

netcat -vz localhost 5432
Connection to localhost (127.0.0.1) 5432 port [tcp/postgresql] succeeded!

That seems a bit fishy though, I’d have to dig around to find the pause container ID, and I can’t explicitly do port mappings.

Another thing that seemed to work is inspecting my container and finding its IPAddress under NetworkSettings. I can connect on the internal port.

docker run   --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 55432:5432 -d postgres

netcat -vz 172.17.0.3 55432
netcat: connect to 172.17.0.3 port 55432 (tcp) failed: Connection refused

netcat -vz 172.17.0.3 5432
Connection to 172.17.0.3 5432 port [tcp/postgresql] succeeded!

Is that safe to rely on? What’s the right way to start a container dynamically and access a port on it?

Thanks,
Mike

In general, what you would do is split the work into independent tasks

  • The first task would use a docker container and allow you to build your docker images as you are currently doing, but you would then push them to a repo such as docker hub.

  • Run the second task as a normal machine environment, which will then allow a docker environment to be created without any limitations as you are no longer running docker within docker.

Doing this will provide a ‘stable’ environment in which you can run docker very much the same way as you would within your own environment or on most third-party environments you may deploy to.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.