Skip to content

Commit 07f13e3

Browse files
committed
feat: add multi-stage Dockerfile for building and running app
1 parent 665cca9 commit 07f13e3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Dockerfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# - Stage 1 --------------------------------------------------------------------
2+
3+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
4+
WORKDIR /src
5+
6+
# Copy and restore dependencies
7+
COPY src/Dotnet.Samples.AspNetCore.WebApi/*.csproj ./Dotnet.Samples.AspNetCore.WebApi/
8+
RUN dotnet restore ./Dotnet.Samples.AspNetCore.WebApi
9+
10+
# Copy source and publish
11+
COPY src/Dotnet.Samples.AspNetCore.WebApi ./Dotnet.Samples.AspNetCore.WebApi
12+
WORKDIR /src/Dotnet.Samples.AspNetCore.WebApi
13+
RUN dotnet publish -c Release -o /app/publish
14+
15+
# - Stage 2 --------------------------------------------------------------------
16+
17+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
18+
WORKDIR /app
19+
20+
# Copy published output
21+
# Note: This includes the SQLite database because it's marked as <Content> with
22+
# <CopyToOutputDirectory> in the .csproj file. No need to copy it manually.
23+
COPY --from=build /app/publish .
24+
25+
# Add non-root user (aspnetcore) for security hardening
26+
RUN adduser --disabled-password --gecos '' aspnetcore \
27+
&& chown -R aspnetcore:aspnetcore /app
28+
USER aspnetcore
29+
30+
# Set environment variables
31+
ENV ASPNETCORE_URLS=http://+:9000
32+
ENV ASPNETCORE_ENVIRONMENT=Production
33+
34+
# Default entrypoint
35+
ENTRYPOINT ["dotnet", "Dotnet.Samples.AspNetCore.WebApi.dll"]

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,29 @@ _Figure: Simplified, conceptual project structure and main application flow. Not
2626
dotnet watch run --project src/Dotnet.Samples.AspNetCore.WebApi/Dotnet.Samples.AspNetCore.WebApi.csproj
2727
```
2828

29-
## Documentation
29+
## Documentation (Development-only)
3030

3131
```console
3232
https://localhost:9000/swagger/index.html
3333
```
3434

3535
![API Documentation](/assets/images/Swagger.png)
3636

37+
## Container
38+
39+
This project includes a multi-stage `Dockerfile` for local development and production builds.
40+
41+
### Build the image
42+
43+
```bash
44+
docker build -t aspnetcore-app .
45+
```
46+
47+
### Run the container
48+
49+
```bash
50+
docker run -p 9000:9000 aspnetcore-app
51+
3752
## Credits
3853

3954
The solution has been coded using [Visual Studio Code](https://code.visualstudio.com/) with the [C# Dev Kit](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) extension.

0 commit comments

Comments
 (0)