Skip to content

Commit 714aecf

Browse files
authored
Prep only: .NET 10 update: Run in Docker Container (#35281)
Prep only for include file.
1 parent 73176f3 commit 714aecf

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

aspnetcore/host-and-deploy/docker/building-net-docker-images.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Windows Home Edition doesn't support Hyper-V, and Hyper-V is needed for Docker.
1818

1919
See [Containerize a .NET app with dotnet publish](/dotnet/core/docker/publish-as-container) for information on containerized a .NET app with `dotnet publish`.
2020

21+
[!INCLUDE[](~/host-and-deploy/docker/includes/building-net-docker-images10.md)]
2122
[!INCLUDE[](~/host-and-deploy/docker/includes/building-net-docker-images9.md)]
2223
[!INCLUDE[](~/host-and-deploy/docker/includes/building-net-docker-images8.md)]
2324
[!INCLUDE[](~/host-and-deploy/docker/includes/building-net-docker-images7.md)]
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
:::moniker range=">= aspnetcore-10.0"
2+
3+
## ASP.NET Core Docker images
4+
5+
For this tutorial, you download an ASP.NET Core sample app and run it in Docker containers. The sample works with both Linux and Windows containers.
6+
7+
The sample Dockerfile uses the [Docker multi-stage build feature](https://docs.docker.com/engine/userguide/eng-image/multistage-build/) to build and run in different containers. The build and run containers are created from images that are provided in Docker Hub by Microsoft:
8+
9+
* `dotnet/sdk`
10+
11+
The sample uses this image for building the app. The image contains the .NET SDK, which includes the Command Line Tools (CLI). The image is optimized for local development, debugging, and unit testing. The tools installed for development and compilation make the image relatively large.
12+
13+
* `dotnet/aspnet`
14+
15+
The sample uses this image for running the app. The image contains the ASP.NET Core runtime and libraries and is optimized for running apps in production. Designed for speed of deployment and app startup, the image is relatively small, so network performance from Docker Registry to Docker host is optimized. Only the binaries and content needed to run an app are copied to the container. The contents are ready to run, enabling the fastest time from `docker run` to app startup. Dynamic code compilation isn't needed in the Docker model.
16+
17+
## Prerequisites
18+
19+
* [.NET SDK 9.0](https://dotnet.microsoft.com/download)
20+
* Docker client 18.03 or later
21+
22+
* Linux distributions
23+
* [Debian](https://docs.docker.com/install/linux/docker-ce/debian/)
24+
* [Fedora](https://docs.docker.com/install/linux/docker-ce/fedora/)
25+
* [Ubuntu](https://docs.docker.com/install/linux/docker-ce/ubuntu/)
26+
* [macOS](https://docs.docker.com/desktop/mac/install/)
27+
* [Windows](https://docs.docker.com/desktop/windows/install/)
28+
29+
* [Git](https://git-scm.com/download)
30+
31+
## Download the sample app
32+
33+
* Download the sample by cloning the [.NET Docker repository](https://github.com/dotnet/dotnet-docker):
34+
35+
```console
36+
git clone https://github.com/dotnet/dotnet-docker
37+
```
38+
39+
## Run the app locally
40+
41+
* Navigate to the project folder at *dotnet-docker/samples/aspnetapp/aspnetapp*.
42+
43+
* Run the following command to build and run the app locally:
44+
45+
```dotnetcli
46+
dotnet run
47+
```
48+
49+
* Go to `http://localhost:<port>` in a browser to test the app.
50+
51+
* Press Ctrl+C at the command prompt to stop the app.
52+
53+
## Run in a Linux container or Windows container
54+
55+
* To run in a Linux container, right-click the System Tray's Docker client icon and select [switch to Linux containers](https://docs.docker.com/desktop/windows/#switch-between-windows-and-linux-containers).
56+
* To run in a Windows container, right-click the System Tray's Docker client icon and select [switch to Windows containers](https://docs.docker.com/desktop/windows/#switch-between-windows-and-linux-containers).
57+
58+
* Navigate to the Dockerfile folder at *dotnet-docker/samples/aspnetapp*.
59+
60+
* Run the following commands to build and run the sample in Docker:
61+
62+
```console
63+
docker build -t aspnetapp .
64+
docker run -it --rm -p <port>:8080 --name aspnetcore_sample aspnetapp
65+
```
66+
67+
The `build` command arguments:
68+
* Name the image aspnetapp.
69+
* Look for the Dockerfile in the current folder (the period at the end).
70+
71+
The run command arguments:
72+
* Allocate a pseudo-TTY and keep it open even if not attached. (Same effect as `--interactive --tty`.)
73+
* Automatically remove the container when it exits.
74+
* Map `<port>` on the local machine to port 8080 in the container.
75+
* Name the container aspnetcore_sample.
76+
* Specify the aspnetapp image.
77+
78+
* Go to `http://localhost:<port>` in a browser to test the app.
79+
80+
## Build and deploy manually
81+
82+
In some scenarios, you might want to deploy an app to a container by copying its assets that are needed at run time. This section shows how to deploy manually.
83+
84+
* Navigate to the project folder at *dotnet-docker/samples/aspnetapp/aspnetapp*.
85+
86+
* Run the [dotnet publish](/dotnet/core/tools/dotnet-publish) command:
87+
88+
```dotnetcli
89+
dotnet publish -c Release -o published
90+
```
91+
92+
The command arguments:
93+
* Build the app in release mode (the default is debug mode).
94+
* Create the assets in the *published* folder.
95+
96+
* Run the app.
97+
98+
* Windows:
99+
100+
```dotnetcli
101+
dotnet published\aspnetapp.dll
102+
```
103+
104+
* Linux:
105+
106+
```dotnetcli
107+
dotnet published/aspnetapp.dll
108+
```
109+
110+
* Browse to `http://localhost:<port>` to see the home page.
111+
112+
To use the manually published app within a Docker container, create a new *Dockerfile* and use the `docker build .` command to build an image.
113+
114+
```dockerfile
115+
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS runtime
116+
WORKDIR /app
117+
COPY published/ ./
118+
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
119+
```
120+
121+
To see the new image use the `docker images` command.
122+
123+
### The Dockerfile
124+
125+
Here's the *Dockerfile* used by the `docker build` command you ran earlier. It uses `dotnet publish` the same way you did in this section to build and deploy.
126+
127+
```dockerfile
128+
# https://hub.docker.com/_/microsoft-dotnet
129+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
130+
WORKDIR /source
131+
132+
# copy csproj and restore as distinct layers
133+
COPY *.sln .
134+
COPY aspnetapp/*.csproj ./aspnetapp/
135+
RUN dotnet restore
136+
137+
# copy everything else and build app
138+
COPY aspnetapp/. ./aspnetapp/
139+
WORKDIR /source/aspnetapp
140+
RUN dotnet publish -c release -o /app --no-restore
141+
142+
# final stage/image
143+
FROM mcr.microsoft.com/dotnet/aspnet:9.0
144+
WORKDIR /app
145+
COPY --from=build /app ./
146+
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
147+
```
148+
149+
In the preceding *Dockerfile*, the `*.csproj` files are copied and restored as distinct *layers*. When the `docker build` command builds an image, it uses a built-in cache. If the `*.csproj` files haven't changed since the `docker build` command last ran, the `dotnet restore` command doesn't need to run again. Instead, the built-in cache for the corresponding `dotnet restore` layer is reused. For more information, see [Best practices for writing Dockerfiles](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache).
150+
151+
## Additional resources
152+
153+
* [Containerize a .NET app with dotnet publish](/dotnet/core/docker/publish-as-container)
154+
* [Docker build command](https://docs.docker.com/engine/reference/commandline/build)
155+
* [Docker run command](https://docs.docker.com/engine/reference/commandline/run)
156+
* [ASP.NET Core Docker sample](https://github.com/dotnet/dotnet-docker) (The one used in this tutorial.)
157+
* [Configure ASP.NET Core to work with proxy servers and load balancers](xref:host-and-deploy/proxy-load-balancer)
158+
* [Working with Visual Studio Docker Tools](xref:host-and-deploy/docker/visual-studio-tools-for-docker)
159+
* [Debugging with Visual Studio Code](https://code.visualstudio.com/docs/nodejs/debugging-recipes#_debug-nodejs-in-docker-containers)
160+
* [GC using Docker and small containers](xref:performance/memory#sc)
161+
* [System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached](xref:host-and-deploy/docker/index#d128)
162+
* [Updates to Docker images](https://andrewlock.net/exploring-the-dotnet-8-preview-updates-to-docker-images-in-dotnet-8/)
163+
164+
## Next steps
165+
166+
The Git repository that contains the sample app also includes documentation. For an overview of the resources available in the repository, see [the README file](https://github.com/dotnet/dotnet-docker/blob/main/samples/aspnetapp/README.md). In particular, learn how to implement HTTPS:
167+
168+
> [!div class="nextstepaction"]
169+
> [Developing ASP.NET Core Applications with Docker over HTTPS](https://github.com/dotnet/dotnet-docker/blob/main/samples/run-aspnetcore-https-development.md)
170+
171+
:::moniker-end

aspnetcore/host-and-deploy/docker/includes/building-net-docker-images9.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:::moniker range=">= aspnetcore-9.0"
1+
:::moniker range="= aspnetcore-9.0"
22

33
## ASP.NET Core Docker images
44

0 commit comments

Comments
 (0)