0

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:
      - [email protected]
      - 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.

2
  • 1
    I assume the python file is on your desktop and not within a docker container -- can you try using host=localhost instead of 127.0.0.1, since you used localhost:5050 to connect to pgadmin? Does that make a difference? And can you post the python error? Commented Sep 28, 2023 at 15:59
  • @richyen : I'm using a VS Code dev Container to run python. And using localhost returns: psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Commented Sep 28, 2023 at 16:35

2 Answers 2

4

this explains the problem

I'm using a VS Code dev Container to run python.

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

you can add this line to your .devcontainer/devcontainer.json file:

"runArgs": ["--add-host=host.docker.internal:host-gateway"]

In this way, you give the dev container a way to reach your host's address by just using the host name host.docker.internal

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, runArgs change, and connecting to host.docker.internal.5432 worked!! Thank you! However, I still don't get, why I was able to connect WITHOUT these changes, but using 172.17.0.3 and a wrong port (5432, instead of 5433 as specified in the map) and only when launching a container within docker desktop.
If you think another question has an answer, vote to close this one as a duplicate; don't copy the answer into a new one here.
0

For anyone who is interested, there is actually a very simple solution to the original problem:

Instead of using 127.0.0.1 (or localhost) 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 to devcontainer.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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.