Docker compose issues with PostgreSQL
When I was working with docker-compose files, found some interesting issue and I couldn’t find the root cause for that issue, but sharing for others.
I am trying to develop a back-end API application using Python FastAPI tech stack. For development purpose I used sqlite3 database and for deployment I planned to use PostgreSQL database. So decided to work on docker-compose file. Created docker compose file like below.
When running docker-compose up -d after build, Got below errors. Docker compose file looks good and I tried to debug all the ways to figure out the root cause. But I am unable to do it and left it for some days.
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "hrmdb_service" (172.19.0.2), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "hrmdb" (172.19.0.2), port 5432 failed: FATAL: the database system is starting up
After some days started working on this issue, somehow I changed the serivce name for db in docker-compose file at line no 14. from hrdmdb_service to hrmdb and it started working. I am not sure it is giving issue, as per the documentation, there is no problem with the service name.
As per my experience when working with docker compose file with PostgreSQL, double check these two points
- Credentials: One of the reason could be invalid user name and password in the database url.
- Service name: Try to avoid using service name in the format *_service. Use service name without _service in the name
- Database URL: A db url should not contain localhost or IP address, it should contain service name of database in the docker compose file.