0

I'm trying to create a .Net and Postgresql's docker container, but it throws the error when I started to build:

8.578 System.ArgumentException: Host can't be null

This is how I settled the Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["file.csproj", "./"]
RUN dotnet restore "./file.csproj"
COPY . .
RUN dotnet tool install --global dotnet-ef --version 7.0.3
RUN /root/.dotnet/tools/dotnet-ef migrations add InitialCreate --project EnrollApp.csproj -v
RUN /root/.dotnet/tools/dotnet-ef database update

WORKDIR "/src/."
RUN dotnet build "file.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "file.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", ".dll"]

This is the docker-compose file

version: '3.4'

networks:
  dev:
    driver: bridge
services:
 db:
   image: postgres
   environment:
     POSTGRES_USER: postgres
     POSTGRES_DB: my_db
     POSTGRES_PASSWORD: example
   ports:
      - 5433:5432
   restart: always
   networks:
      - dev
   volumes:
     - postgres-data:/var/lib/postgresql/data
 adminer:
   image: adminer
   restart: always
   ports:
     - 8080:8080
   networks:
     - dev
 app:
   image: docker.io/library/appdemo
   build:
     context: .
     dockerfile: ./Dockerfile
   environment:
      - ConnectionStrings__DefaultConnection= User ID =postgres;Password=example;Server=db;Port=5432;Database=my_db; Integrated Security=true;Pooling=true;
      - ASPNETCORE_URLS=http://+:80
   ports:
     - 5000:80
   depends_on:
     - db
   networks:
      - dev
volumes:
 postgres-data:

And this is the ConnectionString configuration in Program.cs:

builder.Services.AddDbContext<DbContext>(o => {
     o.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));
    }
);

And finally, this is the connectionString in AppSettings.json

"ConnectionStrings": {
      "DefaultConnection": "Server=db;Port=5432;Database=my_db;Username=postgres;Password=example"
    }

1 Answer 1

-1

Specify network alias as connection-string host.

 db:
   image: postgres
   environment:
     POSTGRES_USER: postgres
     POSTGRES_DB: my_db
     POSTGRES_PASSWORD: example
   ports:
      - 5433:5432
   restart: always
   networks:
     dev:
     aliases:
       - postgresdb
   volumes:
     - postgres-data:/var/lib/postgresql/data

Then the connection-string is "Server=postgresdb;Port=5432;Database=my_db;Username=postgres;Password=example"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.