Run the ASPDOTNETCOREMVC program on Docker-part1

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

receive theThe first step in learning Docker technology based on ASP: Installing the Docker platform in CentOS7"This blog post, after the completion of the Docker platform, you can start to let the Aspdotnetcore program run on the Docker platform.

1. Prepare a ASPDOTNETCOREMVC program on the development machine

The available sample code can be downloaded from here

Https://github.com/shenba2014/AspDotNetCoreMvcDocker

(You need to install. NET Core2.0 and Bower in advance, install Bower with NPM after installing node. js)

After you download the code, navigate to the project's root directory at the command line, and then run the following command

dotnet Restore

Bower Install

Dotnet Run

Then the browser input http://localhost:5000 can see the following interface

This sample code is created based on the template of dotnet new MVC, and then it is done with some refinement, and the process is not described in detail.

2. Create a docker image that can run the Aspdotnecore program

Previously, the Docker environment was built on the server, but the. NET core was not installed to demonstrate running. NET core programs under the host machine just through the Docker platform.

The Docker platform is not aware of. NET core programs, even if you say you are cross-platform. Docker only knows the mirrors and containers.

mirroring and containers are our starting points, and we need to get a mirror that contains a running. NET core environment, and then create a container based on that image and then put our program in this container to run, presumably that's the idea. As for the concept of mirrors and containers, it is simple to say that one is a template, one is an instance, not a detailed said.

We don't have to worry about where to get a usable image,. NET Core has prepared this image for us, and can search for a Microsoft-provided image by using the following command (Docker commands to go to server execution, if development and server are on a machine, please ignore)

Docker Search Microsoft

Get a list of the images in Microsoft name, what we need here is aspnetcore mirror, other self-study.

Microsoft/aspnetcore official images for running compiled ASP. N . ...

The description is official, so you can use it with confidence.

So why was it created, because the Aspnetcore image file provided by Microsoft contains only the basic environment that is running, we need to create a mirror that contains our website program based on this image. It does not seem like the usual way of thinking, the usual practice is to copy the deployed program to the operating environment. We're doing this in Docker, and with a custom image, we can create any number of container samples, which eliminates duplicate copies, which is one of the core competencies of Docker.

Then start creating this custom image.

A. Create a dockerfile in the root of the website program, no suffix, enter the following content

From microsoft/aspnetcore:2.0.0

COPY Dist/app

Workdir/app

EXPOSE 80/tcp

entrypoint ["Dotnet", "AspDotNetCoreMvcDocker.dll"]

This is a YAML-formatted file that provides some parameters for creating the image

From

Based on which image is created, the 2.0 version of Aspnetcore is specified here, and if the version number is not set, the latest version is obtained.

Detailed versions are available for viewing

https://hub.docker.com/r/microsoft/aspnetcore/tags/

COPY

It can be understood that from the native copy content to the image, this setting is copied from the Native Dist directory (release build directory) to the mirrored/app directory

Workdir

Working directory

EXPOSE

Exposed port of the container

EntryPoint

is obviously a portal command, which is set to execute the dotnet command, run the site, this command is executed when the container is started

B. Generating a Web site program

is to compile the website program, generate a release version of the output

Or at the site root, execute the following command

dotnet Publish--framework netcoreapp2.0--configuration Release--output Dist

A dist directory will be generated under the root directory of the site program

C. Creating a mirror

Before you execute the CREATE command, you need to confirm that the Dockerfile and Dist folders are in the same directory, which saves you from specifying a path.

If the local development environment and the server where Docker resides are not on a single machine, you need to copy the Dockerfile and dist files from the previous step to the server.

Then execute the following command at the command line where the Dockerfile is located

Docker build. -T Shenba/aspdotnetcoremvc-f Dockerfile

- T is the label name of the target image and can be set to another name, usually in the form of Docker username/Mirror Name

The Docker username is the registered user name at the Docker site (the subsequent release image is used by the Docker hub)

- F is the configuration file that contains the create image, which is the dockerfile we created

The output after running the above command is as follows:

Sending build context to Docker daemon 4.36MB

Step 1/5: From microsoft/aspnetcore:2.0.0

2.0.0:pulling from Microsoft/aspnetcore

219d2e45b4af:pull Complete

76809febc514:pull Complete

96ef109a5ac7:pull Complete

78a255fc90a4:pull Complete

8865b55bda8e:pull Complete

Digest:sha256:0e9274ca36d13425bf0cc558ceaed355ceffd74814d9a14a679f917cc30ddc15

status:downloaded newer image for microsoft/aspnetcore:2.0.0

---> 4f34e60fe340

Step 2/5: COPY Dist/app

---> 70910ea10aa0

Step 3/5: Workdir/app

---> 23344526b7ef

Removing intermediate container 158997ED60BD

Step 4/5: EXPOSE 80/tcp

---> Running in 43bd0930495b

---> d0e0b20f93cc

Removing intermediate container 43bd0930495b

Step 5/5: entrypoint dotnet AspDotNetCoreMvcDocker.dll

---> Running in b3767135fb4c

---> 407471ef91f2

Removing intermediate container b3767135fb4c

Successfully built 407471EF91F2

Successfully tagged Shenba/aspdotnetcoremvc:latest

You can see that the first step is to download the microsoft/aspnetcore:2.0.0 to local, then execute each line of the Dockerfile command in turn

Run the Docker images command to see two new images

REPOSITORY TAG IMAGE ID CREATED SIZE

Microsoft/aspnetcore 2.0.0 4f34e60fe340 less than a second ago 280MB

SHENBA/ASPDOTNETCOREMVC latest 407471ef91f2 2 minutes ago 284MB

One is Microsoft/aspnetcore mirror, one is our custom image

D. Creating and running a container

There is a custom image here already, that is, the template already has, then the creation of the container is simple.

Run the following command on the server

Docker create-p 3000:80--name App1 SHENBA/ASPDOTNETCOREMVC

- P 3,000:80 Specifies the host's 3000 port and the container's 80 port to do the mapping, then the external Access 3000 port can access to the container 80 port

--name App1 Name

The last one is the name of the mirror, which is our custom image name

Create a container for another port, change the port and name.

Docker create-p 4000:80--name app2 SHENBA/ASPDOTNETCOREMVC

When you run Docker ps-a , you can see what containers have been created.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

104FB49877CF shenba/aspdotnetcoremvc "dotnet aspdotnetc ..." 3 seconds ago Created app2

dfc780c6511c shenba/aspdotnetcoremvc "dotnet aspdotnetc ..." One seconds ago Created App1

Next Run the container App1

Docker Start App1

so app1 this site should run up, and then the browser input http://{ip}:3000/ You can open the site just like the example site

Next Start APP2

Docker start APP2

also no problem, browser input http://{ip}:4000/ You can open the site and output the same results.

This completes the creation of mirrors and containers, and also successfully runs our own programs on the Docker platform.

To summarize, the various parts of the context, currently contains the following sections

Aspdotnetcoremvcdocker MVC Site Program

Dockerfile creating a configuration file for a custom image

Shenba/aspdotnetcoremvc a custom image (created based on the Microsoft/aspnetcore image,

contains a Spdotnetcoremvcdocker the site content

Examples of containers for APP1,APP2 SHENBA/ASPDOTNETCOREMVC, mapped to different ports on the host

The entire diagram is as follows:

Run the ASPDOTNETCOREMVC program on Docker-part1

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.