skip to Main Content

I have the following docker-compose file and when I ran docker-compose up -d --build, I got a message which indicated success. The logs did not have any exception printed.

[+] Running 2/2
 ✔ Container fastapi-tdd-docker-web-db-1  Running                                                                                                                                         0.0s
 ✔ Container fastapi-tdd-docker-web-1     Started

But when I try to access the postgres DB with docker-compose exec web-db psql -U postgres, then try c web_dev, I get this error

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "web_dev" does not exist
Previous connection kept

here is my docker compose yaml file – what is going wrong here?

version: '3.8'

services:
  web:
    build: ./project
    command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000
    volumes:
      - ./project:/usr/src/app
    ports:
      - 8004:8000
    environment:
      - ENVIRONMENT=dev
      - TESTING=0
      - DATABASE_URL=postgres://postgres:postgres@web-db:5432/web_dev
      - DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/web_test
    depends_on:
      - web-db

  web-db:
    build:
      context: ./project/db
      dockerfile: Dockerfile
    expose:
      - 5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

2

Answers


  1. Everything in your files is correct, but it looks like the database is not initialized as you expect it to be. This can happen because the database was initialized in the previous runs. So you need to remove the volumes which were set up to store the database.

    You can request removal of volumes docker-compose down --volumes

    Then, update the containers docker-compose up -d --build

    Login or Signup to reply.
  2. I had the same issue. My issue was with the docker file in

    PROJECTdbDockerfile
    

    I had a typo in "/docker-entrypoint-initdb.d" this caused the same error. I copied and pasted their example and then rebuilt the image and it worked.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search