diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8a75570 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +WORKDIR /source + +# copy csproj and restore as distinct layers +COPY *.sln . +COPY DotnetApiPostgres.Api/*.csproj ./DotnetApiPostgres.Api/ +RUN dotnet restore + +# copy everything else and build app +COPY DotnetApiPostgres.Api/. ./DotnetApiPostgres.Api/ +WORKDIR /source/DotnetApiPostgres.Api +RUN dotnet publish -c release -o /app + +# final stage/image +FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final +WORKDIR /app +COPY --from=build /app ./ + +ENTRYPOINT ["dotnet", "DotnetApiPostgres.Api.dll"] \ No newline at end of file diff --git a/DotnetApiPostgres.Api/Extensions/DbInitializer.cs b/DotnetApiPostgres.Api/Extensions/DbInitializer.cs index 0b4f990..0136d83 100644 --- a/DotnetApiPostgres.Api/Extensions/DbInitializer.cs +++ b/DotnetApiPostgres.Api/Extensions/DbInitializer.cs @@ -7,6 +7,7 @@ public static class DbInitializer { public static async Task InitializedAsync(this WebApplication app) { + Console.WriteLine("=====> Looking to seed data"); using var scope = app.Services.CreateScope(); var services = scope.ServiceProvider; try @@ -16,7 +17,7 @@ public static async Task InitializedAsync(this WebApplication app) } catch (Exception ex) { - Console.WriteLine($"An error occurred during database initialization: {ex.Message}"); + Console.WriteLine($"===> An error occurred during database initialization: {ex.Message}"); } } public static async Task SeedDataAsync(ApplicationDbContext context) diff --git a/DotnetApiPostgres.Api/appsettings.json b/DotnetApiPostgres.Api/appsettings.json index c80ea6f..4688815 100644 --- a/DotnetApiPostgres.Api/appsettings.json +++ b/DotnetApiPostgres.Api/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "default": "Host=localhost;Port=5432;Database=PersonDb;Username=postgres;Password=p@55w0rd" + "default": "Host=db;Port=5432;Database=PersonDb;Username=postgres;Password=p@55w0rd" } } \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..927a55e --- /dev/null +++ b/compose.yaml @@ -0,0 +1,28 @@ +services: + web_api: + container_name: person_api_app + build: . + image: people-api:1.0.0 + ports: + - 8080:8080 + depends_on: + db: + condition: service_healthy + restart: true + db: + image: postgres + container_name: postgres_db + ports: + - 5432:5432 + environment: + POSTGRES_PASSWORD: p@55w0rd + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U postgres" ] + interval: 10s + retries: 5 + start_period: 30s + timeout: 10s +volumes: + postgres_data: