:::warning
本文件包含演練前所需預備之相關資訊,請參閱。
:::
## 🎯開發工具
### 下載並安裝 [Visual Studio 2022](https://visualstudio.microsoft.com/zh-hant/downloads/) - 請自行選擇版本
### 下載並安裝 [Docker Desktop](https://www.docker.com/get-started/)
:::danger
### MacOS 請使用 [OrbStack](https://orbstack.dev/)
:::
### 請參考下圖

### 安裝步驟,可參考:[在Windows上安装Docker桌面_Docker中文網](https://dockerdocs.cn/docker-for-windows/install/)
## 設定使用資源
:::danger
NOTE:如果出現紅框1說明,請從 Disk image location 新增 WSL2 設定檔,反之,直接使用 slider bar 調整即可
You are using the WSL 2 backend, so resource limits are managed by Windows.

:::
### Disk image location 新增 WSL2 設定檔

.wslconfig,可在 WSL 2 上執行的所有已安裝散發套件全域設定設定。
複製以下設定,安裝 WSL2 行2 不需修改,
memory 記憶體,請依自己機器資源調整,建議值 virtual processors * 2
processors 處理器,請依自己機器資源調整,此範例為 2個虛擬處理器
:::danger
筆記作者的設定值為 processors=2, memory=4GB 請依自己需求設定資源
:::
```code=yaml
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=2
```
### 開啟 VS2022 新增 api 空白專案
勾選「啟用 Docker」,選擇 Docker OS 「Linux」,按下「建立」

### 開啟命令提示字元 - Lab 1

切換目錄至專案資料夾

執行 docker build image 指令
```code=shell
docker build . -t dockersample:latest
```
存儲庫名稱將為 dockersample 標籤將為 latest [閱讀有關標籤的更多信息](https://docs.docker.com/engine/reference/commandline/tag/)

### 確認 docker image - Lab 2
```shell
docker images
```

#### 建置完成的 docker image 也能在 Docker Desktop 看見

### 建立開發憑證 - Lab 3
清除先前的 SSL 開發證書
```shell
dotnet dev-certs https --clean
```

#### 產生新的 SSL 開發證書 aspnetapp.pfx 請將 aspnetapp 名稱換為你的應用程式名稱

```shell
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\dockersample.pfx -p 123456
```
#### 檔案會產生在 .aspnet\https 路徑底下,再次確認開發憑證與你的應用程式專案名稱是否一致

#### 信任 ASP.NET Core HTTPS 開發證書
```shell
dotnet dev-certs https --trust
```
:::danger
Linux/macOS 作業系統,沒有 --trust 參數可用
:::

#### 開發證書,可以移至任意目錄存放,只要 docker run 有將開發憑證夾帶至 docker container 即可
作者預設存放目錄位置為:
```shell
d:\opt\docker
```
### docker run - Lab 4
#### 詳細指令參考 [docker run](https://docs.docker.com/engine/reference/commandline/run/)
執行指令
```shell
docker run --name dockersample -p 8000:80 -p 8001:443 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password=123456 -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/DockerSample.pfx -v D:/opt/docker/https:/https/ dockersample:latest
```
#### Options
| Name, shorthand | Default | Description |
|-----------------|---------|-------------|
| --publish , -p | |Publish a container’s port(s) to the host 意指 將容器的端口發佈到主機,8000 主機 Port : 80 容器 Port |
|--env , -e | |Set environment variables 設置環境變數 |
|--volume , -v | |Bind mount a volume 綁定掛載的 volume |
### 完成
