"Step by Step" deploys dotnet core to Docker

Source: Internet
Author: User
Tags dotnet docker ps docker run

The premise of this tutorial is that you have already successfully installed Docker on a Linux server, and I will probably describe the Docker commands used in this process and will not introduce all of the Docker commands (because I won't).

First, run dotnet Core Hello World in Docker

Microsoft officially provides the Docker Image that has the environment required to run the dotnet core, and we can use the command directly:

Docker run-it Microsoft/dotnet:latest

which

-I: Indicates that the future mode starts Docker Container

-T: Represents a pseudo terminal assigned to the Docker container

Microsoft/dotnet: This is the official image name of the Docker core provided by Microsoft,

Latest: Indicates that we use this version of the image

With the above command, we created a Docker Container and started it.

In fact, Docker Run, he was divided by three steps away:

      • Check to see if there are any mirrors that we need to boot, and if not, try to go to the server to get
      • Create a container that corresponds to the Docker command:
Docker create-it Microsoft/dotnet:latest

If the creation succeeds, the ID of a container is output, and we can view it through Docker PS.

      • Launch container, corresponding Docker command
Docker start  -a-i container ID

At this point, we start the most basic container, and in the container, we can execute some dotnet core console commands:

[Email protected]:/# dotnet newcreated new C # projectinch/. [Email protected]:/#mkdirDocker//Create a folder named Docker[email protected]:/# CD docker/[email protected]:/docker# dotnet New//Create a dotnet core project created new C # Projectinch/Docker. [Email protected]:/docker# dotnet Restore//restore Dotnet Core Project Dependency package log:restoring packages for/docker/project.json...log:writing Lockfileto disk. Path:/docker/Project.lock.jsonlog:/docker/Project.jsonlog:Restore completedinch4658ms. [Email protected]:/docker# dotnet Run//start dotnet Core Project Docker (. Netcoreapp,version=v1.0) 'll be compiled because expected outputs is missingcompiling Docker for. Netcoreapp,version=v1.0compilation succeeded. 0Warning (s)0Error (s) time elapsedxx:xx:03.0599700Hello World! Output results

Second, deploy a WEB API project to Docker1. Creating an API

Let's use vs 2015 to create a WEBAPI project with the name: Jaxapi, and make the following adjustments to the default project:

A. All NuGet packages on which the upgrade project relies

Because there may be a slightly older VS version, he default reference package are RC version, do not upgrade may have some minor problems, I encountered in the first attempt to run in the local good, but in the Docker can not run up the problem.

If your vs default reference to the RC version, then you may encounter the upgrade after the project compilation problem, like a nuget bug, he changed the Project.json, my solution is, in frameworks, netcoreapp1.0 The following configuration is added under the node:

"Microsoft.NETCore.App": {      "version": "1.0.0",      "type": "Platform"    }

Then take out the references to Microsoft.NETCore.App in the dependencies.

This problem does not exist in the latest VS, but my company can not install the latest version for various reasons

B. Let Webapi not only listen to requests from localhost.

New API project By default he only listens for Web requests from localhost, and we need to add a configuration to the program class:

 Public classprogram{ Public Static voidMain (string[] args) {        varHost =NewWebhostbuilder (). Usekestrel (). Usecontentroot (Directory.GetCurrentDirectory ()). Useiisintegration (). Useurls ("http://*:9100")//Listen for 9100-port requests from all IPs. Usestartup<startup>()            .        Build (); Host.    Run (); }}
C. Publishing the Web API

Publish Webapi through the file system, there is no different from the general website, all the options default, followed by the VS step-to-step

It is best to leave a backdoor at the time of release, add a layer of folders between release and Publishoutput, and later tell you what to use, such as my publish directory:. \bin\release\docker\publishoutput

2. Writing Dockerfile

Open the directory you just published, create a new file with no suffix dockerfile, and sibling to the Publishoutput folder

Here, if we use the command line to paste the file to the server, the folder is more convenient than pasting a single file, and for my local testing, there is no need to toss back and forth compressed files. This folder level makes it easier for us to upload files ~

The Docker File contents are as follows, note that the port number should be the same as configured in our program above:

From microsoft//file/root/91009100/  tcp# Start the appentrypoint dotnet JaxApi.dll

Then put Dockerfile and publishoutput together upload to Linux server, specifically how to upload everyone eight Immortals crossing recount bar ~

3. Compile into image and run

On the Linux server, under the directory where the Dockerfile is located, run the command:

Docker build-t Jaxapi./

Docker build: Compile a Docker image based on Dockerfile

-T: Make a tag (tag) for the image, similar to a name

This way, our Docker image is compiled on our server, and we can launch our API for outside access.

9100:9100 --name jaxapi Jaxapi

-P: Mapping Docker container ports, 9100 ports in Docker mapped to 9100 ports on Linux

--name: Give my container an individual name, this can not add

This way, if you see the following in the console, you can prove that you have succeeded:

/Dockernow listening on:http://*:9100application started. Press CTRL + C to shut down.

Take a look at your linux server's IP, and then access your API through HTTP://IP:9100, the default API project only/api/values this URL can be accessed, the final effect is as follows:

If you encounter some problems during the reading process, please leave a message to discuss.

To be Continued ...

"Step by Step" deploys dotnet core to Docker

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.