Run ASP. NET Core Web API application and dockerapi in docker
This article describes how to run an ASP program in docker. the basic steps of the NET Core Web API application are described in brief. Many articles on the Internet have introduced the basic concepts of. NET Core and docker. Therefore, this article will not detail these contents. If you are not familiar with. NET Core and docker, we recommend that you first read the documentation related to these technologies and then read this article.
Prerequisites
To complete the drill tasks described in this article, you need to prepare the following environment:
- Visual Studio 2015, or Visual Studio Code, is used to create a Web API application for ASP. NET Core. This document uses Visual Studio 2015
- If Visual Studio 2015 is used, install. NET Core 1.0.0-VS 2015 Tooling Preview 2. Click here to install
- Microsoft. NET Core SDK, which can be downloaded and installed on the official Microsoft website
- A machine with Ubuntu 14.04.4 LTS installed can be a virtual machine. Install docker CLI In Ubuntu. You can follow the steps on the docker website to install
- If you want to use docker in Windows, make sure that the Windows Server meets the following requirements:
- 64-bit Windows 10 Pro, Enterprise, or Education version (for Build 10586 or later versions, install 1511 November update)
- Enable Hyper-V in the system. If it is not enabled, Docker for Windows will automatically enable Hyper-V during installation (the system needs to be restarted during this process)
I will introduce Docker for Windows in another article.
Procedure: Use Visual Studio 2015 to create an ASP. NET Core Web API application
Open Visual Studio 2015 and select in the new project dialog box. ASP. NET Core Web Application (.. NET Core) Project template, name the project DockerWebAPI, and click OK.
In the New ASP. NET Core Web Application (. NET Core) dialog box, select Web API and click OK.
After the project is successfully created, you can see the following project structure in the solution Browser:
I will not introduce the application structure of ASP. NET Core too much here. Next, modify the automatically generated ValuesController so that the GET method returns the machine name of the current machine, as shown below:
Open the Program. cs file and add UseUrls to construct the WebHostBuilder coherent method chain so that our application can accept requests from any IP address. This allows the application to accept HTTP requests in the docker container:
OK. After compilation, run the application using dotnet run on the command line:
Open the browser and test our application. The operation is successful:
Compile an ASP. NET Core Web API application into a Docker Image
First, go to the project root directory (that is, the directory containing the project. json file) and run the dotnet publish command to publish the application. This will copy the compiled DLL along with the dependencies to the bin/Debug/netcoreapp1.0/publish directory. In this case, use software similar to WinSCP to copy all the content in this directory to the Ubuntu machine (if Docker is used in Windows 10, this copy step is not required ). After copying, you can view all these files in the Ubuntu system:
Let's test this application in Ubuntu to see if ASP. NET Core Web API can share the same user experience in Linux and Windows. Run dotnet DockerWebAPI in this directory. dll command (to run the dotnet command, you need to install it in Ubuntu. NET Core CLI can also be downloaded and installed on the official Microsoft website, but this step is not required for the drills in this article), you can see the following interface:
Run the curl command to test:
The test is successful.
Next, there is a DockerWebAPI. create a new Dockerfile in the directory of the dll file. Use a text editor such as vim or nano and enter the following content (# The behavior comment line starting with can be left blank ):
# Building Docker ImageFROM microsoft/dotnet based on microsoft/dotnet: latest: latest # enter the/usr/local/src directory in docker RUN cd/usr/local/src # create the DockerWebAPI directory RUN mkdir DockerWebAPI # Set the working path WORKDIR/usr/local/src/DockerWebAPI # COPY all files in the current folder to the working directory *. *. /# EXPOSE port 5000 EXPOSE 5000 to the outside world # execute dotnet DockerWebAPI. dll command CMD ["dotnet", "DockerWebAPI. dll "]
We will not discuss the definition of Dockerfile for the moment. Please refer to the official Docker documentation.
Still in the current directory, execute the following command to create a Docker Image:
docker build -t daxnet/docker-webapi .
Note: Do not omit the last vertex. It indicates that the Docker Image is generated using the Dockerfile of the current path. If this is the first generation, the Docker Image, microsoft/dotnet: latest, needs to be downloaded as the basic Image, so it will be slow (because of domestic network speed, you know ). If it succeeds, you can see the following picture:
Run ASP. NET Core Web API application in Docker Container
Now, we can use docker run to execute the generated Docker Image. Docker run loads the Docker Image to the Docker iner and then runs the command specified by Dockerfile (that is, the dotnet DockerWebAPI. dll command ). The docker run Command is as follows:
docker run -it -p 8080:5000 daxnet/docker-webapi
In this command:
- -The it parameter indicates that a simulated shell environment needs to be provided and user interaction is required. In this way, when we press Ctrl + C, we can stop our applications.
- -P 8080: 5000 indicates that port 5000 of Docker Container needs to be mapped to port 8080 of the host environment, that is, the client can directly access our application through port 8080.
- The daxnet/docker-webapi parameter specifies the Docker Image to be run. The default latest tag is used here.
After running, you can see the following interface:
Let's test it. Use the curl command in Linux to call our service. Note that, compared with the previous test, the port number needs to be changed from 5000 to 8080:
Of course, you can also call the following in the browser of another machine:
In fact, the returned machine name is already the Docker container ID value:
Summary
This document describes how to use Visual Studio 2015 to create an ASP. NET Core WebAPI application and run the application in Docker. In some cases, the description may not be very clear. If you have any questions, please leave a message for discussion. In the future, we will strive to share more practical experience on Visual Studio 2015, Docker,. NET Core, and other related aspects.
Reference page: http://qingqingquege.cnblogs.com/p/5933752.html