Docker container can't connect to each other

Viewed 504

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?

1 Answers

You don't get to choose what the container expose inside the internal docker network created by compose.

The postgress image exposes the port 5432, so your expose: 5431 instruction just exposes a port on which postgress is not listening, so it is useless.

So in between containers, you are stuck to using the port 5432 for postgress, unless you derive from the existing image, change the configuration there and expose the port you'd like.

The instruction below just change the port on which the container is exposed to the outside world, as pointed by the documentation

ports:
  - 5431:5432

This means that you are exposing the port 5432 of the container to the port 5431 of your host.

Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).

Source: https://docs.docker.com/compose/compose-file/#ports


To sum up:

  • your Symfony configuration should read:
    DATABASE_URL="postgresql://user:password@database:5432/my_db?serverVersion=11&charset=utf8"
    
  • the expose directive in your compose file is useless
  • the ports directive as you wrote it there is only convenient if you want to connect from your host machine to the postgress container on a different port than the default one (i.e. because you already have a local postgress server running and, so the port is in use)
Related