In my symfony project, I create a docker container for postgresql:
php:
build:
context: "."
dockerfile: ./docker/php/Dockerfile
volumes:
- .:/var/www/symfony:rw
depends_on:
- postgres
postgres:
image: postgres:13-alpine
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: mydb
ports: [5432]
In my .env file I try to connect to database as follows:
DATABASE_URL="postgresql://root:root@postgres:5432/mydb?serverVersion=15&charset=utf8"
When I try to migrate to DB, I get the following error:
[critical] Error thrown while running command "make:migration". Message: "An exception occurred in the driver: SQLSTATE[08006] [7] could not translate host name "postgres" to address: nodename nor servname provided, or not known"
What am I missing?
CodePudding user response:
I think that you missed to set up an inter-container internal network. It does not come as a default.
Join both php
and postgres
services to a network (e.g. internal
):
services:
php:
...
networks:
- internal
...
postgres:
...
networks:
- internal
...
networks:
internal:
That last bit of network definition is left blank on purpose - we are assuming default bridge mode but you can define whichever you like.