【原創】 Docker 中 運行 ASP.NET Core 站點

索引:html

目錄索引web

一. 創建 .NetCore 項目docker

   a.新建項目json

------------------------------------------------------------------------------------vim

------------------------------------------------------------------------------------bash

       b.選擇項目類型app

------------------------------------------------------------------------------------ide

------------------------------------------------------------------------------------網站

        c.添加控制器ui

------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------

------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Mvc;
 6 
 7 namespace DemoLM.Controllers
 8 {
 9     public class DockerTestController : Controller
10     {
11         public IActionResult DockerIndex()
12         {
13             return View();
14         }
15     }
16 }
DockerTestController

-------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------

        d.添加視圖

-------------------------------------------------------------------------------------

 

---------------------------------------------------------------------------------------

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>DockerIndex</title>
11 </head>
12 <body>
13     <h1>這是Docker中的Asp.net Core APP !</h1>
14 </body>
15 </html>
DockerIndex

---------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------

       e.修改默認請求

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Builder;
 6 using Microsoft.AspNetCore.Hosting;
 7 using Microsoft.Extensions.Configuration;
 8 using Microsoft.Extensions.DependencyInjection;
 9 using Microsoft.Extensions.Logging;
10 
11 namespace DemoLM
12 {
13     public class Startup
14     {
15         public Startup(IHostingEnvironment env)
16         {
17             var builder = new ConfigurationBuilder()
18                 .SetBasePath(env.ContentRootPath)
19                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
20                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
21                 .AddEnvironmentVariables();
22             Configuration = builder.Build();
23         }
24 
25         public IConfigurationRoot Configuration { get; }
26 
27         // This method gets called by the runtime. Use this method to add services to the container.
28         public void ConfigureServices(IServiceCollection services)
29         {
30             // Add framework services.
31             services.AddMvc();
32         }
33 
34         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
35         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
36         {
37             loggerFactory.AddConsole(Configuration.GetSection("Logging"));
38             loggerFactory.AddDebug();
39 
40             if (env.IsDevelopment())
41             {
42                 app.UseDeveloperExceptionPage();
43                 app.UseBrowserLink();
44             }
45             else
46             {
47                 app.UseExceptionHandler("/Home/Error");
48             }
49 
50             app.UseStaticFiles();
51 
52             app.UseMvc(routes =>
53             {
54                 routes.MapRoute(
55                     name: "default",
56                     template: "{controller=DockerTest}/{action=DockerIndex}/{id?}");
57             });
58         }
59     }
60 }
Startup

---------------------------------------------------------------------------------------

 --------------------------------------------------------------------------------------

      f.發佈

---------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------

二. 準備 CentOS 環境

       a.準備虛擬機

---------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

       b.安裝 docker

1 yum install docker
cent bash

----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------

       c.設置docker 服務

1 systemctl start docker.service
2 systemctl enable docker.service
cent bash

-----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------

三. 打包 站點鏡像

       a.將程序放入 /root

-----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------

       b.命令切換至 DemoLM

1 cd ./DemoLM
cent bash

-----------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------

      c.建立 Dockerfile

1 vim ./Dockerfile
cent bash

--------------------------------------------------------------------------------------

------------------------------------------------------------------------------------

1 FROM docker.io/microsoft/aspnetcore
2 COPY ./bin/Release/PublishOutput ./publish
3 WORKDIR /publish
4 EXPOSE 80
5 CMD ["dotnet","DemoLM.dll"]
image bash

-----------------------------------------------------------------------------------

------------------------------------------------------------------------------------

      d.打包鏡像

1 docker build -t lmapp:1.0 .
cent bash

------------------------------------------------------------------------------------

 

---------------------------------------------------------------------------------------

四. 運行鏡像 並瀏覽網站

        a.查看鏡像

1 docker images
cent bash

---------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

      b.運行鏡像

1 docker run -d -t --name lmweb10 -p 8800:80 lmapp:1.0
cent bash

----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

       c.查看運行狀態

1 docker logs lmweb10
cent bash

---------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------

1 docker ps
cent bash

-----------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------

      d.瀏覽站點

-------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

五. 詳細知識點講解,請看PPT 分享 O(∩_∩)O哈哈~  ( PS:請關注後續博客 )

       a. Linux 講解

       b. .NetCore 講解

       c. Docker 講解

       e. Git hooks 講解

       f. Visual Studio 講解

 

                                         蒙

                                    2017-07-05  14:28  週三

相關文章
相關標籤/搜索