Home > OS >  Can't connect with docker-compose to Postgres database
Can't connect with docker-compose to Postgres database

Time:06-10

I'm trying to build a docker-compose file that will spin up my EF Core web api project, connecting to my Postgres database.

I'm having a hard time getting the EF project connecting to the database.

This is what I currently have for my docker-compose.yml:

version: '3.8'

services:

  web:
    container_name: 'mybackendcontainer'
    image: 'myuser/mybackend:0.0.6'
    build:
      context: .
      dockerfile: backend.dockerfile
    ports:
     - 8080:80
    depends_on:
     - postgres
    networks:
      - mybackend-network

  postgres:
    container_name: 'postgres'
    image: 'postgres:latest'
    environment:
      - POSTGRES_USER=username
      - POSTGRES_PASSWORD=MySuperSecurePassword!
      - POSTGRES_DB=MyDatabase
    networks:
      - mybackend-network
    expose: 
      - 5432
    volumes:
      - ./db-data/:/var/lib/postgresql/data/

  pgadmin:
    image: dpage/pgadmin4
    ports:
        - 15433:80
    env_file:
        - .env
    depends_on:
        - postgres
    networks:
        - mybackend-network
    volumes:
        - ./pgadmin-data/:/var/lib/pgadmin/

networks:
  mybackend-network:
    driver: bridge

And my web project docker file looks like this:

# Get base DSK Image from Microsoft
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy the CSPROJ file and restore any dependencies (via NUGET)
COPY *.csproj ./
RUN dotnet restore

# Copy the project files and build our release
COPY . ./
RUN dotnet publish -c Release -o out

# Generate runtime image - do not include the whole SDK to save image space
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
EXPOSE 80
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyBackend.dll"]

And my connection string looks like this:

User ID =bootcampdb;Password=MySuperSecurePassword!;Server=postgres;Port=5432;Database=MyDatabase; Integrated Security=true;Pooling=true;

Currently I have two problems:

  1. I'm getting Npgsql.PostgresException (0x80004005): 57P03: the database system is starting up when I do docker-compose -up. I tried to add the healthcheck to my postgress db but that did not work. When I go to my Docker desktop app, and start my backend again, that message goes away and I get my second problem...

  2. Secondly after the DB started it's saying: FATAL: password authentication failed for user "username". It looks like it's not creating my user for the database. I even changed not to use .env files but have the value in my docker-compose file, but its still not working. I've tried to do docker-compose down -v to ensure my volumes gets deleted.

Sorry these might be silly questions, I'm still new to containerization and trying to get this to work.

Any help will be appreciated!

CodePudding user response:

Problem 1: Having depends_on only means that docker-compose will wait until your postgres container is started before it starts the web container. The postgres container needs some time to get ready to accept connections and if you attempt to connect before it's ready, you get the error you're seeing. You need to code your backend in a way that it'll wait until Postgres is ready by retrying the connection with a delay.

Problem 2: Postgres only creates the user and database if no database already exists. You probably have an existing database in ./db-data/ on the host. Try deleting ./db-data/ and Postgres should create the user and database using the environment variables you've set.

  • Related