ConnectTimeoutError while running 'pip install' via docker-compose

Viewed 1484

I'm new to docker and currently trying to build an image for my Django project. Here's my Dockerfile:

FROM python:3.8.5-alpine
WORKDIR /my_project

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

COPY . .

When I run docker-compose build, execution breaks at the second pip command with the following error;

WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f2b21bd1610>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/asgiref/

Some Context:

OS: Ubuntu 20.04.1 LTS Kernel: Linux 5.8.0-34-generic

docker --version Docker version 20.10.2, build 2291f61

docker-compose --version docker-compose version 1.27.4, build 40524192

I have gone through a lot of similar questions online but none of their corresponding solutions work for me. I'll be more than glad to share any other info needed to assist in troubleshooting.

1 Answers

Sound there is some issue with the network connectivity while build the docker container. Use Host as an network inside compose file to resolve the issue.

version: '3.4'
services:
  django_image:
    build:
      context: .
      network: host

Give it a try and it will solve the issue.

Related