Search code examples
.netpostgresqldockerdocker-composemicroservices

How should be docker-compose file for PostgreSQL and .Net Core in Docker Environment multi-container structure?


This is worked for me in docker-compose, one of the container is postgresqldb another one is .net core 3.1 container.

#docker-compose.yml
version: '3.4'

services:

    customerdb:
        image: postgres

    customer.api:
        image: ${DOCKER_REGISTRY-}customerapi
        build:
            context: .
            dockerfile: Customer/Customer.API/Dockerfile

docker-compose.override.yml

customerdb:
    container_name: customerdb
    image: postgres
    restart: always
    environment:
        POSTGRES_PASSWORD: postgres
        POSTGRES_DB: postgres
        POSTGRES_USER: postgres
    ports:
        - 5100:5432
    volumes:
        - ./postgres-data1:/var/lib/postgresql/data 

customer.api:
    container_name: customerapi
    environment:
        - ASPNETCORE_ENVIRONMENT=Development
        - "ConnectionStrings:CustomerConnection=host=host.docker.internal;port=5100;database=CustomerDB;username=postgres;password=postgres"
    depends_on:
        - customerdb
    volumes:
        - ${HOME}/.microsoft/usersecrets/:/root/.microsoft/usersecrets
        - ${HOME}/.aspnet/https:/root/.aspnet/https
    ports:
        - "8000:80"

I used ConnectionStrings:CustomerConnection=host=... because I used to app.config for connection string. But how to reach from customerapi to customerdb different connection string?


Solution

  • Use this dude:

    customerdb:
        container_name: customerdb
        image: postgres
        restart: always
        environment:
            POSTGRES_PASSWORD: postgres
            POSTGRES_DB: postgres
            POSTGRES_USER: postgres
        ports:
            - 5100:5432
        volumes:
            - ./postgres-data1:/var/lib/postgresql/data 
    
    customer.api:
        container_name: customerapi
        links:
            - "customerdb:database"
        environment:
            - ASPNETCORE_ENVIRONMENT=Development
            - "ConnectionStrings:CustomerConnection=host=customerdb;port=5432;database=CustomerDB;username=postgres;password=postgres"
        depends_on:
            - customerdb
        volumes:
            - ${HOME}/.microsoft/usersecrets/:/root/.microsoft/usersecrets
            - ${HOME}/.aspnet/https:/root/.aspnet/https
        ports:
            - "8000:80"