# 多用户 Docker 环境下 PyPi 源按需加速

### 理论分析加速比

\begin{aligned} &\left\{ \begin{aligned} t_1&=\frac{nm}{t}\\ t_2&=\frac{(n-1)m}{d}+\frac{m}{t} \end{aligned} \right. &\\ \frac{t_1-t_2}{t_1}&=\frac{\frac{nm}{t}-\frac{(n-1)m}{d}-\frac{m}{t}}{\frac{nm}{t}}\\ &=\frac{nm-\frac{(n-1)mt}{d}-m}{nm}\\ &=1-\frac{(n-1)t}{nd}-\frac{1}{n} \end{aligned}


\begin{aligned} =&1-\frac{5(n-1)}{200n}-\frac{1}{n}\\ =&1-\frac{1}{40}+\frac{1}{40n}-\frac{1}{n}\\ =&\frac{39}{40}-\frac{39}{40n}\\ =&\frac{39n-39}{40n} \end{aligned}


\begin{aligned} n&=2, &\frac{t_1-t_2}{t_1}&=48.75\%\\ n&=3, &\frac{t_1-t_2}{t_1}&=65.0\%\\ n&=5, &\frac{t_1-t_2}{t_1}&=78.0\%\\ n&=10, &\frac{t_1-t_2}{t_1}&=87.75\% \end{aligned}


### JupyterHub的例子

git clone https://github.com/jupyterhub/jupyterhub-deploy-docker
cd jupyterhub-deploy-docker
git clone -b local https://github.com/BlueCocoa/pip-cache


# Copyright (c) Jupyter Development Team.

# JupyterHub docker-compose configuration file
version: "2"

services:
pypicache:
image: pypicache
restart: always
build:
context: pip-cache
dockerfile: Dockerfile
args:
PYPI_MIRROR_URL: ${PYPI_MIRROR_URL} PYPI_PACKAGE_LOC:${PYPI_PACKAGE_LOC}
container_name: pypicache
volumes:
- "pypicache:${PYPI_CACHE_CONTAINER}" hub-db: image: postgres:9.5 container_name: jupyterhub-db restart: always environment: POSTGRES_DB:${POSTGRES_DB}
PGDATA: ${DB_VOLUME_CONTAINER} env_file: - secrets/postgres.env volumes: - "db:${DB_VOLUME_CONTAINER}"
hub:
depends_on:
- pypicache
- hub-db
build:
context: .
dockerfile: Dockerfile.jupyterhub
args:
JUPYTERHUB_VERSION: ${JUPYTERHUB_VERSION} restart: always image: jupyterhub container_name: jupyterhub volumes: # Bind Docker socket on the host so we can connect to the daemon from # within the container - "/var/run/docker.sock:/var/run/docker.sock:rw" # Bind Docker volume on host for JupyterHub database and cookie secrets - "data:${DATA_VOLUME_CONTAINER}"
ports:
- "443:443"
- pypicache
- hub-db
environment:
# All containers will join this network
DOCKER_NETWORK_NAME: ${DOCKER_NETWORK_NAME} # JupyterHub will spawn this Notebook image for users DOCKER_NOTEBOOK_IMAGE:${LOCAL_NOTEBOOK_IMAGE}
# Notebook directory inside user image
DOCKER_NOTEBOOK_DIR: ${DOCKER_NOTEBOOK_DIR} # Using this run command (optional) DOCKER_SPAWN_CMD:${DOCKER_SPAWN_CMD}
# Postgres db info
POSTGRES_DB: ${POSTGRES_DB} POSTGRES_HOST: hub-db env_file: - secrets/postgres.env - secrets/oauth.env command: > jupyterhub -f /srv/jupyterhub/jupyterhub_config.py volumes: data: external: name:${DATA_VOLUME_HOST}
db:
external:
name: ${DB_VOLUME_HOST} pypicache: external: name:${PYPI_CACHE_HOST}

networks:
default:
external:
name: \${DOCKER_NETWORK_NAME}


### 设置需要加速的镜像源

PYPI_CACHE_HOST=pypicache
PYPI_CACHE_CONTAINER=/srv/pypicache/data
PYPI_MIRROR_URL=https://pypi.tuna.tsinghua.edu.cn/simple
PYPI_PACKAGE_LOC=/packages


packages的相对路径是在上两层的packages下，也就是https://mirrors.ustc.edu.cn/pypi/web/keras/../../packages，将相对路径去掉之后，重新计算路径则是https://mirrors.ustc.edu.cn/pypi/web/packages，那么URI也就是/pypi/web/packages，所以 PYPI_PACKAGE_LOC 就写 /pypi/web/packages

mkdir -p ~/.pip && echo "[global]" > ~/.pip/pip.conf && \
echo "trusted-host=pypicache" >> ~/.pip/pip.conf && \
echo "index-url=http://pypicache/simple" >> ~/.pip/pip.conf


### Have fun and save time~

声明: 本文为0xBBC原创, 转载注明出处喵～

## One thought on “多用户 Docker 环境下 PyPi 源按需加速”

1. 发现做博客的人都是有钱人啊，都是吃苹果的