使用开发代理时,可以选择直接在计算机或 Docker 容器中运行它。 在 Docker 中运行开发代理是将其与本地环境隔离的好方法。 它还为您提供了一种在本地计算机与 CI/CD 环境之间使用开发代理的一致方法。
使用开发代理 Docker 映像
为方便起见,我们提供了一个现成的开发代理 Docker 映像。 可以使用它在 Docker 容器中运行开发代理。 可以使用以下命令从 GitHub 容器注册表拉取映像:
docker pull ghcr.io/dotnet/dev-proxy:latest
注释
若要试用最新的预览功能,请使用开发代理容器的 beta 版本。
docker pull ghcr.io/dotnet/dev-proxy:beta
如果在 CI/CD 管道中使用开发代理,请考虑使用映像的特定版本,而不是 latest
或 beta
。 这样,就可以确保您的流程不受最新版本 Dev Proxy 中引入的任何重大变更的影响。 例如,若要使用开发代理版本 0.26.0,请使用以下命令:
docker pull ghcr.io/dotnet/dev-proxy:0.26.0
启动开发代理容器
若要启动开发代理容器,请使用以下命令:
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
运行命令后,开发代理会自动启动,并在端口 8000 上侦听流量。 由于它在 Docker 容器中运行,因此它不会自动注册为主机上的系统代理来截获 Web 请求。 相反,你需要在主机上手动配置系统代理或为应用程序配置代理。
通过以交互方式(使用 -it
选项)运行容器,可以从命令行控制开发代理。 与开发代理交互非常有用,例如,启动和停止录制、清除屏幕等。如果在后台启动容器,仍可使用 开发代理 API 控制开发代理。
参数
开发代理 Docker 包含多个参数,可用于自定义其行为。
港口
该映像公开以下端口:
- 8000 - 用于开发代理监听传入流量的端口。
- 8897 - 开发代理在其上公开其 API 的端口。 可以使用它以编程方式与 Dev 代理进行交互。
重要
请务必将这两个端口映射到主机,以便可以从本地计算机访问 开发代理并使用开发代理工具包。
体积
该镜像公开以下卷:
- /config - 容器从中启动开发代理的当前工作目录。 如果映射的文件夹包含
devproxyrc.json
文件,则开发代理会自动使用它来配置自身。 - /home/devproxy/.config/dev-proxy/rootCert - Dev Proxy 在其中存储其根证书的文件夹。 通过将卷映射到主机,可以轻松访问根证书并将其安装到系统或浏览器中。
小提示
或者,若要将根证书映射到主机,可以使用开发代理 API 以 PEM(隐私增强邮件)格式 下载根证书的公钥 。 若要下载证书,请调用 GET http://127.0.0.1:8897/proxy/rootCertificate?format=crt
终结点。
在 Docker 中使用开发代理
当启动开发代理容器时,它会自动在端口 8000 上启动开发代理以监听传入的流量。
默认配置
开发代理会在你映射到 devproxyrc.json
卷的文件夹中查找 /config
文件。 如果找到该文件,它将使用它来配置自身。 如果找不到该文件,则使用默认配置。
自定义配置
你可以通过在映射到 devproxyrc.json
卷的文件夹中创建 /config
文件,为开发代理使用自定义配置。 或者,可以在启动开发代理时使用 --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
卷。
其他选项
如果要使用其他选项,可以像直接在计算机上运行开发代理时一样指定它们。 例如,若要指定要监视的 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/*"