Dockerize a .NET Core application
Dockerize a .NET Core application
Introduction
This example demonstrates how to dockerize an ASP.NET Core application.
Why build ASP.NET Core?
- Open-source
- Develop and run your ASP.NET Core apps cross-platform on Windows, MacOS and Linux
- Great for modern cloud-based apps, such as web apps, IoT apps and mobile backends
- ASP.NET Core apps can run on .NET Core or on the full .NET Framework
- Designed to provide an optimized development framework for apps that are deployed to the cloud or run on-premise
- Modular components with minimal overhead retain flexibility while constructing your solutions
Prerequisites
This example assumes you already have an ASP.NET Core app on your machine. If you are new to ASP.NET you can follow a simple tutorial to initialize a project or clone our ASP.NET Docker Sample.
Create a Dockerfile for an ASP.NET Core application
- Create a
Dockerfile
in your project folder. - Add the text below to your
Dockerfile
for either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker for Windows. Read more on switching containers. - The
Dockerfile
assumes that your application is calledaspnetapp
. Change theDockerfile
to use the DLL file of your project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | FROM microsoft/aspnetcore-build: 2 . 0 AS build-env WORKDIR /app # Copy csproj and restore as distinct layers COPY *.csproj ./ RUN dotnet restore # Copy everything else and build COPY . ./ RUN dotnet publish -c Release -o out # Build runtime image FROM microsoft/aspnetcore: 2 . 0 WORKDIR /app COPY --from=build-env /app/out . ENTRYPOINT [ "dotnet" , "aspnetapp.dll" ] |
To make your build context as small as possible add a .dockerignore
file to your project folder and copy the following into it.
1 2 | bin\ obj\ |
Build and run the Docker image
- Open a command prompt and navigate to your project folder.
- Use the following commands to build and run your Docker image:
1 2 | $ docker build -t aspnetapp . $ docker run -d -p 8080 : 80 --name myapp aspnetapp |
View the web page running from a container
- Go to localhost:8080 to access your app in a web browser.
- If you are using the Nano Windows Container and have not updated to the Windows Creator Update there is a bug affecting how Windows 10 talks to Containers via “NAT” (Network Address Translation). You must hit the IP of the container directly. You can get the IP address of your container with the following steps:
- Run
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" myapp
- Copy the container ip address and paste into your browser. (For example,
172.16.240.197
)
- Run
Further reading
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?