1
---
2
name: containerize-aspnetcore
3
description: 'Containerize an ASP.NET Core project by creating Dockerfile and .dockerfile files customized for the project.'
4
---
5
6
# ASP.NET Core Docker Containerization Prompt
7
8
## Containerization Request
9
10
Containerize the ASP.NET Core (.NET) project specified in the settings below, focusing **exclusively** on changes required for the application to run in a Linux Docker container. Containerization should consider all settings specified here.
11
12
Abide by best practices for containerizing .NET Core applications, ensuring that the container is optimized for performance, security, and maintainability.
13
14
## Containerization Settings
15
16
This section of the prompt contains the specific settings and configurations required for containerizing the ASP.NET Core application. Prior to running this prompt, ensure that the settings are filled out with the necessary information. Note that in many cases, only the first few settings are required. Later settings can be left as defaults if they do not apply to the project being containerized.
17
18
Any settings that are not specified will be set to default values. The default values are provided in `[square brackets]`.
19
20
### Basic Project Information
21
1. Project to containerize:
22
- `[ProjectName (provide path to .csproj file)]`
23
24
2. .NET version to use:
25
- `[8.0 or 9.0 (Default 8.0)]`
26
27
3. Linux distribution to use:
28
- `[debian, alpine, ubuntu, chiseled, or Azure Linux (mariner) (Default debian)]`
29
30
4. Custom base image for the build stage of the Docker image ("None" to use standard Microsoft base image):
31
- `[Specify base image to use for build stage (Default None)]`
32
33
5. Custom base image for the run stage of the Docker image ("None" to use standard Microsoft base image):
34
- `[Specify base image to use for run stage (Default None)]`
35
36
### Container Configuration
37
1. Ports that must be exposed in the container image:
38
- Primary HTTP port: `[e.g., 8080]`
39
- Additional ports: `[List any additional ports, or "None"]`
40
41
2. User account the container should run as:
42
- `[User account, or default to "$APP_UID"]`
43
44
3. Application URL configuration:
45
- `[Specify ASPNETCORE_URLS, or default to "http://+:8080"]`
46
47
### Build configuration
48
1. Custom build steps that must be performed before building the container image:
49
- `[List any specific build steps, or "None"]`
50
51
2. Custom build steps that must be performed after building the container image:
52
- `[List any specific build steps, or "None"]`
53
54
3. NuGet package sources that must be configured:
55
- `[List any private NuGet feeds with authentication details, or "None"]`
56
57
### Dependencies
58
1. System packages that must be installed in the container image:
59
- `[Package names for the chosen Linux distribution, or "None"]`
60
61
2. Native libraries that must be copied to the container image:
62
- `[Library names and paths, or "None"]`
63
64
3. Additional .NET tools that must be installed:
65
- `[Tool names and versions, or "None"]`
66
67
### System Configuration
68
1. Environment variables that must be set in the container image:
69
- `[Variable names and values, or "Use defaults"]`
70
71
### File System
72
1. Files/directories that need to be copied to the container image:
73
- `[Paths relative to project root, or "None"]`
74
- Target location in container: `[Container paths, or "Not applicable"]`
75
76
2. Files/directories to exclude from containerization:
77
- `[Paths to exclude, or "None"]`
78
79
3. Volume mount points that should be configured:
80
- `[Volume paths for persistent data, or "None"]`
81
82
### .dockerignore Configuration
83
1. Patterns to include in the `.dockerignore` file (.dockerignore will already have common defaults; these are additional patterns):
84
- Additional patterns: `[List any additional patterns, or "None"]`
85
86
### Health Check Configuration
87
1. Health check endpoint:
88
- `[Health check URL path, or "None"]`
89
90
2. Health check interval and timeout:
91
- `[Interval and timeout values, or "Use defaults"]`
92
93
### Additional Instructions
94
1. Other instructions that must be followed to containerize the project:
95
- `[Specific requirements, or "None"]`
96
97
2. Known issues to address:
98
- `[Describe any known issues, or "None"]`
99
100
## Scope
101
102
- ✅ App configuration modification to ensure application settings and connection strings can be read from environment variables
103
- ✅ Dockerfile creation and configuration for an ASP.NET Core application
104
- ✅ Specifying multiple stages in the Dockerfile to build/publish the application and copy the output to the final image
105
- ✅ Configuration of Linux container platform compatibility (Alpine, Ubuntu, Chiseled, or Azure Linux (Mariner))
106
- ✅ Proper handling of dependencies (system packages, native libraries, additional tools)
107
- ❌ No infrastructure setup (assumed to be handled separately)
108
- ❌ No code changes beyond those required for containerization
109
110
## Execution Process
111
112
1. Review the containerization settings above to understand the containerization requirements
113
2. Create a `progress.md` file to track changes with check marks
114
3. Determine the .NET version from the project's .csproj file by checking the `TargetFramework` element
115
4. Select the appropriate Linux container image based on:
116
- The .NET version detected from the project
117
- The Linux distribution specified in containerization settings (Alpine, Ubuntu, Chiseled, or Azure Linux (Mariner))
118
- If the user does not request specific base images in the containerization settings, then the base images MUST be valid mcr.microsoft.com/dotnet images with a tag as shown in the example Dockerfile, below, or in documentation
119
- Official Microsoft .NET images for build and runtime stages:
120
- SDK image tags (for build stage): https://github.com/dotnet/dotnet-docker/blob/main/README.sdk.md
121
- ASP.NET Core runtime image tags: https://github.com/dotnet/dotnet-docker/blob/main/README.aspnet.md
122
- .NET runtime image tags: https://github.com/dotnet/dotnet-docker/blob/main/README.runtime.md
123
5. Create a Dockerfile in the root of the project directory to containerize the application
124
- The Dockerfile should use multiple stages:
125
- Build stage: Use a .NET SDK image to build the application
126
- Copy csproj file(s) first
127
- Copy NuGet.config if one exists and configure any private feeds
128
- Restore NuGet packages
129
- Then, copy the rest of the source code and build and publish the application to /app/publish
130
- Final stage: Use the selected .NET runtime image to run the application
131
- Set the working directory to /app
132
- Set the user as directed (by default, to a non-root user (e.g., `$APP_UID`))
133
- Unless directed otherwise in containerization settings, a new user does *not* need to be created. Use the `$APP_UID` variable to specify the user account.
134
- Copy the published output from the build stage to the final image
135
- Be sure to consider all requirements in the containerization settings:
136
- .NET version and Linux distribution
137
- Exposed ports
138
- User account for container
139
- ASPNETCORE_URLS configuration
140
- System package installation
141
- Native library dependencies
142
- Additional .NET tools
143
- Environment variables
144
- File/directory copying
145
- Volume mount points
146
- Health check configuration
147
6. Create a `.dockerignore` file in the root of the project directory to exclude unnecessary files from the Docker image. The `.dockerignore` file **MUST** include at least the following elements as well as additional patterns as specified in the containerization settings:
148
- bin/
149
- obj/
150
- .dockerignore
151
- Dockerfile
152
- .git/
153
- .github/
154
- .vs/
155
- .vscode/
156
- **/node_modules/
157
- *.user
158
- *.suo
159
- **/.DS_Store
160
- **/Thumbs.db
161
- Any additional patterns specified in the containerization settings
162
7. Configure health checks if specified in the containerization settings:
163
- Add HEALTHCHECK instruction to Dockerfile if health check endpoint is provided
164
- Use curl or wget to check the health endpoint
165
8. Mark tasks as completed: [ ] → [✓]
166
9. Continue until all tasks are complete and Docker build succeeds
167
168
## Build and Runtime Verification
169
170
Confirm that Docker build succeeds once the Dockerfile is completed. Use the following command to build the Docker image:
171
172
```bash
173
docker build -t aspnetcore-app:latest .
174
```
175
176
If the build fails, review the error messages and make necessary adjustments to the Dockerfile or project configuration. Report success/failure.
177
178
## Progress Tracking
179
180
Maintain a `progress.md` file with the following structure:
181
```markdown
182
# Containerization Progress
183
184
## Environment Detection
185
- [ ] .NET version detection (version: ___)
186
- [ ] Linux distribution selection (distribution: ___)
187
188
## Configuration Changes
189
- [ ] Application configuration verification for environment variable support
190
- [ ] NuGet package source configuration (if applicable)
191
192
## Containerization
193
- [ ] Dockerfile creation
194
- [ ] .dockerignore file creation
195
- [ ] Build stage created with SDK image
196
- [ ] csproj file(s) copied for package restore
197
- [ ] NuGet.config copied if applicable
198
- [ ] Runtime stage created with runtime image
199
- [ ] Non-root user configuration
200
- [ ] Dependency handling (system packages, native libraries, tools, etc.)
201
- [ ] Health check configuration (if applicable)
202
- [ ] Special requirements implementation
203
204
## Verification
205
- [ ] Review containerization settings and make sure that all requirements are met
206
- [ ] Docker build success
207
```
208
209
Do not pause for confirmation between steps. Continue methodically until the application has been containerized and Docker build succeeds.
210
211
**YOU ARE NOT DONE UNTIL ALL CHECKBOXES ARE MARKED!** This includes building the Docker image successfully and addressing any issues that arise during the build process.
212
213
## Example Dockerfile
214
215
An example Dockerfile for an ASP.NET Core (.NET) application using a Linux base image.
216
217
```dockerfile
218
# ============================================================
219
# Stage 1: Build and publish the application
220
# ============================================================
221
222
# Base Image - Select the appropriate .NET SDK version and Linux distribution
223
# Possible tags include:
224
# - 8.0-bookworm-slim (Debian 12)
225
# - 8.0-noble (Ubuntu 24.04)
226
# - 8.0-alpine (Alpine Linux)
227
# - 9.0-bookworm-slim (Debian 12)
228
# - 9.0-noble (Ubuntu 24.04)
229
# - 9.0-alpine (Alpine Linux)
230
# Uses the .NET SDK image for building the application
231
FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS build
232
ARG BUILD_CONFIGURATION=Release
233
234
WORKDIR /src
235
236
# Copy project files first for better caching
237
COPY ["YourProject/YourProject.csproj", "YourProject/"]
238
COPY ["YourOtherProject/YourOtherProject.csproj", "YourOtherProject/"]
239
240
# Copy NuGet configuration if it exists
241
COPY ["NuGet.config", "."]
242
243
# Restore NuGet packages
244
RUN dotnet restore "YourProject/YourProject.csproj"
245
246
# Copy source code
247
COPY . .
248
249
# Perform custom pre-build steps here, if needed
250
# RUN echo "Running pre-build steps..."
251
252
# Build and publish the application
253
WORKDIR "/src/YourProject"
254
RUN dotnet build "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/build
255
256
# Publish the application
257
RUN dotnet publish "YourProject.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
258
259
# Perform custom post-build steps here, if needed
260
# RUN echo "Running post-build steps..."
261
262
# ============================================================
263
# Stage 2: Final runtime image
264
# ============================================================
265
266
# Base Image - Select the appropriate .NET runtime version and Linux distribution
267
# Possible tags include:
268
# - 8.0-bookworm-slim (Debian 12)
269
# - 8.0-noble (Ubuntu 24.04)
270
# - 8.0-alpine (Alpine Linux)
271
# - 8.0-noble-chiseled (Ubuntu 24.04 Chiseled)
272
# - 8.0-azurelinux3.0 (Azure Linux)
273
# - 9.0-bookworm-slim (Debian 12)
274
# - 9.0-noble (Ubuntu 24.04)
275
# - 9.0-alpine (Alpine Linux)
276
# - 9.0-noble-chiseled (Ubuntu 24.04 Chiseled)
277
# - 9.0-azurelinux3.0 (Azure Linux)
278
# Uses the .NET runtime image for running the application
279
FROM mcr.microsoft.com/dotnet/aspnet:8.0-bookworm-slim AS final
280
281
# Install system packages if needed (uncomment and modify as needed)
282
# RUN apt-get update && apt-get install -y \
283
# curl \
284
# wget \
285
# ca-certificates \
286
# libgdiplus \
287
# && rm -rf /var/lib/apt/lists/*
288
289
# Install additional .NET tools if needed (uncomment and modify as needed)
290
# RUN dotnet tool install --global dotnet-ef --version 8.0.0
291
# ENV PATH="$PATH:/root/.dotnet/tools"
292
293
WORKDIR /app
294
295
# Copy published application from build stage
296
COPY --from=build /app/publish .
297
298
# Copy additional files if needed (uncomment and modify as needed)
299
# COPY ./config/appsettings.Production.json .
300
# COPY ./certificates/ ./certificates/
301
302
# Set environment variables
303
ENV ASPNETCORE_ENVIRONMENT=Production
304
ENV ASPNETCORE_URLS=http://+:8080
305
306
# Add custom environment variables if needed (uncomment and modify as needed)
307
# ENV CONNECTIONSTRINGS__DEFAULTCONNECTION="your-connection-string"
308
# ENV FEATURE_FLAG_ENABLED=true
309
310
# Configure SSL/TLS certificates if needed (uncomment and modify as needed)
311
# ENV ASPNETCORE_Kestrel__Certificates__Default__Path=/app/certificates/app.pfx
312
# ENV ASPNETCORE_Kestrel__Certificates__Default__Password=your_password
313
314
# Expose the port the application listens on
315
EXPOSE 8080
316
# EXPOSE 8081 # Uncomment if using HTTPS
317
318
# Install curl for health checks if not already present
319
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
320
321
# Configure health check
322
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
323
CMD curl -f http://localhost:8080/health || exit 1
324
325
# Create volumes for persistent data if needed (uncomment and modify as needed)
326
# VOLUME ["/app/data", "/app/logs"]
327
328
# Switch to non-root user for security
329
USER $APP_UID
330
331
# Set the entry point for the application
332
ENTRYPOINT ["dotnet", "YourProject.dll"]
333
```
334
335
## Adapting this Example
336
337
**Note:** Customize this template based on the specific requirements in containerization settings.
338
339
When adapting this example Dockerfile:
340
341
1. Replace `YourProject.csproj`, `YourProject.dll`, etc. with your actual project names
342
2. Adjust the .NET version and Linux distribution as needed
343
3. Modify the dependency installation steps based on your requirements and remove any unnecessary ones
344
4. Configure environment variables specific to your application
345
5. Add or remove stages as needed for your specific workflow
346
6. Update the health check endpoint to match your application's health check route
347
348
## Linux Distribution Variations
349
350
### Alpine Linux
351
For smaller image sizes, you can use Alpine Linux:
352
353
```dockerfile
354
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
355
# ... build steps ...
356
357
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final
358
# Install packages using apk
359
RUN apk update && apk add --no-cache curl ca-certificates
360
```
361
362
### Ubuntu Chiseled
363
For minimal attack surface, consider using chiseled images:
364
365
```dockerfile
366
FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled AS final
367
# Note: Chiseled images have minimal packages, so you may need to use a different base for additional dependencies
368
```
369
370
### Azure Linux (Mariner)
371
For Azure-optimized containers:
372
373
```dockerfile
374
FROM mcr.microsoft.com/dotnet/aspnet:8.0-azurelinux3.0 AS final
375
# Install packages using tdnf
376
RUN tdnf update -y && tdnf install -y curl ca-certificates && tdnf clean all
377
```
378
379
## Notes on Stage Naming
380
381
- The `AS stage-name` syntax gives each stage a name
382
- Use `--from=stage-name` to copy files from a previous stage
383
- You can have multiple intermediate stages that aren't used in the final image
384
- The `final` stage is the one that becomes the final container image
385
386
## Security Best Practices
387
388
- Always run as a non-root user in production
389
- Use specific image tags instead of `latest`
390
- Minimize the number of installed packages
391
- Keep base images updated
392
- Use multi-stage builds to exclude build dependencies from the final image
393