Contents

初探Docker-Visual Studio

初探Docker-Visual Studio

承上篇,上篇只是簡單嘗試一下Docker,這篇則是記錄自己開發環境,如何運行Docker

ASP.NET 專案

新增ASP.NET Core專案

我直接取名Docker1

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_16-23-41.png

新增Dockerfile和.dockerignore

在方案資料夾中新增Dockerfile和dockerignore,若Dockerfile放在專案資料夾中,請參考補充

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_14-08-11.png

Dockerfile內容

Dockerfile內容,以下為微軟範例稍加修改,說明可參考

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# https://hub.docker.com/_/microsoft-dotnet-core
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.sln .
COPY Docker1/*.csproj ./Docker1/
RUN dotnet restore

# copy everything else and build app
COPY Docker1/. ./Docker1/
WORKDIR /source/Docker1
RUN dotnet publish -c release -o /app --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "Docker1.dll"]

.dockerignore內容

1
2
3
4
5
6
7
8
# directories
**/bin/
**/obj/
**/out/

# files
Dockerfile*
**/*.md

建立鏡像Image

1
docker build -t docker1 .

-t docker1 : 將產生的 Image 取名為 docker1

. : 是指當下目錄

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_16-45-58.png

運行Container

1
docker run -it --rm -p 5000:80 --name docker1_sample docker1

-t : 在容器中建立了一個終端機(base)

-i : 建立與容器輸入(stdin)的互動連結

-it : 進入Base執行一些命令並查看返回結果(-i -t合併使用)

-p 本機 Port : Container 內部 Port : 建立本機端與容器mapping

--name : 指定 Container 的別名

注意這邊docker1預設是使用latest,如果Image的TAG非latest,則要指定,例如 : docker2:dev

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_16-49-27.png /static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_16-50-41.png

雖然可以正常執行了,但剛剛看到PowerShell有出現警告,應是SSL問題

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_16-58-46.png

處理HTTPS

參考微軟Docs使用 HTTPS 執行預先建立的容器映射

  1. 產生憑證並設定本機電腦

    {password}請自行替換

    1
    2
    
    dotnet dev-certs https -ep $env:USERPROFILE\.aspnet\https\aspnetapp.pfx -p {password here}
    dotnet dev-certs https --trust
    
  2. 使用命令 shell 中針對 HTTPS 設定的 ASP.NET Core 來執行容器映射

    1
    
    docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="{password}" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:/https/ docker1
    

連線到 https://localhost:8001/ 現在https可正常執行,且警告消失了

 

手動建置並部署

上面的Dockerfile是放到Docker伺服器才build,但我另一個常用的是build後的檔案,所以這邊也來測一下

發佈專案

先將Docker1專案,發佈成published資料夾

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_17-39-06.png

新增Dockerfile

/static/初探Docker_VisualStudio_5805edf8734b46e199e08f44e54a8091/2020-10-30_15-56-27.png

Dockerfile內容

1
2
3
4
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY published/. ./
ENTRYPOINT ["dotnet", "Docker1.dll"]

建立鏡像Image和運行Container

同樣在此目錄底下運行Image和Container

1
2
3
4
5
docker build -t docker1 .
docker run -it --rm -p 5000:80 --name docker1_sample docker1

// OR 使用SSL
// docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="{password}" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:/https/ docker1

連線到 https://localhost:8001/

補充

Dockerfile放在專案資料夾中,一併受到Git的控制,Dockerfile內容則修改為

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# https://hub.docker.com/_/microsoft-dotnet-core
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY Docker1.csproj .
RUN dotnet restore Docker1.csproj

# copy everything else and build app
COPY . .
RUN dotnet publish -c release -o /app 

# final stage/image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "Docker1.dll"]

參考

Docker 从入门到实践

https://yeasy.gitbook.io/docker_practice/

Docker 官網

https://www.docker.com/

深入理解 Docker 构建上下文

https://blog.csdn.net/qianghaohao/article/details/87554255

ASP.NET Core 的 Docker 映像

https://docs.microsoft.com/zh-tw/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.1

Docker 應用程式的開發工作流程

https://docs.microsoft.com/zh-tw/dotnet/architecture/microservices/docker-application-development-process/docker-app-development-workflow

結論

初探應該到這邊就結束了,主要是以手動建立 Dockerfile 為主,透過不斷的trial and error,加深對Dockerfile 的印象,最主要是層層疊上去的概念,之後等有機會建置微服務且使用 Docker,再來深入組建多容器 Docker 應用程式的部分。