Install Docker on Linux and successfully deploy NET Core 2.0

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

Overview

A container, as the name implies, is a vessel used for storing and accommodating things;

and container technology with the rise of Docker is gradually reflected in the eyes of everyone, it is an abstract concept, but also silently exist in the world for many years of technology, not only to make the application of complete isolation, but also to share the underlying system resources while playing its greatest advantage. Compared to virtual machines, the same server can create twice times the instance, so that not only saves the system overhead, and the utilization and performance has been improved, why not. The most important point is also to help developers achieve the "build, run Everywhere" ideal!

So what about Docker?

    • Docker is a container engine developed and open source based on the Go language

    • Docker packs everything it needs to run the application into a separate container

    • Docker can automate and configure development/online environments to quickly build, test, and run complex multi-container applications

    • Docker can also be quickly scaled and provisioned for applications with thousands of nodes or containers

    • Can run on major Linux systems, Macs and windows, and ensure that wherever the software is deployed, it will work and get the same results

Introduction to Related concepts

    • Image mirroring and Container containers: You can interpret both as class and instance objects, or ISO system mirroring and virtual machine relationships. Different image packages contain different software or environments, but you can manage them using dockerfile (files created by the Docker-specific syntax rules). The container is a miniature system with image as a template, which can run independently, an image can create instances of multiple container containers

    • Registry:docker Hub Mirrored warehouse, providing everyone with a huge mirror resource to pull and use

    • Dockerfile: is a file that combines image commands for automatic construction of an image

Body

System environment

Host: Windows 10 Pro

Linux Server: Ubuntu 17.04 (Gnu/linux 4.10.0-28-generic x86_64), upgraded from version 16.10, managed by Hyper-V

Software Environment

Development tools: Visual Studio 15.3 (. NET Core 2.0)

Remote management tools: Putty, SSH-based, simple remote command control, save you from knocking the order wrong

SFTP client: WINSCP, handling file transfers between Linux and Windows

Container: Docker 17.06.0-ce

First, install Docker

A), set up a docker warehouse

1, according to the Convention, first update the Ubuntu package index

$ sudo apt-get update

2. Allow the server to update the warehouse using HTTPS

$ sudo apt-get install Apt-transport-https ca-certificates Curl Software-properties-common

3, add Docker official key pair, if no problem, will return "OK"

$ Curl-fssl HTTPS://DOWNLOAD.DOCKER.COM/LINUX/UBUNTU/GPG | sudo apt-key add-

4, check the key (9dc8 5822 9fc7 DD38 854A e2d8 8d81 803C 0EBF CD88) is consistent with the official

$ sudo apt-key fingerprint 0ebfcd88

5, set up the official stable version of the warehouse source

$ sudo add-apt-repository "Deb [ARCH=AMD64] Https://download.docker.com/linux/ubuntu $ (lsb_release-cs) stable"

b), installing Docker

1. Update the package index again, because the Docker repository has just been added to ensure that the Docker source is hit

$ sudo apt-get update

2. Install the latest version of the DOCKER-CE Community Edition

$ sudo apt-get install Docker-ce

3. If you need to install another version, use the following command

$ sudo apt-get install docker-ce=1.13

4. See if the installation is successful

$ docker Version

5, try to run a test image, the almighty Hello world!

$ sudo docker run Hello-world
Two. Docker configuration for Netcore

Preparation: First record several common commands that may be used next

sudo docker pull microsoft/dotnet//    pulls a mirror called "microsoft/dotnet" from the Docker repository sudo docker build light    // Build a container called light, sudo docker run Hello-world    //Run Hello-world container sudo docker images    //view all mirrors sudo docker rmi 63sf86332e    //delete a imageID image sudo Docker rmi $ (Docker images-q)    //Remove all mirrors sudo docker ps-a    //View all containers sudo Docker RM 6f0c67de4b72    //delete a Containerid container sudo docker rm $ (sudo docker ps-a-Q)   //Remove all containers

1, pull dotnet-sdk The latest image, slow words, please search for Daocloud Docker hub speed.

sudo docker pull Microsoft/dotnet:latest

Look at the mirror when you are done

sudo docker images

2. Go to this image, view the version no problem after creating a console program, and then run and view the results

sudo docker run-it  microsoft/dotnet//Each run will re-construct a new non-affected container, please view dotnet--versionmkdir TESTCD testdotnet New

