I am trying to connect to a dockerized Postgres from Python. However, I have not managed to make it work the way I believe it should work. As I am just a beginner with Docker, I used first used the postgres-pgadmin yaml from the awesome-compose github, as follows:
version: '3.1'
services:
postgres:
image: postgres:16.0
container_name: postgresdb
restart: always
ports:
- "5432:5432"
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- d:/DB/dev:/var/lib/postgresql/data
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4:latest
environment:
- PGADMIN_DEFAULT_EMAIL=myemail@some.com
- PGADMIN_DEFAULT_PASSWORD=pass
ports:
- "5050:80"
restart: always
With this, I can access pgadmin using localhost:5050, and I see that postgres is up and running without errors.
Now, the absolutely only way to set up the postgres server in pgadmin that works for me is to use postgresdb as the host. Using 127.0.0.1, localhost, or the IP shown in inspect does not work. Also, no matter what I try, I can’t connect at all from python. Here is btw the python code:
import psycopg2
connection = psycopg2.connect(database="postgres", user='postgres', password='postgres', host='127.0.0.1', port=5432)
I also checked whether port 5432 is listening using netstat -ad
and I can see both
0.0.0.0:5050
0.0.0.0:5432
These two entries don’t show up when I take the containers down, thus I strongly assume they come from the postgre and pgadmin. So, I don’t understand why I can’t connect to postgres through python.
Then I thought there would be some "oddity" (from a beginner perspective at least) with docker compose, so I spun up another container on the postgres image (using docker desktop directly, not docker compose). This initially also did not work, until I changed the host port to 5433 (i.e. used the 5433:5432
mapping). And now I was able to connect to the server via python, but had to use the "docker" IP, i.e. something like 172.17.0.3
AND it only worked with port 5432, despite the mapping to 5433.
I would have expected that I could use something like 127.0.0.1:5433
to connect, but that does not work.
As for the system, I’m on Windows 11 with WSL2, and docker desktop 4.23.0. I have the suspicion that there is something in Windows 11 that causes the issues, but I don’t know where to even start. Btw, did turn off virus and firewall, but to no avail.
Any help would be highly appreciated.
2
Answers
For anyone who is interested, there is actually a very simple solution to the original problem:
Instead of using
127.0.0.1
(orlocalhost
) in the python script, the IP address of the host machine should be used (for me, it's something like 192.168.0.60). The changes todevcontainer.json
are not necessary.The explanation is, that the loopback address 127.0.0.1 refers back to the docker container (python dev container) as the host, and not the Windows host, i.e. the local machine itself.
In the solution from Ryabchenko, the
host.docker.internal
address actually resolves to the Windows host address (192.168.0.60)this explains the problem
In your
VS Code dev Container
by default you see isolated docker network for your container. You need to access network on your host machine.You need one of answers from Docker in Docker setup with VSCode Dev Containers: How to access running docker containers on the host machine
try this one