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