使用開發 Proxy 時,您可以選擇直接在機器或 Docker 容器中執行它。 在 Docker 中執行開發 Proxy 是將其與本機環境隔離的絕佳方式。 它也為您提供在本地主機與 CI/CD 環境之間使用開發代理的一致方式。
使用 Dev Proxy Docker 映像
為了您的方便,我們提供了一個可以直接使用的 Docker 映像檔,內含 Dev Proxy。 您可以使用它,在 Docker 容器中執行 Dev Proxy。 您可以使用下列命令,從 GitHub Container Registry 提取映射:
docker pull ghcr.io/dotnet/dev-proxy:latest
備註
若要嘗試最新的預覽功能,請使用開發 Proxy 容器的 Beta 版本。
docker pull ghcr.io/dotnet/dev-proxy:beta
如果您在 CI/CD 管線中使用 Dev Proxy,請考慮使用特定版本的映像檔,而不是 latest
或 beta
。 如此一來,您就可以確保管線不會受到最新版開發 Proxy 中導入的任何重大變更所影響。 例如,若要使用 Dev Proxy 0.26.0 版,請使用下列命令:
docker pull ghcr.io/dotnet/dev-proxy:0.26.0
啟動開發 Proxy 容器
若要啟動 Dev Proxy 容器,請使用下列命令:
docker run \
# remove the container when it exits
--rm \
# run the container interactively
-it \
# map Dev Proxy ports to the host
-p 8000:8000 -p 8897:8897 \
# map volumes to the host. Create the `cert` folder in the current working directory before running the command
-v ${PWD}:/config -v ${PWD}/cert:/home/devproxy/.config/dev-proxy/rootCert \
# specify the image to use
ghcr.io/dotnet/dev-proxy:0.26.0
執行命令之後,Dev Proxy 會自動啟動,並接聽埠 8000 上的流量。 由於它在 Docker 容器中執行,因此不會自動註冊為主機上的系統 Proxy 來攔截 Web 要求。 相反地,您必須在主機上手動設定系統 Proxy,或為您的應用程式設定 Proxy。
透過以互動方式執行容器(使用 -it
選項),您可以從命令行控制Dev Proxy。 與 Dev Proxy 互動很有説明,例如,用於啟動和停止錄製、清除螢幕等等。如果您在背景啟動容器,您仍然可以使用 開發 Proxy API 來控制開發 Proxy。
參數
開發 Proxy Docker 包含數個參數,可用來自定義其行為。
港口
映射會公開下列埠:
- 8000 - 開發 Proxy 接聽傳入流量的埠。
- 8897:開發代理公開其 API 的埠。 您可以使用它,以程序設計方式與開發 Proxy 互動。
這很重要
請務必將這兩個埠對應至主機,以便您可以從本機計算機存取 Dev Proxy 並使用 Dev Proxy 工具組。
音量
影像顯示以下磁碟區:
-
/config - 容器啟動開發 Proxy 的目前工作目錄。 如果您對應的資料夾包含檔案
devproxyrc.json
,Dev Proxy 會自動使用它來設定本身。 - /home/devproxy/.config/dev-proxy/rootCert - Dev Proxy 儲存其跟證書的資料夾。 藉由將磁碟區對應至主機,您可以輕鬆地存取根憑證,並在您的系統或瀏覽器中加以安裝。
小提示
或者,與其將根證書對應至主機,您可以使用開發代理 API,以 PEM(隱私權增強郵件)格式下載根證書的公鑰。 若要下載憑證,請呼叫 GET http://127.0.0.1:8897/proxy/rootCertificate?format=crt
端點。
在 Docker 中使用開發 Proxy
當您啟動開發 Proxy 容器時,會自動啟動開發 Proxy 接聽埠 8000 上的連入流量。
預設設定值
開發人員 Proxy 會尋找 devproxyrc.json
您對應至磁碟區之資料夾中的 /config
檔案。 如果它找到檔案,它會使用它來設定本身。 如果找不到檔案,則會使用預設組態。
自訂設定
您可以在對應至 /config
磁碟區的資料夾中建立 devproxyrc.json
檔案,以使用 Dev Proxy 的自定義組態。 或者,您可以在啟動 Dev Proxy 時使用 --config-file
參數來指定組態檔。 例如,若要使用 myconfig.json
檔案,請使用下列命令:
docker run \
# remove the container when it exits
--rm \
# run the container interactively
-it \
# map Dev Proxy ports to the host
-p 8000:8000 -p 8897:8897 \
# map volumes to the host. Create the `cert` folder in the current working directory before running the command
-v ${PWD}:/config -v ${PWD}/cert:/home/devproxy/.config/dev-proxy/rootCert \
# specify the image to use
ghcr.io/dotnet/dev-proxy:0.26.0 \
# specify the configuration file to use
--config-file /config/myconfig.json
您指定的組態檔必須可從容器存取。 如果您使用相對路徑,它必須相對於磁碟區 /config
。
其他選項
如果您想要使用其他選項,您可以直接在機器上執行 Dev Proxy 時,以相同的方式指定它們。 例如,若要指定要監看的 URL,請使用下列命令:
docker run \
# remove the container when it exits
--rm \
# run the container interactively
-it \
# map Dev Proxy ports to the host
-p 8000:8000 -p 8897:8897 \
# map volumes to the host. Create the `cert` folder in the current working directory before running the command
-v ${PWD}:/config -v ${PWD}/cert:/home/devproxy/.config/dev-proxy/rootCert \
# specify the image to use
ghcr.io/dotnet/dev-proxy:0.26.0 \
# specify the URLs to watch
--urls-to-watch "https://example.com/*"