From 5bb2743cb87cdaf14dc43576e7d0b17559b5a63a Mon Sep 17 00:00:00 2001 From: Stille Date: Sun, 10 Jul 2022 20:33:00 +0800 Subject: [PATCH] support custom api url by environment --- Dockerfile | 3 ++- README.md | 25 +++++++++++++------------ start.sh | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) create mode 100755 start.sh diff --git a/Dockerfile b/Dockerfile index bbb2765..af8cc69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,6 @@ RUN npm run build FROM nginx:1.16-alpine COPY --from=build /app/dist /usr/share/nginx/html +COPY start.sh / EXPOSE 80 -CMD [ "nginx", "-g", "daemon off;" ] +CMD [ "sh", "-c", "/start.sh" ] diff --git a/README.md b/README.md index 4f57f60..a7cf5ac 100644 --- a/README.md +++ b/README.md @@ -16,25 +16,26 @@ Docker [stilleshan/subweb](https://hub.docker.com/r/stilleshan/subweb)* ### docker 本地版 *适用于本机部署使用* ```shell -docker run -d --name subweb --restart always -p 18080:80 stilleshan/subweb +docker run -d --name subweb --restart always \ + -p 18080:80 \ + stilleshan/subweb ``` 访问 `http://127.0.0.1:18080` -### docker 在线版 (自定义后端 API) -*适用于服务器部署,配合域名反代使用* - -**修改自定义 subconverter API 地址需要自行编译构建镜像. ** +### docker 自定义后端 API 地址 +修改`API_URL`环境变量为你的后端 API 地址 ```shell -git clone https://github.com/stilleshan/subweb -cd subweb -# 修改 .env.productionn 中 VUE_APP_BASE_API_URL 为你 subconverter 后端 API 地址. -docker build -t subweb . -# 构建镜像 -docker run -d --name subweb --restart always -p 18080:80 subweb -# 启动容器 +docker run -d --name subweb --restart always \ + -p 18080:80 \ + -e API_URL=https://sub.ops.ci \ + stilleshan/subweb ``` +访问 `http://127.0.0.1:18080` +> *推荐使用 nginx 反向代理部署* + + ## 链接 - [GitHub - stilleshan/subweb](https://github.com/stilleshan/subweb) - [GitHub - stilleshan/subconverter](https://github.com/stilleshan/subconverter) diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..ee0bf38 --- /dev/null +++ b/start.sh @@ -0,0 +1,14 @@ +#/bin/sh + +if [ $API_URL ]; then + echo "当前 API 地址为: $API_URL" + for js in /usr/share/nginx/html/js/*.js + do + sed -i "s#http://127.0.0.1:25500#$API_URL#g" $js + done +else + echo "当前为默认本地 API 地址: http://127.0.0.1:25500" + echo "如需修改请在容器启动时使用 -e API_URL='https://sub.ops.ci' 传递环境变量" +fi + +nginx -g "daemon off;"