. NET Core for Docker

Source: Internet
Author: User
Tags dotnet hosting install node docker run

This article describes how to play the. NET core in Docker

First pull the image in the official document

Docker Pull Microsoft/dotnet:latest

And then there's dotnet this runtime, here's a Web project to create a Web-type project

dotnet NEW-T Web

If you do not have a local dotnet runtime, you can Docker run a

Docker run-rm -it-v/home/hello_dotnet/:/home/hello_dotnet/  microsoft/dotnet:latest

The above command means letting Docker runa dotnet:latest image (microsoft/dotnet:latest) and putting the local/home/hello_ The dotnet volume path is mounted under the container's/home/hello_dotnet (-v/home/hello_dotnet/:/home/hello_dotnet/) and then provides interactive mode for the input output (-it ) automatically delete containers after exiting (--rm)

And then into the container to generate something like this

 [[Email protected]172 -16 - Span style= "color: #800080;" >0 -rm -it-v /home/hello_dotnet/:/home/hello_dotnet/microsoft/dotnet:latest[email  Protected]: /# cd/home/hello_dotnet/[email protected]: /home/ hello_dotnet# dotnet New-t webcreated new C # project  in /home/hello_dotnet.[ Email protected]: /home/hello_dotnet# ls  controllers Data Models Program.cs readme.md Services Startup.cs views APPSETTINGS.J Son Bower.json gulpfile.js package.json project.json Web. config wwwroot[email protected]: /home/hello_dot net# 

Then update the next package dependency

dotnet Restore

Run directly

Dotnet Run

Modify the default listening port by default localhost:5000 is not good proxy, modify it to 0.0.0.0

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Hosting;namespacewebapplication{ Public classProgram { Public Static voidMain (string[] args) {            varHost =NewWebhostbuilder (). Useurls ("http://0.0.0.0:5000")//Add this line                . Usekestrel (). Usecontentroot (Directory.GetCurrentDirectory ()). Useiisintegration (). Usestartup<Startup>()                .             Build (); Host.        Run (); }    }}

OK, no problem, publish.

[Email protected]:/home/ for. Netcoreapp,version=v1. 0  "npm"

Well, it is not possible to publish a hint to find NPM, NPM This is not node's package dependency tool. After viewing the web that generated this build relies on gulp so it hurts. The default container is only the runtime environment (which may be primarily for thin mirroring) and it needs to install node

You can install node in the container or go to other machines publish

Install NPM

and installing NPM is not enough to install Bower Gulp

No executable found matching command "bower"

No executable found matching command "gulp"

NPM Install Bower-g

NPM Install Gulp-g

OK it's over ...

Project hello_dotnet (. netcoreapp,version=v1.0) was previously compiled. Skipping compilation.

Configuring the following project for use with IIS: '/home/hello_dotnet/bin/debug/netcoreapp1.0/publish '

Updating Web. config at '/home/hello_dotnet/bin/debug/netcoreapp1.0/publish/web.config '

Configuring project completed successfully

Publish:published To/home/hello_dotnet/bin/debug/netcoreapp1.0/publish

Published 1/1 Projects successfully

Exit the container back to the server

[Email PROTECTED]:/HOME/HELLO_DOTNET/BIN/DEBUG/NETCOREAPP1.0# Exitexit[[email protected]172- --0- -home]#lsData Docker-Compose hello_dotnet Java xiaoming xiaoqiu[[email protected]172- --0- -home]# CD hello_dotnet/[[Email protected]172- --0- -hello_dotnet]#lsAppsettings.json bin Bower.json Controllers Data gulpfile.js Models node_modules obj Package.json Program.cs Project.json Project.lock.json readme.md Services Startup.cs views web. config wwwroot

The code is already created, so just find a runtime dotnet Hello_dotnet.dll to start the web.

Pack an image, write a dockerfile.

Create a new working directory to organize the structure

[[Email protected]172- --0- -dotnet]# tree-l2. ├──dockerfile└──hello_dotnet├──appsettings.json├──bin├──bower.json├──controllers├──data    ├──gulpfile.js├──models├──node_modules├──obj├──package.json├──program.cs├──project.json ├──project.lock.json├──readme.md├──services├──startup.cs├──views├──web.config└──wwwroot [[ Email protected]172- --0- -dotnet]# Docker Build-t"hello_dotnet:1.0"./[[Email protected]172- --0- -dotnet]# Docker Build-t"hello_dotnet:1.0"./sending build context to Docker daemon53.04Mbstep1: From microsoft/dotnet:latest--->4028809f66a4step2: COPY hello_dotnet/home/www--->600558b4e65dremoving Intermediate Container 0c05fc314674step3: workdir/home/www---> Runninginchb5b029517595--->c9f05e277de9removing Intermediate Container b5b029517595step4: EXPOSE8080---> Runninginch2a00bea9393c--->b921fe0482f0removing Intermediate Container 2a00bea9393cstep5: CMD dotnet hello_dotnet.dll---> Runninginchf770b366be81--->11027359f344removing Intermediate Container f770b366be81successfully built 11027359f344 run [[email protected]172- --0- -dotnet]# Docker Run--RM-it-p8888: theHello_dotnet:1.0Info: microsoft.extensions.dependencyinjection.dataprotectionservices[0] User profile is available. Using'/root/.aspnet/dataprotection-keys'As key repository; Keys is not being encrypted at rest. Hosting environment:productioncontent Root Path:/home/Wwwnow listening on:http://0.0.0.0:5000application started. Press Ctrl+c to shut.

Browser Access http://172.16.0.20:8888

OK, everything's fine.

Of course, you can also run in daemon mode.

8888: hello_dotnet:1.0

Reference documents

[0] Https://dotnet.github.io

[1] Https://docs.docker.com/engine/reference/builder

[2] Http://stackoverflow.com/questions/34212765/how-do-i-get-the-kestrel-web-server-to-listen-to-non-localhost-requests

. NET Core for 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.