I have 2 projects. The first contains this docker-compose.yml
:
# pg/docker-compose.yml
version: "3"
services:
postgres:
image: postgres
environment:
POSTGRES_USER: pguser
POSTGRES_DB: pg
ports:
- "5432:5432"
volumes:
- "postgres-data:/var/lib/postgresql/data"
networks:
- some_net
volumes:
postgres-data:
networks:
some_net:
driver: bridge
The second project has this docker compose file:
# pg_client/docker-compose.yml
version: "3"
services:
postgres_client:
image: tmaier/postgresql-client
command: postgres://pguser@localhost:5432/pg
networks:
- pg_some_net
networks:
pg_some_net:
external: true
The tmaier/postgresql-client
image is extremely simple, it has PostgresSQL installed and runs the command psql DOCKER_COMMAND
at build time. In my case this is psql postgres://pguser@localhost:5432/pg
.
I can connect to my Postgres db just fine from the command line:
$ psql postgres://pgmuser@localhost:5432/pg
psql (12.1)
Type "help" for help.
pg=#
But when the postgres_client
container attempts to connect (after running docker-compose up
), it fails saying
postgres_client_1 | psql: could not connect to server: Connection refused
postgres_client_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
postgres_client_1 | TCP/IP connections on port 5432?
postgres_client_1 | could not connect to server: Address not available
postgres_client_1 | Is the server running on host "localhost" (::1) and accepting
postgres_client_1 | TCP/IP connections on port 5432?
Is there something special about docker-compose that I need to account for when writing my Postgres connection URI?
Any help is much appreciated.