Hi everyone, I’m very new to Docker and running into an issue: I have two containers that I am running with docker-compose - one is a python app, the other a postgres container that has the data I’m analyzing with the app. I can get them both up and running but when connecting to the postgres database, I receive the following error:
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host “db” (172.30.0.2) and accepting
TCP/IP connections on port 8000?
However, using the command line, I CAN successfully connect to the database using
psql -U postgres -h 172.30.0.2 -p 8000 -d mydb
Is there an reason why I can’t connect from the other docker container?
Here is the yaml file I’m using for the containers
version: "3.9"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: "docker"
ports:
- "8000:5432"
volumes:
- ./init.sh:/docker-entrypoint-initdb.d/init.sh
- ./data_store/data.sql:/tmp/psql_data/data.sql
tty: true
stdin_open: true
app:
build: .
ports:
- "8081:8080"
depends_on:
- db
links:
- db
stdin_open: true
tty: true
And the connection code in python is
params = {'host':'db','port':'8000','database':'mydb', 'user':'postgres', 'password':'docker'}
psycopg2.connect(**params)