. NET core for docker, coredocker

Source: Internet
Author: User
Tags install node

. NET core for docker, coredocker

This article describes how. net core works in docker.

First, pull the image according to the official documentation.

docker pull microsoft/dotnet:latest

 

Then we have the dotnet runtime. Here we use a web project as an example to create a web project.

dotnet new -t web

If there is no local dotnet runtime, you can run

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

The above command means to make docker run a dotnet: latest image (microsoft/dotnet: latest) mount the local/home/hello_dotnet volume path to/home/hello_dotnet of the container (-v/home/hello_dotnet/:/home/hello_dotnet /) then, the interactive mode is provided to automatically delete the container (-- rm) after the input/output (-it) exits)

Then enter the container to generate something similar to this

 

[root@172-16-0-20 home]# docker run --rm -it -v /home/hello_dotnet/:/home/hello_dotnet/  microsoft/dotnet:latestroot@193fb1cf32a6:/# cd /home/hello_dotnet/root@193fb1cf32a6:/home/hello_dotnet# dotnet new -t webCreated new C# project in /home/hello_dotnet.root@193fb1cf32a6:/home/hello_dotnet# lsControllers  Data  Models  Program.cs  README.md  Services  Startup.cs  Views  appsettings.json  bower.json  gulpfile.js  package.json  project.json  web.config  wwwrootroot@193fb1cf32a6:/home/hello_dotnet#

 

Then update the package dependency

Dotnet restore

Run directly

Dotnet run

Modify the default listening port localhost: 5000 as a proxy and change it to 0.0.0.0.

 

Using System; using System. collections. generic; using System. IO; using System. linq; using System. threading. tasks; using Microsoft. aspNetCore. hosting; namespace WebApplication {public class Program {public static void Main (string [] args) {var host = new WebHostBuilder (). useUrls ("http: // 0.0.0.0: 5000") // Add this line. useKestrel (). useContentRoot (Directory. getCurrentDirectory ()). useIISIntegration (). useStartup <Startup> (). build (); host. run ();}}}

 

OK. No problem. publish.

root@193fb1cf32a6:/home/hello_dotnet# dotnet publishPublishing hello_dotnet for .NETCoreApp,Version=v1.0No executable found matching command "npm"

 

Yes. The publish prompt cannot find npm. Isn't the node package dependency tool npm .. After checking that the generated web is dependent on gulp, the default container is only the runtime environment (probably mainly to streamline the image ).

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

apt-get update & apt-get install npm

You need to install bower gulp after installing npm.

No executable found matching command "bower"

No executable found matching command "gulp"

Npm install bower-g

Npm install gulp-g

OK is over...

Project hello_dotnet (. NETCoreApp, Version = v1.0) was previusly compiled. Skipping compilation.

Ing 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'

Using ing project completed successfully

Publish: Published to/home/hello_dotnet/bin/Debug/netcoreapp1.0/publish

Published 1/1 projects successfully

Exit the container and return to the server

root@193fb1cf32a6:/home/hello_dotnet/bin/Debug/netcoreapp1.0# exitexit[root@172-16-0-20 home]# lsdata  docker-compose  hello_dotnet  java  xiaoming  xiaoqiu[root@172-16-0-20 home]# cd hello_dotnet/[root@172-16-0-20 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 has been created. Now, you only need to find a runtime dotnet hello_dotnet.dll to start the web.

Package an image and write a dockerfile.

Create a new working directory to organize the structure

[Root @ 172-16-0-20 dotnet] # tree-L 2. ── Dockerfile ── hello_dotnet ├ ── appsappsettings. 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 [root @ 172-16-0-20 dotnet] # docker build-t "hello_dotnet: 1.0 ". /[root @ 172-16-0-20 dotnet] # docker build-t "hello_dotnet: 1.0 ". /Sending build context to Docker daemon 53.04 MBStep 1: FROM microsoft/dotnet: latest ---> 4028809f66a4Step 2: COPY hello_dotnet/home/www ---> using intermediate container 0c05fc314674Step 3: WORKDIR/home/www ---> Running in b5b029517595 ---> using intermediate container limit 4: EXPOSE 8080 ---> Running in 2a00bea9393c ---> using intermediate container 2a00bea9393cStep 5: CMD dotnet hello_dotnet.dll ---> Running in f770b%be81 ---> using intermediate container extends built 11027359f344 run [root @ 172-16-0-20 dotnet] # docker run -- rm-it-p 8888: 5000 hello_dotnet: 1.0 info: Microsoft. extensions. dependencyInjection. dataProtectionServices [0] User profile is available. using '/root /. aspnet/DataProtection-Keys 'as key repository; keys will not be encrypted at rest. hosting environment: ProductionContent root path:/home/wwwNow listening on: http://0.0.0.0:5000Application Started. Press Ctrl + C to shut down.

 

Browser access http: // 172.16.0.20: 8888

OK, everything is normal.

 

You can also run it in daemon mode.

docker run -d -p 8888:5000 hello_dotnet:1.0

References

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

[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

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.