系列13 docker asp.net core部署

一.介紹

    本篇完整介紹asp.net core web api如何部署到docker容器中,並經過外部訪問web api服務。在編寫完成dockerfile以後,能夠經過docker [image] build命令來建立鏡像。web

    基本的格式爲:docker build [options] path | url | -docker

    該命令將讀取指定路徑(包括子目錄)的dickerfile,並將該路徑下全部數據做爲上下文發送給docker服務端。docker服務端在校驗dockerfile格式經過後,啄條執行其中定義的指令,碰到ADD, COPY 和RUN指令會生成一層新的鏡像。最終若是建立鏡像成功,會返回最終鏡像的ID。json

  1.  dockerfile文件建立ubuntu

    建立asp.net core web api應用程序,項目名爲:k8swebapi。 dockerfile以下所示:windows

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
EXPOSE 5000
COPY . .
ENTRYPOINT ["dotnet", "k8swebapi.dll", "--urls", "http://*:5000;http://*:5001"]centos

   2.上傳api

    asp.net core web api 發佈後,上傳到雲服務器opt目錄下瀏覽器

[root@VM_0_12_centos opt]# ls
containerd  kubectl-1.17.3-0.x86_64.rpm  rh  webapipublish

    定位到webapipublish目錄下,查看剛上傳的web文件bash

[root@VM_0_12_centos opt]# cd webapipublish
[root@VM_0_12_centos webapipublish]# ls
appsettings.Development.json                         Microsoft.OpenApi.dll
appsettings.json                                     Swashbuckle.AspNetCore.Swagger.dll
dll                                                  Swashbuckle.AspNetCore.SwaggerGen.dll
Dockerfile                                           Swashbuckle.AspNetCore.SwaggerUI.dll
k8swebapi.deps.json                                  System.Runtime.CompilerServices.Unsafe.dll
k8swebapi.dll                                        System.Text.Encodings.Web.dll
k8swebapi.pdb                                        System.Text.Json.dll
k8swebapi.runtimeconfig.json                         web.config
k8swebapi.Views.dll                                  wwwroot
k8swebapi.Views.pdb                                  YLYUN.Common.Dapper.dll
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer.dll  YLYUN.Common.Dapper.pdb
Microsoft.AspNetCore.Mvc.Versioning.dll              YLYUN.Common.Dapper.xml
Microsoft.Bcl.AsyncInterfaces.dll

   3.構建鏡像服務器

   執行格式:docker build -t <要生成的鏡像的名稱>   

        k8swebapi爲鏡像的名稱

     結尾的 . 指dockerfile路徑(.是表明上下文路徑,由於dockerfile就在當前目錄下)

[root@VM_0_12_centos webapipublish]# docker build -t k8swebapi .
Sending build context to Docker daemon 7.513MB
Step 1/5 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
---> e7e3b238011c
Step 2/5 : WORKDIR /app
---> Running in f6496d30d085
Removing intermediate container f6496d30d085
---> 5657f984ae35
Step 3/5 : EXPOSE 5000
---> Running in 93c58d4b5fc1
Removing intermediate container 93c58d4b5fc1
---> 1a6657883eb9
Step 4/5 : COPY . .
---> 9badab908e55
Step 5/5 : ENTRYPOINT ["dotnet", "k8swebapi.dll", "--urls", "http://*:5000;http://*:5001"]
---> Running in 25a44038e606
Removing intermediate container 25a44038e606
---> 3068b399a9f4
Successfully built 3068b399a9f4
Successfully tagged k8swebapi:latest

    構建鏡像成功後,顯示Successfully ,返回鏡像ID 3068b399a9f4

  4.建立容器並啓動容器

    使用docker run  

    -d表明是後臺運行容器

    --rm指定容器中止後自動刪除容器, 用docker stop  contriner 中止後自動刪除該容器

    --P(大寫)  宿主主機自動分配端口並關聯容器暴露的5000端口,外部使用宿主主機自動分配的端口訪問web api服務

    --name    容器名稱

    最後一個參數是鏡像名稱,前面的參數都是[OPTIONS]  格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

[root@VM_0_12_centos webapipublish]# docker run -d --rm -P  --name  k8swebapi  k8swebapi
dd8b01b33183f621d98043fdddbb7fda5817d312b7943a53718d6e6b0b0b94aa

    經過docker ps查看建立的容器,和宿主自動分配的端口32770。 容器內訪問api服務端口5000, 容器外訪問api服務端口32770

[root@VM_0_12_centos webapipublish]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
dd8b01b33183        k8swebapi           "dotnet k8swebapi.dl…"   About an hour ago   Up About an hour    0.0.0.0:32770->5000/tcp   k8swebapi
3ddee94cef3a        ubuntu:latest       "/bin/bash"              6 days ago          Up 6 days                                     quizzical_nash

 

  5. 進入容器  

   進入k8swebapi容器,查看app目錄,再請求api服務,以下所示:

[root@VM_0_12_centos webapipublish]# docker exec -it dd8b01b33183 /bin/bash
root@dd8b01b33183:/app# ls
Dockerfile                         System.Text.Encodings.Web.dll  k8swebapi.Views.pdb
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer.dll  System.Text.Json.dll        k8swebapi.deps.json
Microsoft.AspNetCore.Mvc.Versioning.dll             YLYUN.Common.Dapper.dll        k8swebapi.dll
Microsoft.Bcl.AsyncInterfaces.dll             YLYUN.Common.Dapper.pdb        k8swebapi.pdb
Microsoft.OpenApi.dll                     YLYUN.Common.Dapper.xml        k8swebapi.runtimeconfig.json
Swashbuckle.AspNetCore.Swagger.dll             appsettings.Development.json   web.config
Swashbuckle.AspNetCore.SwaggerGen.dll             appsettings.json            wwwroot
Swashbuckle.AspNetCore.SwaggerUI.dll             dll
System.Runtime.CompilerServices.Unsafe.dll         k8swebapi.Views.dll
root@dd8b01b33183:/app# curl http://localhost:5000/api/v1/user/IndexList
[{"id":1,"name":"張三"},{"id":2,"name":"李四"}]root@dd8b01b33183:/app# 

  6. 外部訪問

    在宿主主機上訪問,以下所示:

[root@VM_0_12_centos webapipublish]# curl http://localhost:32770/api/v1/user/IndexList
[{"id":1,"name":"張三"},{"id":2,"name":"李四"}][root@VM_0_12_centos webapipublish]# 

    外面經過windows系統的瀏覽器訪問(這裏的ip爲服務器雲網ip),以下所示:

相關文章
相關標籤/搜索