I have two containers in a docker compose, a Postgres DB and a Symfony PHP app.
From within the backend-app (PHP Symfony) I would like to execute commands that handle the DB connection, but none of the configs I try seem to work.
I added expose (https://docs.docker.com/compose/compose-file/#expose) as hinted here and also added a default network as hinted here
docker-compose.yml
version: '3'
services:
database:
image: postgres:11
ports:
- 5431:5432
expose:
- "5431"
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: user
POSTGRES_DB: my_db
volumes:
- ./database:/var/lib/postgresql/data
frontend:
image: frontend-image:latest
ports:
- 8090:80
backend-app:
image: backend-app:latest
links:
- database
backend-web:
image: backend-web:latest
ports:
- 8091:80
depends_on:
- backend-app
- database
networks:
default:
Depending on the DB config in the Backend Container I get different error messages when executing php bin/console doctrine:schema:update --force inside backend-app:
DATABASE_URL="postgresql://user:password@database:5431/my_db?serverVersion=11&charset=utf8"
leads to
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused. Is the server running on host "database" (172.21.0.2) and accepting TCP/IP connections on port 5431?
and
DATABASE_URL="postgresql://user:password@localhost:5431/my_db?serverVersion=11&charset=utf8"
leads to
SQLSTATE[08006] [7] 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 5431?
Where am I wrong in my approaches?