DOCKER上运行DOTNET CORE

原文: DOCKER上运行DOTNET CORE

下载microsoft/dotnet镜像

运行命令:

docker pull microsoft/dotnet

如果没有使用阿里镜像加速的,参照这篇先配置好再跑上面命令:

http://www.cnblogs.com/windchen/p/6231009.html

启动持久化容器

docker run -itd -p 5000:5000 microsoft/dotnet

-p用来添加Host跟Container的端口映射

创建.NET Core MVC项目

找到刚才启动容器的id:

docker ps

进入容器

docker attach [id]

创建及启动.NET Core MVC项目

mkdir MyFirstWeb
cd MyFirstWeb
dotnet new -t web
dotnet restore
dotnet run

使用博客园Nuget镜像加速

上面dotnet restore这一步可能会卡很久遇到超时的状况,因为Nuget在国外的原因,博客园有提供加速镜像,参照设定好之后,速度会快很多

http://www.cnblogs.com/windchen/articles/6235381.html

因为microsoft/dotnet镜像里面么有vi编辑器,所以改NuGet.Config文件需要在Host上改好之后再Copy进去。

先把NuGet.Config文件从容器里面cp出来

cp ~/.nuget/NuGet/NuGet.Config ./

按ctrl+p,ctrl+q退出容器,然后运行

docker cp [container id]:/MyFirstWeb/NuGet.Config ./

现在就可以在Host上编辑NuGet.Config文件了。

编辑好了之后再复制进容器

docker cp ./NuGet.Config  [container id]:/MyFirstWeb/

改变默认建立的MVC项目监听的Host地址

用同样的方法修改Program.cs文件,红色字体部分:

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore.Hosting;
 7 
 8 namespace WebApplication
 9 {
10     public class Program
11     {
12         public static void Main(string[] args)
13         {
14             var host = new WebHostBuilder()
15                 .UseKestrel()
16                 .UseContentRoot(Directory.GetCurrentDirectory())
17                 .UseIISIntegration()
18                 .UseUrls("http://*:5000")
19                 .UseStartup<Startup>()
20                 .Build();
21 
22             host.Run();
23         }
24     }
25 }
复制代码

修改完成之后,再运行

dotnet run

然后就可以通过Host的IP地址来访问了:

http://host:5000

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10336499.html