Running the ASP. NET Core Web API application in Docker

Source: Internet
Author: User
Tags dotnet docker run microsoft website

This article is an article that guides you through a quick walkthrough, which describes the basic steps of running an ASP. NET Core Web API application in Docker, as well as some simple descriptions of the use of Docker during the introduction process. For the basic concepts of. NET core and Docker, many articles have been introduced on the web, so this article will not be covered in detail. For friends who are not familiar with. NET Core and Docker, it is recommended that you first review the documentation related to these technologies before you read this article.

Prerequisite

To complete the walkthrough tasks described in this article, you need to prepare the following environments:

    • Visual Studio 2015, or Visual Studio Code, is used to create Web API applications for ASP. This article uses Visual Studio 2015
    • If you are using Visual Studio 2015, you will need to install. NET Core 1.0.0–vs Tooling Preview 2. Please click here to install
    • Microsoft. NET Core SDK, can download and install to Microsoft Official website
    • A machine equipped with Ubuntu 14.04.4 LTS, which can be a virtual machine. Install the Docker CLI in Ubuntu. You can follow the steps on the Docker website to install
    • If you want to use Docker under Windows, make sure that your Windows machine meets the following requirements:
      • 64-bit Windows Pro, enterprise or education version (build 10586 or later, need to install 1511 November update)
      • Enable Hyper-V in the system. If not enabled, Docker for Windows automatically enables Hyper-V during installation (this process requires a system restart)

I'll cover the contents of Docker for Windows in another article.

Walkthrough steps to create an ASP. NET core Web API application using Visual Studio 2015

With Visual Studio 2015 open, in the dialog box for the new project, select the ASP. NET Core WEB application (. NET core) project template, name the project DOCKERWEBAPI, and then click the OK button.

In the new ASP. NET Core Web Application dialog box, select the Web API and click the OK button.

After the project is successfully created, you can see the following project structure in the solution browser:

There is also no too much introduction to the application Architecture of ASP. Next, we modify the auto-generated valuescontroller so that the Get method returns the machine name of the current machine, as follows:

Open the Program.cs file, and in the build Webhostbuilder coherent method chain, join the Useurls call so our application can accept requests from any IP. This allows the application to accept HTTP requests in the Docker container as well:

OK, after compiling, run the application at the command line using dotnet run:

Open your browser and test our app to run successfully:

Compiling 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 use the dotnet Publish command to publish the application. This will copy the compiled DLL, together with the dependencies, to the Bin/debug/netcoreapp1.0/publish directory. At this point, use winscp similar software to copy all the contents of the directory to the Ubuntu machine (and of course, if you are using Docker under Windows 10, you do not need this copy step). After the copy is complete, you can see all of these files in the Ubuntu system:

By the way, we test this app in Ubuntu to see if the ASP. NET Core Web API has the same user experience as Windows in Linux systems. At this point, run the dotnet DockerWebAPI.dll command in this directory (to run the dotnet command, you will need to install the. NET Core CLI in Ubuntu, or you can download the installation to the official Microsoft website, but this step is not necessarily required for this walkthrough) , you can see the following interface:

Test with the Curl command:

The test was successful.

Next, in this package contains DockerWebAPI.dll file directory, create a new Dockerfile file, using Vim or Nano and other text editor, enter the following (#开头的行为注释行, can not input):

# build Docker Imagefrom microsoft/dotnet:latest# into Docker's/USR/LOCAL/SRC directory based on Microsoft/dotnet:latest run cd/usr/local/ src# Create dockerwebapi directory run mkdir dockerwebapi# set working path workdir/usr/local/src/dockerwebapi# copy all files under the current folder to the working directory copy *. * ./# exposing 5000 ports to the outside world expose 5000# execute dotnet DockerWebAPI.dll command cmd ["dotnet", "DockerWebAPI.dll"]

For a dockerfile definition, please refer to the official Docker documentation.

Still under the current directory, execute the following command to create the Docker Image:

Docker build-t Daxnet/docker-webapi.

Note that you must not omit the last dot, which represents the dockerfile of the current path to generate the Docker Image. If this is the first generation, because you need to download microsoft/dotnet:latest This Docker image as the base image, it will be slow (domestic speed reasons, you understand). If successful, you can generally see the following screen:

Running the ASP. NET Core Web API application in Docker container

Now we can use Docker run to execute the Docker image that was just generated. Docker run loads the Docker image into Docker container and executes the command specified by Dockerfile (that is, the dotnet DockerWebAPI.dll command). The commands for Docker run are as follows:

Docker Run-it-p 8080:5000 Daxnet/docker-webapi

In this command:

    • The-it parameter indicates the need to provide a simulated shell environment and requires user interaction so that when we press CTRL + C, we can stop our application
    • The-p 8,080:5,000 parameter indicates that the 5000 port of Docker container needs to be mapped to the 8080 port of the host environment, that is, the client can access our application directly via 8080 port
    • The DAXNET/DOCKER-WEBAPI parameter specifies the Docker Image that needs to be run. The default latest label is used here

After running, you can see the following interface:

Let's test the use of the Curl command in Linux to invoke our service, noting that the port number needs to be changed from 5000 to 8080 compared to the previous test:

Of course, it can also be called in the browser of another machine:

In fact, the machine name returned is already the ID value of the Docker container:

Summarize

This article describes how to create an ASP. NET Core WEBAPI application using Visual Studio 2015 and how to run the application in Docker by quickly describing the individual steps. Some places may not be described very clearly, if in doubt welcome message discussion. Strive to be able to share more relevant hands-on experience with Visual Studio 2015, Docker, and. NET Core.

Running the ASP. NET Core Web API application in Docker

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.