dotnet New consoledotnet Run    //.netcore2.0 has turned dotnet restore into an implicit command that will be executed automatically when needed

Description: Use the Ctrl+d shortcut key or type the exit command to exit the command

Third, upgrade the project to. NetCore2.0, and released

1. Modify the project files for the class library and the Web, respectively, to. NET Core 2.0 and. NET Standard 2.0, including some components on nuget, or directly through the target framework, properties, right-click Project, At the same time modify the Program.cs and Startup.cs files to 2.0 default way, after the completion of their appearance is Jiangzi, is not very concise and clear.

Csproj project file for class library:

<project sdk= "MICROSOFT.NET.SDK" >  <PropertyGroup>    <TargetFramework>netstandard2.0< /targetframework>  </PropertyGroup></Project>

Csproj project file for Web

<project sdk= "Microsoft.NET.Sdk.Web" > <PropertyGroup> <targetframework>netcoreapp2.0</ Targetframework> <runtimeidentifiers>win10-x64;ubuntu.16.10-x64;ubuntu.16.04-x64</ Runtimeidentifiers>
   <assettargetfallback>$ (assettargetfallback);p ortable-net45+win8+wp8+wpa81;</assettargetfallback> </PropertyGroup> <ItemGroup> <packagereference include= " Microsoft.AspNetCore.All "version=" 2.0.0 "/> <packagereference include=" Microsoft.AspNetCore.Session "Version = "2.0.0"/> <packagereference include= "Microsoft.EntityFrameworkCore.SqlServer" version= "2.0.0"/> <pack Agereference include= "Microsoft.EntityFrameworkCore.Tools" version= "2.0.0"/>
<packagereference include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" version="2.0.0" privateassets="All"/> </ItemGroup> <ItemGroup> <dotnetclitoolreference include= " Microsoft.VisualStudio.Web.CodeGeneration.Tools "version=" 2.0.0 "/> </ItemGroup> <ItemGroup> < Projectreference include= ". \light.common\light.common.csproj "/> <projectreference include=". \light.model\light.model.csproj "/> </ItemGroup></Project>

A simple Program.cs

Using microsoft.aspnetcore;using microsoft.aspnetcore.hosting;namespace light.web{public    class program    { Public        static void Main (string[] args)        {            buildwebhost (args). Run ();        }        public static Iwebhost Buildwebhost (string[] args) =            Webhost.createdefaultbuilder (args)                . Usestartup<startup> ()                . Build ();}    }

Here are the related configuration items for my startup file

View Code

Microsoft Official Upgrade Wizard for your reference: https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/

2. Publish the project using CLI commands, you can publish directly without specifying the run-time identity, because the project is not running on the server (Windows or Linux).

dotnet publish-c Release-o. /publish

Iv. Deploying with Docker

1. Log on to the server using WINSCP, and upload the post-publish folder publish to the server

2. Create the Dockerfile file and configure it to the following content

From Microsoft/dotnetworkdir/appcopy./publish. entrypoint ["Dotnet", "Light.Web.dll"]

3. Switch to the directory where the Dockerfile is located, and execute the build command, the prompt after success

sudo docker build-t lightweb. Notice there's one more point behind.

4, run just build successfully, name is Lightweb container, directly bind the server's 80 port

sudo docker run-p 80:80-e "aspnetcore_urls=http://+:80" Lightweb

After successful operation (please temporarily ignore warning), indicating that the program in the container is using 80 port listening ....

5, try to use IP access, do not know their IP, please use the ifconfig command to view

At last

In the last picture of the time, in fact, his heart is still very excited; although some errors, such as the Mvcrazorcompileonpublish node in the project file, should be changed to false when publishing to Ubuntu, or deleted directly, Moreover, the name suffix of the picture is uppercase, but my program is written in lowercase, after publishing the picture is not displayed, because Linux is strictly case-sensitive. In addition to the reminder, when publishing to IIS, please update the Aspnetcoremodule module to the latest Dotnetcore.2.0.0-windowshosting.exe.

There is no introduction here. NetCore2.0 related knowledge, want to know can go to Zhang Brigade long summary:. NET Core 2.0 official Release information summary

Believe. NetCore2.0 's press conference brought about different changes in the community, the need to learn and accumulate more and more technology, or the old saying: learning.

Install Docker on Linux and successfully deploy NET Core 2.0

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.