Skip to content

Commit

Permalink
🐛 fix: add PROXY_URL in docker with proxychains-ng (lobehub#3362)
Browse files Browse the repository at this point in the history
* ✨: feat: support PROXY_URL in docker with proxychains-ng

* 🔨 chore: let proxychains config more readable

* 🔨 chore: add package update

* ⚡️ perf: add CN mirror for Alpine

* 🔨 chore: add more comments

* 🔨 chore: change USE_NPM_CN_MIRROR to USE_CN_MIRROR

* 🐛 fix: fix cn mirror for alpine during building

* 🐛 fix: fix permission issue

* 🐛 fix: fix app path error

* ✨ feat: support proxy server with domain

* 🔨 chore: add more comments & cleanup code

* 🔨 chore: add more comments

* ⚡️ perf: resolve only when host is domain

* 🔨 chore: move IP_REGEX to head

* 🔨 chore: replace `! -z` to `-n`

* 🔨 chore: update comments

* ✨ feat: add `DEFAULT_AGENT_CONFIG` and `SYSTEM_AGENT` to env, allow set default model

* Add documentation for `PROXY_URL` environment variable

Add documentation for `PROXY_URL` environment variable in both English and Chinese versions of the environment variables guide.

* **English Documentation**
  - Add `PROXY_URL` section to specify the proxy URL for connecting to external services.
  - Include example values and a callout for Docker Desktop users.

* **Chinese Documentation**
  - Add `PROXY_URL` section to specify the proxy URL for connecting to external services.
  - Include example values and a callout for Docker Desktop users.

---------

Co-authored-by: Arvin Xu <[email protected]>
  • Loading branch information
hezhijie0327 and arvinxx authored Aug 3, 2024
1 parent 324be07 commit 920de7c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
61 changes: 54 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
## Base image for all the stages
FROM node:20-alpine AS base

ARG USE_CN_MIRROR

RUN \
# If you want to build docker in China, build with --build-arg USE_CN_MIRROR=true
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" "/etc/apk/repositories"; \
fi \
# Add proxychains-ng package & update base package
&& apk update \
&& apk add --no-cache proxychains-ng \
&& apk upgrade --no-cache \
# Add user nextjs to run the app
addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nextjs
&& addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nextjs \
&& chown -R nextjs:nodejs "/etc/proxychains" \
&& rm -rf /tmp/* /var/cache/apk/*

## Builder image, install all the dependencies and build the app
FROM base AS builder

ARG USE_NPM_CN_MIRROR
ARG USE_CN_MIRROR

ENV NEXT_PUBLIC_BASE_PATH=""

Expand Down Expand Up @@ -37,8 +49,8 @@ COPY package.json ./
COPY .npmrc ./

RUN \
# If you want to build docker in China, build with --build-arg USE_NPM_CN_MIRROR=true
if [ "${USE_NPM_CN_MIRROR:-false}" = "true" ]; then \
# If you want to build docker in China, build with --build-arg USE_CN_MIRROR=true
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
export SENTRYCLI_CDNURL="https://npmmirror.com/mirrors/sentry-cli"; \
npm config set registry "https://registry.npmmirror.com/"; \
fi \
Expand Down Expand Up @@ -85,7 +97,10 @@ ENV HOSTNAME="0.0.0.0" \
# General Variables
ENV ACCESS_CODE="" \
API_KEY_SELECT_MODE="" \
FEATURE_FLAGS=""
DEFAULT_AGENT_CONFIG="" \
SYSTEM_AGENT="" \
FEATURE_FLAGS="" \
PROXY_URL=""

# Model Variables
ENV \
Expand Down Expand Up @@ -138,4 +153,36 @@ USER nextjs

EXPOSE 3210/tcp

CMD ["node", "/app/server.js"]
CMD \
if [ -n "$PROXY_URL" ]; then \
# Set regex for IPv4
IP_REGEX="^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$"; \
# Set proxychains command
PROXYCHAINS="proxychains -q"; \
# Parse the proxy URL
host_with_port="${PROXY_URL#*//}"; \
host="${host_with_port%%:*}"; \
port="${PROXY_URL##*:}"; \
protocol="${PROXY_URL%%://*}"; \
# Resolve to IP address if the host is a domain
if ! [[ "$host" =~ "$IP_REGEX" ]]; then \
nslookup=$(nslookup -q="A" "$host" | tail -n +3 | grep 'Address:'); \
if [ -n "$nslookup" ]; then \
host=$(echo "$nslookup" | tail -n 1 | awk '{print $2}'); \
fi; \
fi; \
# Generate proxychains configuration file
printf "%s\n" \
'localnet 127.0.0.0/255.0.0.0' \
'localnet ::1/128' \
'proxy_dns' \
'remote_dns_subnet 224' \
'strict_chain' \
'tcp_connect_time_out 8000' \
'tcp_read_time_out 15000' \
'[ProxyList]' \
"$protocol $host $port" \
> "/etc/proxychains/proxychains.conf"; \
fi; \
# Run the server
${PROXYCHAINS} node "/app/server.js";
11 changes: 11 additions & 0 deletions docs/self-hosting/environment-variables/basic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ Further reading:

For specific content, please refer to the [Feature Flags](/docs/self-hosting/advanced/feature-flags) documentation.

### `PROXY_URL`

- Type: Optional
- Description: Used to specify the proxy URL for connecting to external services. The value of this variable should be different in different deployment environments.
- Default: -
- Example: `http://127.0.0.1:7890` or `socks5://localhost:7891`

<Callout type="info">
If you're using Docker Desktop on Windows or macOS, it relies on a virtual machine. In this setup, `localhost` / `127.0.0.1` refers to the localhost of the container itself. In such cases, please try using `host.docker.internal` instead of `localhost`.
</Callout>

## Plugin Service

### `PLUGINS_INDEX_URL`
Expand Down
12 changes: 12 additions & 0 deletions docs/self-hosting/environment-variables/basic.zh-CN.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ LobeChat 在部署时提供了一些额外的配置项,你可以使用环境

具体的内容可以参见 [特性标志](/zh/docs/self-hosting/advanced/feature-flags) 中的说明。

### `PROXY_URL`

- 类型:可选
- 描述:用于指定连接到外部服务的代理 URL。该变量的值在不同的部署环境中应该有所不同。
- 默认值:-
- 示例:`http://127.0.0.1:7890``socks5://localhost:7891`


<Callout type="info">
`Docker Desktop``Windows ``macOS `上走的是虚拟机方案,如果是 `localhost` / `127.0.0.1` 是走到自身容器的 `localhost`,此时请尝试用 `host.docker.internal` 替代 `localhost`
</Callout>

## 插件服务

### `PLUGINS_INDEX_URL`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"pull": "git pull",
"release": "semantic-release",
"self-hosting:docker": "docker build -t lobe-chat:local .",
"self-hosting:docker-cn": "docker build -t lobe-chat:local --build-arg USE_NPM_CN_MIRROR=true .",
"self-hosting:docker-cn": "docker build -t lobe-chat:local --build-arg USE_CN_MIRROR=true .",
"start": "next start -p 3210",
"stylelint": "stylelint \"src/**/*.{js,jsx,ts,tsx}\" --fix",
"test": "npm run test-app && npm run test-server",
Expand Down

0 comments on commit 920de7c

Please sign in to comment.