標籤:
本文描述下 .net core 在 docker 裡面的玩法
首先按照官方文檔先 拉取鏡像
docker pull microsoft/dotnet:latest
然後就有了 dotnet 這個運行時了,這裡以一個 Web 專案舉例子 先建立一個 web 類型的 project
dotnet new -t web
如果本地沒有 dotnet 運行時可以docker run 一個
docker run --rm -it -v /home/hello_dotnet/:/home/hello_dotnet/ microsoft/dotnet:latest
上面這個命令的意思是 讓 docker 運行(docker run)一個dotnet:latest的鏡像(microsoft/dotnet:latest) 並且把本地的/home/hello_dotnet卷路徑掛載到容器的/home/hello_dotnet 下面(-v /home/hello_dotnet/:/home/hello_dotnet/)然後提供互動式模式能夠輸入輸出(-it)退出之後自動刪除容器(--rm)
然後進入容器產生類似這樣
[[email protected]172-16-0-20 home]# docker run --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# lsControllers Data Models Program.cs README.md Services Startup.cs Views appsettings.json bower.json gulpfile.js package.json project.json web.config wwwroot[email protected]:/home/hello_dotnet#
然後更新下包依賴
dotnet restore
直接運行
dotnet run
修改一下預設的監聽連接埠預設是 localhost:5000 不好做代理,把它修改成 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") //添加這一行 .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } }}
ok 沒啥問題 publish 一下
[email protected]:/home/hello_dotnet# dotnet publishPublishing hello_dotnet for .NETCoreApp,Version=v1.0No executable found matching command "npm"
額,並不能夠 publish 提示找不到 npm, npm 這個不是 node 的包依賴工具麼。。 經過查看發現這個產生的 web 依賴了 gulp 所以很蛋疼 預設的容器只有運行時環境(可能主要是為了精簡鏡像)需要自己在安裝 node
這裡可以在容器裡面把 node 裝好或者去其他機器上 publish
apt-get update & apt-get install npm
並且安裝完 npm 還不夠 還需要 安裝 bower gulp
No executable found matching command "bower"
No executable found matching command "gulp"
npm install bower -g
npm install gulp -g
ok 結束了...
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
退出容器回到伺服器上
[email protected]:/home/hello_dotnet/bin/Debug/netcoreapp1.0# exitexit[[email protected]172-16-0-20 home]# lsdata docker-compose hello_dotnet java xiaoming xiaoqiu[[email protected]172-16-0-20 home]# cd hello_dotnet/[[email protected]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
代碼都已經建立好了,現在只要找個運行時 dotnet hello_dotnet.dll 就可以啟動 web 了
打包一個鏡像 寫個 dockerfile 就好
先建立個工作目錄整理下結構
[[email protected]172-16-0-20 dotnet]# tree -L 2.├── 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-16-0-20 dotnet]# docker build -t "hello_dotnet:1.0" ./ [[email protected]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 ---> 600558b4e65dRemoving intermediate container 0c05fc314674Step 3 : WORKDIR /home/www ---> Running in b5b029517595 ---> c9f05e277de9Removing intermediate container b5b029517595Step 4 : EXPOSE 8080 ---> Running in 2a00bea9393c ---> b921fe0482f0Removing intermediate container 2a00bea9393cStep 5 : CMD dotnet hello_dotnet.dll ---> Running in f770b366be81 ---> 11027359f344Removing intermediate container f770b366be81Successfully built 11027359f344運行[[email protected]172-16-0-20 dotnet]# docker run --rm -it -p 8888:5000 hello_dotnet:1.0info: 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.
瀏覽器訪問 http://172.16.0.20:8888
ok 一切正常
當然也可以用守護進程的模式運行
docker run -d -p 8888:5000 hello_dotnet:1.0
參考文檔
[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