0

I have a Postgres Docker Container running locally, and the docker compose code for it looks like this

version: '3.9'
services:
  db:
    image: "postgres"
    container_name: db
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=dbname
  1. The database is started using the docker compose run db command

  2. I then find the IP address of the container once it's running, which is "192.168.240.2"

  3. When I try to connect to the database with SqlAlchemy like the following in a python program (this is on the same computer but outside of the container)

import sqlalchemy

engine = sqlalchemy.create_engine('postgresql://postgres:password@192.168.240.2:5432/dbname')
engine.connect()

It shows me this error:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "192.168.240.2", port 5432 failed: Operation timed out Is the server running on that host and accepting TCP/IP connections?

Anyone knows what the problem is here? thanks!

Tried searching for the error message, but changing the input to the create_engine() function according to other posts various ways still result in the same problem. However, I still imagine something's off with the string?

jiajunxu
  • 21
  • 1
  • 3
  • hmmm doing that still resulted in basically the same error – jiajunxu Feb 02 '23 at 00:54
  • thanks larsks, I edited the original question to label the 3 steps i took. in summary, the sqlalchemy program to connect to the database was run outside of the container but on the same computer. In this case, what should be passed to the create_engine() function? – jiajunxu Feb 02 '23 at 03:36
  • I apologize; I misread your question and you should ignore my previous comment (which I have removed). What you are doing (connecting directly to the container) should work. That said, if you're publishing a port (which you are, in the `ports` section of your `docker-compose.yaml`), you should be connecting to the port on your host, rather than looking up the ip address of the container (that's why we publish ports -- to expose a service outside of the container). – larsks Feb 02 '23 at 03:43
  • Some diagnostics to try: Is postgres actually running (`docker-compose ps`)? Can you connect to the postgres service using `psql`? Do the postgres logs show anything of interest? How did you determine the ip address of the container? – larsks Feb 02 '23 at 03:45
  • The postgres docker is running because i can see it in the docker desktop UI (it says running) I find the IP address of the container by first running `docker ps`, and then, using the container ID returned, run `sudo docker container inspect container_ID`, and the IP address is under NetworkSettings/Networks/IPAddress – jiajunxu Feb 02 '23 at 04:13
  • sorry missed the comment above. so you are suggesting that the IP address of the container is not the one I should be using, but rather, I should use the IP address of the host? – jiajunxu Feb 02 '23 at 04:25
  • https://stackoverflow.com/questions/35429837/docker-compose-port-mapping could this help you? – kocoler Feb 02 '23 at 04:51
  • hmmm not sure how this post applies to my problem? I think I'm already forwarding the port for the postgres docker container? – jiajunxu Feb 02 '23 at 16:29

0 Answers0