Running ASP. NET Core in the Docker container for Linux and Windows
Translator : In fact, I have been studying this part of the past week, the results of the weekend did not have time to summarize the article, Scott Hanselman. Then I will translate this article, let more Chinese readers see. Of course, I met Scott's pit.
But first, I'll explain the concepts of Linux containers and Windows containers for unfamiliar friends.
As containers become an unavoidable option for virtualization and application hosting, Windows has also started to provide container functionality to the public (in fact, Microsoft has and has been using container technology for a long time). Such container functionality is implemented in the internal engine in two ways, at the kernel level (Windows Server 2016 only) and the Hyper-V level (supported by Windows 10 and Windows Server 2016). But the external interface and standards follow Docker, and the advantage is that the Windows container ecosystem can be well integrated with the existing Linux container ecosystem. So the Windows container is called Docker on Windows. The advent of Windows containers opens New horizons for the container of traditional Windows server-side applications, such as placing qlik sense servers in containers to instantly orchestrate clusters.
Previously, Docker also launched a release version of Windows 10 that uses Hyper-V to run a Linux virtual machine as a container host. That's called Docker for Windows.
So on Windows 10, you can install both Docker for Windows and Docker on Windows, but you can't run both at the same time. The default installation directory is different, but the client (that is, Docker.exe) has the same name, so I changed the on version of the client execution file name can be used together. In fact, although the client version is different, but the basic command is still compatible, with one of them is enough. Because Docker Deamon is the same access address in both.
Scott Hanselman's article address is: http://www.hanselman.com/blog/ExploringASPNETCoreWithDockerInBothLinuxAndWindowsContainers.aspx
===== I am the delimiter that begins the text of the translation =====
In last May, the use of ASP. NET and Docker have hobbled things. But the great thing is that we've been making progress. I've written a blog post to show how to put the ASP. NET 5 (then called 5, now renamed Core 1.0) app to be published in Docker. Later, in November 2015, new tools like Docker Toolbox and kitematic made things easier. In May 2016 Docker for Windows Beta continues to make this thing a breeze.
So at this point in October 2016, let's look at how to use ASP. NET Core, Docker, and windows for development.
I installed the following things:
- Visual Studio Community 2015
- ASP. NET Core
- . NET Core 1.0.1-vs Tooling Preview 2
- Docker for Windows (I use beta channel)
- Visual Studio Tools for Docker
Docker for Windows is really good, it can automatically configure Hyper-V for you, create a Docker host OS, and start up this virtual machine. Save a lot of time.
This is my Linux host, and I don't need to worry too much about it. I'm going to do everything through the command line or Visual Studio.
First through File | New project to create an ASP. NET core application that runs in.
Then right-click on the project name and select Add | DockerSupport. This menu is from the visual Studio Tools for Docker extension. This step will add basic dockerfile and other docker-compose files. With this out-of-the-box step, I've done all the configuration to deploy the ASP. NET core application to the Docker Linux container.
ASP. NET core runs in a Docker Linux container
From my ASP. NET core application, we can see that the underlying image it uses (that is, the from statement in Dockerfile) is the ASP. NET core image of Linux.
From microsoft/aspnetcore:1.0.1
entrypoint ["Dotnet", "WebApplication4.dll"]
ARG source=.
Workdir/app
EXPOSE 80
COPY $source.
Next, because I don't want Docker to compile my application, I just want to publish it locally. You can read Steve Laske's blog post "Building Optimized Docker Images with ASP. NET Core" to learn how to build applications in one container and run them in other containers. This optimizes server utilization and resources.
I will publish, build, and run the image with the following command-line directives.
>dotnet Publish
>docker Images
REPOSITORY TAG IMAGE ID CREATED SIZE
Aspnetcoreonlinux latest dab2bff7e4a6 seconds ago 276.2 MB
Microsoft/aspnetcore 1.0.1 2e781d03cb22 hours ago 266.7 MB
>docker run-it-d-P 85:80 aspnetcoreonlinux
1cfcc8e8e7d4e6257995f8b64505ce25ae80e05fe1962d4312b2e2fe33420413
>docker PS
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cfcc8e8e7d4 aspnetcoreonlinux "dotnet webapplicatio" 2 seconds ago up 1 seconds 0.0.0.0:85->80/tcp Clever_archimedes
In this scenario, my ASP. NET core application can be run in Docker. We have now tried a Docker container, which is hosted in a Linux host that runs through Hyper-V in Windows.
What else can we do?
ASP. NET core runs in a Docker Windows container running Windows Nano Server
There's a Windows Server called Windows Server Corethat removes UI-related stuff; there's a Windows Serverthat's called Windows Nano Server, which lets windows shrink to only hundreds of m instead of a few grams. This means that from the point of view of required features and server usage, this is a very good choice, allowing you to deploy as little space as possible.
Let me see if I can run the ASP. NET core through the Kestrel "translator note: The Cross-platform Web server implementation of ASP. NET core" into Windows Nano server. Of course, because the nano is powerful, I can also run IIS in this container, which is documented here.
Michael Friis, from Docker, has a great blog post that describes building and running Docker apps in Windows Server containers. After installing the latest version of Docker for Windows, you can switch between Linux and Windows containers through the context menu.
So now I'm going to use Docker with Windows container. You may not know that you already have a Windows container! It has been released together with the Windows 10 anniversary version. You can enable the container feature in the Windows Properties dialog box:
I'll modify dockerfile to use Windows Nano Server mirroring. I can also use environment variables and expose commands inside Docker to control the ports that are exposed by ASP.
From Microsoft/dotnet:nanoserver
entrypoint ["Dotnet", "WebApplication4.dll"]
ARG source=.
Workdir/app
ENV Aspnetcore_urls http://+:82
EXPOSE 82
COPY $source.
Next, I publish and build ...
>dotnet Publish
Then, run it and map the Windows external ports inside the Windows container!
Note: When Windows 10 communicates with the container through "NAT" (Network address translation), there is a bug that you cannot access the container application directly through http://localhost:82 as you (i) wish. You have to access it through the IP of the container itself. Once I hear more about this bug and the situation is fixed, I will publish it here in time. It should appear within a few days via Windows Update. The way to get the IP address of the container from Docker is:Docker inspect-f "{{. NetworkSettings.Networks.nat.IPAddress}}" HASH
Then I will run my ASP. NET Core app in Windows Nano Server with the following command (again, it will run in the Nano server container in Windows 10).
>docker run-it-d-P 88:82 Aspnetcoreonnano
afafdbead8b04205841a81d974545f033dcc9ba7f761ff7e6cc0ec8f3ecce215
>docker inspect-f "{{. NetworkSettings.Networks.nat.IPAddress}}" AfA
172.16.240.197
Now, I can access this site through 172.16.240.197:82. Once the bug has been fixed, we'll be able to access it like any other container.
The most beautiful side of the Windows container is that it is very fast and lightweight. Once the image is downloaded and built on the machine, you can start and stop them all in seconds by Docker.
However, you can also use Docker to isolate Windows containers by using the following command:
Docker run--isolation=hyperv-it-d-P 86:82 Aspnetcoreonnano
As a result, the instance is completely isolated and running through Hyper-V itself. You get the best things in the world: speed, ease of deployment, and optional and convenient isolation.
ASP. NET core runs in a Docker Windows container running Windows Server Core
Next, I modified the dockerfile to use the full Windows Server core image. After downloading and installing this image, it takes about 8G of space and takes a little time to download and decompress, but it is really windows. You can also choose to run as a container or as an isolated hyper-V container.
Here, I use Windows Sever core that contains. NET core by modifying the FROM statement:
From MICROSOFT/DOTNET:1.0.0-PREVIEW2-WINDOWSSERVERCORE-SDK
entrypoint ["Dotnet", "WebApplication4.dll"]
ARG source=.
Workdir/app
ENV Aspnetcore_urls http://+:82
EXPOSE 82
COPY $source.
Note: I hear that a. NET core image using Windows Sever Core may be canceled. Because it makes sense to run. NET Core in Windows Nano Server or other lightweight mirrors. You should use the sever core for more cumbersome applications. If you really need to run the. NET core of Sever core, you can make your own dockerfile to easily build the image you want.
Next, I'll publish, build, and run again.
>docker Images
REPOSITORY TAG IMAGE ID CREATED SIZE
Aspnetcoreonnano Latest 7e02d6800acf minutes ago 1.113 GB
Aspnetcoreonservercore latest a11d9a9ba0c2 minutes ago 7.751 GB
Since the container can start and stop very quickly, I can run a complete Web cluster using Redis, a SQL container, and a third container that contains other parts of the container. or mix and match.
>docker PS
CONTAINER ID IMAGE COMMAND PORTS NAMES
D32a981ceabb aspnetcoreonwindows "dotnet webapplicatio" 0.0.0.0:87->82/tcp Compassionate_ Blackwell
A179a48ca9f6 Aspnetcoreonnano "dotnet webapplicatio" 0.0.0.0:86->82/tcp determined_ Stallman
170a8afa1b8b Aspnetcoreonnano "dotnet webapplicatio" 0.0.0.0:89->82/tcp agitated_ Northcutt
afafdbead8b0 Aspnetcoreonnano "dotnet webapplicatio" 0.0.0.0:88->82/tcp naughty_ Ramanujan
2cf45ea2f008 a7fa77b6f1d4 "dotnet webapplicatio" 0.0.0.0:97->82/tcp Sleepy_hodgkin
Summarize
Again, let's read another article in Michae, where he uses the Docker compose to run the ASP. SQL Express in a Windows container, and it runs in another, and Steve Lasker's blog post (in fact his entire article is a gold mine) mentions the creation of an optimized Docker image for ASP.
IMAGE ID respository TAG SIZE
0ec4274c5571 Web optimized 276.2 MB
F9f196304c95 Web single 583.8 MB
F450043e0a44 microsoft/aspnetcore 1.0.1 266.7 MB
706045865622 microsoft/aspnetcore-build 1.0.1 896.6 MB
Steve mentions a number of tricks that allow you to solve most of the problems with Docker and ASP.
All that means (IMHO), you can use ASP. NET Core in:
- Running ASP. NET Core in Linux
- Inside the Docker container
- In any cloud platform
- Running ASP. NET core in Windows, Windows Server, Server Core, and nano server
- Inside the Windows container of Docker
- Inside the Hyper-V quarantine container in Docker
This means that you can select any feature support and optimize the size for server usage and convenience. Once all the tools (Docker for Windows and visual Studio Docker tools) are ready, we can have a good debugging environment and support for workflow from development to production.
Are you developing with Docker, container, and ASP. Hope to see your voice in the comments.
Running ASP. NET Core in Docker container