Skip to content

Commit ba48574

Browse files
committed
added docker support, docker-compose
1 parent 965769c commit ba48574

File tree

6 files changed

+81
-40
lines changed

6 files changed

+81
-40
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ This project is a boilerplate for building .NET API applications with various fe
6868
6969
```
7070

71+
1.1 Setting cert
72+
73+
```
74+
dotnet dev-certs https -ep ${HOME}/.aspnet/https/dotnetapi-boilerplate.pfx -p mypassword234
75+
dotnet dev-certs https --trust
76+
77+
```
78+
79+
1.2 Running the container with cert
80+
81+
```
82+
docker pull mcr.microsoft.com/dotnet/samples:aspnetapp
83+
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORTS=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="mypassword234" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/dotnetapi-boilerplate.pfx -v ${HOME}/.aspnet/https:/https/
84+
85+
```
86+
7187
2. The application will be available at `http://localhost:7000` and `https://localhost:7001` (or the configured URL).
7288
7389
### Health Check Endpoint

netapi-boilerplate.sln

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Use the official .NET SDK image to build the application
2+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
3+
WORKDIR /app
4+
5+
# Expose the port the application runs on
6+
# EXPOSE 8080
7+
# EXPOSE 8000
8+
# ENV ASPNETCORE_URLS=https://+;http://+
9+
# ENV ASPNETCORE_HTTPS_PORTS=8001
10+
# ENV ASPNETCORE_HTTP_PORTS=8000
11+
12+
ARG BUILD_CONFIGURATION=Release
13+
14+
# Copy the project files and restore dependencies
15+
COPY . ./
16+
RUN dotnet restore
17+
18+
# Build the application
19+
RUN dotnet publish -c Release -o out
20+
21+
# Use the official .NET runtime image to run the application
22+
FROM mcr.microsoft.com/dotnet/aspnet:9.0
23+
WORKDIR /app
24+
COPY --from=build /app/out .
25+
26+
27+
# Run the application
28+
ENTRYPOINT ["dotnet", "NetAPI.dll"]

src/Program.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,18 @@
77
using Microsoft.Extensions.Configuration;
88

99

10-
var builder = WebApplication
11-
.CreateBuilder(args)
12-
.ConfigureApplicationBuilder();
13-
14-
var IsDevelopment = builder.Environment.IsDevelopment();
15-
16-
var configuration = builder.Configuration;
17-
18-
var app = builder
19-
.Build()
20-
.ConfigureApplication();
21-
2210
try
2311
{
12+
var builder = WebApplication.CreateBuilder(args).ConfigureApplicationBuilder();
13+
14+
var app = builder.Build();
15+
16+
app.ConfigureApplication();
2417

25-
// Console.WriteLine(app.Environment.IsDevelopment().ToString());
26-
Log.Information($"Starting host in {builder.Environment.EnvironmentName} mode");
27-
// Console.WriteLine($"Running in Dev={isDev} mode");
18+
// Log.Information($"Application is running in {builder.Environment.EnvironmentName} mode.");
2819

2920
app.Run();
21+
3022
return 0;
3123
}
3224
catch (Exception ex)

src/Properties/launchSettings.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
"environmentVariables": {
1919
"ASPNETCORE_ENVIRONMENT": "Development"
2020
}
21+
},
22+
"Docker": {
23+
"commandName": "Docker",
24+
"launchBrowser": true,
25+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_HTTPS_PORTS": "8081",
28+
"ASPNETCORE_HTTP_PORTS": "8080"
29+
},
30+
"publishAllPorts": true,
31+
"useSSL": true
2132
}
2233
}
23-
}
34+
}

src/docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3.4'
2+
3+
services:
4+
dotnetapi-boilerplate:
5+
image: dotnetapi-boilerplate
6+
build:
7+
context: .
8+
dockerfile: Dockerfile
9+
ports:
10+
- "8080:8080"
11+
- "8081:8081"
12+
environment:
13+
- ASPNETCORE_ENVIRONMENT=Production
14+
- ASPNETCORE_URLS=https://+:8081;http://+:8080
15+
- ASPNETCORE_Kestrel__Certificates__Default__Password=mypassword234
16+
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/dotnetapi-boilerplate.pfx
17+
volumes:
18+
- ~/.aspnet/https:/https:ro

0 commit comments

Comments
 (0)