本文描述下 .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)

然后进入容器生成类似这样

[root@--- home]# docker run --rm -it -v /home/hello_dotnet/:/home/hello_dotnet/  microsoft/dotnet:latest

root@193fb1cf32a6:/# cd /home/hello_dotnet/

root@193fb1cf32a6:/home/hello_dotnet# dotnet new -t web

Created new C# project in /home/hello_dotnet.

root@193fb1cf32a6:/home/hello_dotnet# ls

Controllers  Data  Models  Program.cs  README.md  Services  Startup.cs  Views  appsettings.json  bower.json  gulpfile.js  package.json  project.json  web.config  wwwroot

root@193fb1cf32a6:/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 一下

root@193fb1cf32a6:/home/hello_dotnet# dotnet publish

Publishing hello_dotnet for .NETCoreApp,Version=v1.

No 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

退出容器回到服务器上

root@193fb1cf32a6:/home/hello_dotnet/bin/Debug/netcoreapp1.# exit

exit

[root@--- home]# ls

data  docker-compose  hello_dotnet  java  xiaoming  xiaoqiu

[root@--- home]# cd hello_dotnet/

[root@--- hello_dotnet]# ls

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

代码都已经创建好了,现在只要找个运行时 dotnet hello_dotnet.dll 就可以启动 web 了

打包一个镜像 写个 dockerfile 就好

先新建个工作目录整理下结构

[root@--- dotnet]# tree -L 

.

├── 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

[root@--- dotnet]# docker build -t "hello_dotnet:1.0" ./

[root@--- dotnet]# docker build -t "hello_dotnet:1.0" ./

Sending build context to Docker daemon 53.04 MB

Step  : FROM microsoft/dotnet:latest

 ---> 4028809f66a4

Step  : COPY hello_dotnet /home/www

 ---> 600558b4e65d

Removing intermediate container 0c05fc314674

Step  : WORKDIR /home/www

 ---> Running in b5b029517595

 ---> c9f05e277de9

Removing intermediate container b5b029517595

Step  : EXPOSE 

 ---> Running in 2a00bea9393c

 ---> b921fe0482f0

Removing intermediate container 2a00bea9393c

Step  : CMD dotnet hello_dotnet.dll

 ---> Running in f770b366be81

 ---> 11027359f344

Removing intermediate container f770b366be81

Successfully built 11027359f344

运行

[root@--- dotnet]# docker run  --rm -it -p : hello_dotnet:1.0

info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[]

      User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.

Hosting environment: Production

Content root path: /home/www

Now listening on: http://0.0.0.0:5000

Application started. Press Ctrl+C to shut down.

浏览器访问 http://172.16.0.20:8888

ok 一切正常

当然也可以用守护进程的模式运行

docker run -d -p : 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的更多相关文章

  1. ASP.NET Core开发-Docker部署运行

    ASP.NET Core开发Docker部署,.NET Core支持Docker 部署运行.我们将ASP.NET Core 部署在Docker 上运行. 大家可能都见识过Docker ,今天我们就详细 ...

  2. 基于Microsoft Azure、ASP.NET Core和Docker的博客系统

    欢迎阅读daxnet的新博客:一个基于Microsoft Azure.ASP.NET Core和Docker的博客系统   2008年11月,我在博客园开通了个人帐号,并在博客园发表了自己的第一篇博客 ...

  3. [翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据

    原文地址:http://www.dotnetcurry.com/aspnet/1354/elastic-search-kibana-in-docker-dotnet-core-app 想要轻松地通过许 ...

  4. .NET Core微服务之ASP.NET Core on Docker

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Docker极简介绍 1.1 总体介绍 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.D ...

  5. [翻译] ASP.NET Core 利用 Docker、ElasticSearch、Kibana 来记录日志

    原文: Logging with ElasticSearch, Kibana, ASP.NET Core and Docker 一步一步指导您使用 ElasticSearch, Kibana, ASP ...

  6. asp.net core的docker实践

    如果centos中没有安装和docker和.net core镜像,先安装docker和asp.net core 镜像 安装dockeryum -y install docker-io 启动 Docke ...

  7. .Net Core in Docker - 在容器内编译发布并运行

    Docker可以说是现在微服务,DevOps的基础,咱们.Net Core自然也得上Docker..Net Core发布到Docker容器的教程网上也有不少,但是今天还是想来写一写. 你搜.Net c ...

  8. .net core in Docker 部署方案(随笔)

    前一段时间由于项目需要 .net core 在docker下的部署,途中也遇到很多坑,看了各同行的博客觉得多多少少还是有些问题,原本不想写此篇文章,由于好友最近公司也需要部署,硬是要求,于是花了些时间 ...

  9. Asp.Net Core 使用Docker进行容器化部署(一)

    前几篇文章介绍了Liunx下的环境搭建,今天来分享一下.Net Core在Liunx上的部署. 我采用的方案是使用Dokcer作为运行虚拟机,Nginx作为Http服务器来进行反向代理,你可以理解为D ...

随机推荐

  1. 关于Charles抓取手机访问的Https请求

    准备工作 本次测试的Charles版本为3.9.1 · 首先在Charles中开启HTTP请求的远程监听. · 然后分别在手机和Mac上安装Charles的证书. 注意:证书一定要一致,否则抓取不到. ...

  2. C#: using JsonReader avoid Deserialize Json to dynamic

    namespace Test { using Microshaoft; using Test.Models; using Newtonsoft.Json; using System; using Sy ...

  3. oracle创建DBLINK

    1.查看Global_name参数 show parameter global_name;  该参数为true时,你在本地建立的DBLINK的名称必须和远程的Global_name一致才行. 2.查看 ...

  4. jQuery判断checked的三种方法

    今天在查看他人源码时看到在判断复选框是否选中时,与自己的写法不同: .is(":checked") vs .prop("checked") == true 因此 ...

  5. 浩瀚PDA开单器-结束手工开单模式【百货、商超】PDA安卓智能手持POS 进销存管理系统移动收银管理软件

    移动销售终端:先进的跨平台技术,支持智能PDA等移动终端提供移动销售开单.移动POS.移动查询货品.移动盘点等功能. 移动开单器的操作说明 第一步:选客户 Ø        用户在空白开单主页左划屏幕 ...

  6. 丢掉慢吞吞的AVD吧,android模拟神器:Genymotion

    安装图文介绍: http://padhz.com/how-to-run-android-simulator-on-pc.html; 刚好手头上有visual box,亲自试用了一下.新建了一个Gala ...

  7. Can't load IA 32-bit .dll on a AMD 64-bit platform错误的解决

    64位的系统,64位的myeclipse,64位的jdk,64位的tomcat,结果报错:Can't load IA 64-bit .dll on a AMD 32-bit platform,简直无语 ...

  8. SOAPUI使用教程-测试JDBC数据库

    soapUI中有除了开源版本的一些非常实用的功能: 使用在项目级配置的JDBC连接 使用向导创建复杂的查询. 结果显示XML输出视图(以及该使用向导在此视图中提供的XPath断言). 提供JDBC连接 ...

  9. Linux内核笔记--深入理解文件描述符

    内核版本:linux-2.6.11 文件描述符(file descriptor)在Linux编程里随处可见,设备读写.网络通信.进程通信,fd可谓是关键中的关键. 深入理解可以增加我们使用它的信心. ...

  10. Leetcode Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...