You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker-Compose构建Django镜像时执行pip install出现ConnectTimeoutError问题求助

Fixing Pip Timeout Issues in Django Docker Build

Hey there! I know how frustrating it is to hit a timeout when trying to build your Docker image for a Django project—especially when you're just getting started with Docker. Let's walk through some practical fixes to get past that ConnectTimeoutError with pypi.org:

1. Switch to a Faster PyPI Mirror

The most common fix for this kind of timeout is using a regional PyPI mirror that's closer to your location, which reduces latency. Here's how to update your Dockerfile to use a trusted mirror:

Option A: Add the mirror directly to your pip install command

Modify your pip install line in the Dockerfile:

RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt

Option B: Set a global pip config for the image

Add these lines before your pip install steps to configure the mirror globally:

RUN mkdir -p /root/.pip
RUN echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple" > /root/.pip/pip.conf

2. Fix Docker's DNS Configuration

Sometimes Docker uses DNS servers that can't resolve pypi.org properly. Let's update Docker's daemon settings to use reliable public DNS servers like Google's:

  1. Create or edit /etc/docker/daemon.json on your Ubuntu host:
{
  "dns": ["8.8.8.8", "8.8.4.4"]
}
  1. Restart the Docker service to apply changes:
sudo systemctl restart docker
  1. Try rebuilding your image with docker-compose build again.

3. Try a Non-Alpine Python Image

Alpine images are lightweight but can sometimes have network or dependency quirks. Switching to the slim variant of the Python image (based on Debian) might resolve the timeout:

Update your Dockerfile's base image line:

FROM python:3.8.5-slim

You'll also need to add a few extra dependencies for pip to work properly with this image:

RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    python3-dev \
    && rm -rf /var/lib/apt/lists/*

4. Check Host Network & Proxy Settings

  • First, verify your Ubuntu host can reach pypi.org:
    ping pypi.org
    curl https://pypi.org/simple/asgiref/
    
    If these commands fail, your host has a network issue (like a firewall, corporate proxy, or DNS problem).
  • If you're behind a proxy, pass the proxy args during the build:
    docker-compose build --build-arg HTTP_PROXY=http://your-proxy-address:port --build-arg HTTPS_PROXY=http://your-proxy-address:port
    
    Or add proxy environment variables directly to your Dockerfile (just remember to remove them if you don't need them long-term):
    ENV HTTP_PROXY=http://your-proxy-address:port
    ENV HTTPS_PROXY=http://your-proxy-address:port
    

Start with the first fix (switching the PyPI mirror) since it's the quickest to test, then work your way down if needed. Let me know if any of these get your build working!

内容的提问来源于stack exchange,提问作者Otieno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:02:32