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"
}