本篇同步發文在個人Blog: 一袋.NET要扛幾樓?打造容器化的ASP.NET Core網站!系列文章 - (17) 建立會員系統 - 6
1. 建立Dockerfile
===================
在AuthApi專案右鍵=>加入=>Dokcer支援,選擇目標平台Linux,將會在根目錄新增Dockerfile檔案,但因為專案目錄設計方式,會讓VS產生錯誤的連結,所以需要再手動修改,變成下方的內容。其內容主要功能是在Docker容器內載入NET Core SDK編譯我們的程式碼,將編譯後的目錄檔案複製到Runtime環境的/app目錄,再用NET Core Runtime執行商品列表的網頁系統。
另外AuthApi要啟用Https,使用dotnet core sdk產生HTTPS的證書和密碼,連同放到編譯後的目錄。
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /code
EXPOSE 80
EXPOSE 443
COPY . .
RUN dotnet restore
RUN dotnet publish -o /out/ -c Release
RUN dotnet dev-certs https --clean
RUN dotnet dev-certs https -ep ./AuthApi.pfx -p crypticpassword
RUN mv ./AuthApi.pfx /out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
COPY --from=build /out /app/
WORKDIR /app
ENTRYPOINT ["dotnet", "AuthApi.dll"]
2. 修改dokcer-compose.yml
===========================
在docker-compose.yml加入AuthApi的container,另外指定HTTPS的證書和密碼,如下方的yml。主要的參數包含:
version: "3.4"
networks:
frontend:
backend:
services:
webmvc:
build:
context: .\src\Presentation\WebMvc
dockerfile: Dockerfile
environment:
- CatalogUrl=http://catalog
- IdentityUrl=https://${RPGSHOP_EXTERNAL_DNS_NAME_OR_IP}:7101
- CallBackUrl=http://${RPGSHOP_EXTERNAL_DNS_NAME_OR_IP}:7500/
container_name: rpgwebmvc
ports:
- "7500:80"
networks:
- frontend
depends_on:
- catalog
- auth
catalog:
build:
context: .\src\Services\CatalogApi
dockerfile: Dockerfile
image: rpgshop/catalog
environment:
- DatabaseServer=mssqlserver
- DatabaseName=CatalogDb
- DatabaseUser=sa
- DatabasePassword=JustTest!
- ExternalCatalogBaseUrl=http://${RPGSHOP_EXTERNAL_DNS_NAME_OR_IP}:7000
container_name: rpgcatalogapi
ports:
- "7000:80"
networks:
- backend
- frontend
depends_on:
- mssqlserver
auth:
build:
context: .\src\Services\AuthApi
dockerfile: Dockerfile
image: rpgshop/auth
environment:
- MvcClient=http://${RPGSHOP_EXTERNAL_DNS_NAME_OR_IP}:7500
- DatabaseServer=mssqlserver
- DatabaseName=AuthDb
- DatabaseUser=sa
- DatabasePassword=JustTest!
- ExternalCatalogBaseUrl=http://${RPGSHOP_EXTERNAL_DNS_NAME_OR_IP}:7000
- ASPNETCORE_URLS=http://+:80;https://+:443
- ASPNETCORE_Kestrel__Certificates__Default__Password=crypticpassword
- ASPNETCORE_Kestrel__Certificates__Default__Path=./AuthApi.pfx
container_name: rpgauthapi
ports:
- "7100:80"
- "7101:443"
networks:
- backend
- frontend
depends_on:
- mssqlserver
mssqlserver:
image: "microsoft/mssql-server-linux:latest"
ports:
- "1445:1433"
container_name: rpgmssqlcontainer
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=JustTest!
networks:
- backend
用cmd在docker-compose.yml所在位置輸入指令:
docker-compose build
docker-compose up
執行完後,在瀏覽器輸入http://docker.for.win.localhost:7500 ,按了右上角登入,跳轉到AuthApi的登入頁面,輸入完再跳回WebMvc的Catalog/About頁面,如圖1至圖3。
圖1
圖2
圖3
--------------------------------------------------------------
這一系列的專案將開在這Github:RPGShopOnContainers,有需要的人可自行pull / download~
下一篇將開始寫加入購物車的功能。