Change pip Registry in Dockerfile


Default pip resigtry is desperately slow in China. That makes building python docker image a PITA. Luckily we can config Dockerfile with a different pip registry:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Dockerfile
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

...

# Upgrade pip then change pip registry
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

...

RUN pip install --no-cache-dir -r requirements.txt

--no-cache-dir will shrink the image size by disabling the cache. It’s a good practice to use it for building docker images.

If changing ubuntu source is needed:

1
2
3
4
# Dockerfile
RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list
&& sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list
&& apt-get update && apt-get upgrade