diff --git a/Dockerfile b/Dockerfile index 6be4d9f..dd1b410 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,43 @@ FROM registry.fedoraproject.org/fedora:43 +# Proxy configuration ARGs +ARG HTTP_PROXY="" +ARG HTTPS_PROXY="" +ARG SOCKS_PROXY="" + +# Set environment variables if proxies are provided +ENV http_proxy=$HTTP_PROXY +ENV https_proxy=$HTTPS_PROXY +ENV no_proxy="localhost,127.0.0.1,::1" + +# Configure DNF proxy if provided +RUN if [ -n "$HTTP_PROXY" ]; then \ + echo "proxy=$HTTP_PROXY" >> /etc/dnf/dnf.conf; \ + fi + # Copy proxychains configuration COPY proxychains4.conf /etc/proxychains.conf +# Update proxychains4.conf if SOCKS_PROXY is provided (format: host:port) +RUN if [ -n "$SOCKS_PROXY" ]; then \ + HOST=$(echo $SOCKS_PROXY | cut -d: -f1); \ + PORT=$(echo $SOCKS_PROXY | cut -d: -f2); \ + sed -i "s/socks5.*/socks5 $HOST $PORT/g" /etc/proxychains.conf; \ + else \ + # If no proxy, remove the hardcoded one to avoid proxychains errors + sed -i "s/^socks5.*/# socks5 127.0.0.1 9050/g" /etc/proxychains.conf; \ + fi + +# Define a proxy wrapper +RUN echo '#!/bin/bash' > /usr/local/bin/maybe_proxy && \ + echo 'if grep -q "^socks5 [0-9]" /etc/proxychains.conf; then proxychains "$@"; else "$@"; fi' >> /usr/local/bin/maybe_proxy && \ + chmod +x /usr/local/bin/maybe_proxy + # 1. System Base & Build Tools RUN dnf -y install --setopt=install_weak_deps=False --nodocs \ python3.13 python3.13-devel git rsync libatomic bash ca-certificates curl \ gcc gcc-c++ binutils make ffmpeg-free \ - cmake ninja-build aria2c tar xz vim nano proxychains \ + cmake ninja-build aria2c tar xz vim nano proxychains-ng \ libdrm-devel zlib-devel openssl-devel jq \ numactl-devel gperftools-libs procps-ng \ && dnf clean all && rm -rf /var/cache/dnf/* @@ -19,12 +49,12 @@ ARG GFX=gfx120X-all RUN set -euo pipefail; \ BASE="https://therock-nightly-tarball.s3.amazonaws.com"; \ PREFIX="therock-dist-linux-${GFX}-${ROCM_MAJOR_VER}"; \ - KEY="$(proxychains curl -s "${BASE}?list-type=2&prefix=${PREFIX}" \ + KEY="$(maybe_proxy curl -s "${BASE}?list-type=2&prefix=${PREFIX}" \ | tr '<' '\n' \ | grep -o "therock-dist-linux-${GFX}-${ROCM_MAJOR_VER}\..*\.tar\.gz" \ | sort -V | tail -n1)"; \ echo "Downloading Latest Tarball: ${KEY}"; \ - proxychains aria2c -x 16 -s 16 -j 16 --file-allocation=none "${BASE}/${KEY}" -o therock.tar.gz; \ + maybe_proxy aria2c -x 16 -s 16 -j 16 --file-allocation=none "${BASE}/${KEY}" -o therock.tar.gz; \ mkdir -p /opt/rocm; \ tar xzf therock.tar.gz -C /opt/rocm --strip-components=1; \ rm therock.tar.gz @@ -58,10 +88,10 @@ RUN printf 'source /opt/venv/bin/activate\n' > /etc/profile.d/venv.sh RUN python -m pip install --upgrade pip wheel packaging "setuptools<80.0.0" # 5. Install PyTorch (TheRock Nightly) -RUN proxychains python -m pip install \ +RUN maybe_proxy python -m pip install \ --index-url https://rocm.nightlies.amd.com/v2-staging/gfx120X-all/ \ --pre torch torchaudio torchvision && \ - proxychains python -m pip install pyyaml + maybe_proxy python -m pip install pyyaml # Flash-Attention WORKDIR /opt @@ -71,8 +101,10 @@ ENV HIP_PATH="/opt/rocm" ENV PATH="/opt/rocm/bin:/opt/rocm/llvm/bin:$PATH" ENV LD_LIBRARY_PATH="/opt/rocm/lib:/opt/rocm/lib64:/opt/rocm/llvm/lib" -RUN git config --global http.proxy socks5://192.168.0.11:1080 && \ - git config --global https.proxy socks5://192.168.0.11:1080 && \ +RUN if [ -n "$SOCKS_PROXY" ]; then \ + git config --global http.proxy "socks5://$SOCKS_PROXY" && \ + git config --global https.proxy "socks5://$SOCKS_PROXY"; \ + fi && \ git clone --recursive https://github.com/ROCm/flash-attention.git && \ cd flash-attention && \ git checkout main_perf && \ @@ -80,7 +112,7 @@ RUN git config --global http.proxy socks5://192.168.0.11:1080 && \ cd /opt && rm -rf /opt/flash-attention # 6. Clone vLLM -RUN proxychains git clone https://github.com/vllm-project/vllm.git /opt/vllm +RUN maybe_proxy git clone https://github.com/vllm-project/vllm.git /opt/vllm WORKDIR /opt/vllm # --- PATCHING --- @@ -127,7 +159,7 @@ RUN export HIP_DEVICE_LIB_PATH=$(find /opt/rocm -type d -name bitcode -print -qu # bitsandbytes WORKDIR /opt -RUN proxychains git clone -b rocm_enabled_multi_backend https://github.com/ROCm/bitsandbytes.git +RUN maybe_proxy git clone -b rocm_enabled_multi_backend https://github.com/ROCm/bitsandbytes.git WORKDIR /opt/bitsandbytes ENV HIP_PLATFORM="amd" diff --git a/build_and_run.sh b/build_and_run.sh index cbf5040..7ff7b29 100644 --- a/build_and_run.sh +++ b/build_and_run.sh @@ -82,6 +82,8 @@ show_usage() { echo " -m, --models DIR 指定模型目录路径" echo " -p, --port PORT 指定服务端口" echo " -b, --branch NAME 指定 Git 分支(默认:main)" + echo " --http-proxy URL 指定构建时的 HTTP 代理 (例如: http://192.168.0.10:7890)" + echo " --socks-proxy HOST:PORT 指定构建时的 SOCKS 代理 (例如: 192.168.0.10:1080)" echo " -h, --help 显示此帮助信息" echo "" echo "示例:" @@ -98,6 +100,8 @@ FORCE_BUILD=false STOP_ONLY=false DETACH=true CUSTOM_CONFIG="" +HTTP_PROXY_ARG="" +SOCKS_PROXY_ARG="" CUSTOM_MODELS="" CUSTOM_PORT="" @@ -107,6 +111,14 @@ while [[ $# -gt 0 ]]; do FORCE_BUILD=true shift ;; + --http-proxy) + HTTP_PROXY_ARG="$2" + shift 2 + ;; + --socks-proxy) + SOCKS_PROXY_ARG="$2" + shift 2 + ;; -s|--stop) STOP_ONLY=true shift @@ -265,7 +277,16 @@ build_image() { BUILD_START=$(date +%s) - if docker build --network=host --build-arg MAX_JOBS=32 -t "${IMAGE_NAME}:${IMAGE_TAG}" .; then + # 准备构建参数 + BUILD_ARGS="--network=host --build-arg MAX_JOBS=32" + if [ -n "$HTTP_PROXY_ARG" ]; then + BUILD_ARGS="${BUILD_ARGS} --build-arg HTTP_PROXY=${HTTP_PROXY_ARG} --build-arg HTTPS_PROXY=${HTTP_PROXY_ARG}" + fi + if [ -n "$SOCKS_PROXY_ARG" ]; then + BUILD_ARGS="${BUILD_ARGS} --build-arg SOCKS_PROXY=${SOCKS_PROXY_ARG}" + fi + + if docker build ${BUILD_ARGS} -t "${IMAGE_NAME}:${IMAGE_TAG}" .; then BUILD_END=$(date +%s) BUILD_TIME=$((BUILD_END - BUILD_START))