Docker build-容器构建加速攻略

容器构建时须要下载多种软件,每每这是很是耗时间的。hub.docker.com原本就慢,尤为是遇到存放在gcr.io/aws等上面的模块就挂了,pip安装python模块是也较慢,conda的下载更是如蜗牛。node

加快容器构建时的下载速度,有多种方法:python

一、放在“外面的服务器”构建,而后传送到aliyun等镜像,下载速度就会快不少不少。git

二、添加proxy和pip、conda的镜像。以下是给jupyterhub环境下使用构建的一个singleuser镜像。github

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
FROM jupyter/all-spark-notebook:5811dcb711ba

LABEL maintainer="Databook Project,https://github.com/databooks<openthings@163.com>"

USER root

# ====================================================================
# Add proxy, using --build-arg "HTTP_PROXY=http://192.168.199.99:9999"

ENV HTTP_PROXY ${HTTP_PROXY}
ENV HTTPS_PROXY ${HTTP_PROXY}
ENV http_proxy ${HTTP_PROXY}
ENV https_proxy ${HTTP_PROXY}

#Add conda install mirror:

RUN echo $http_proxy && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ && \
    conda config --set show_channel_urls yes

#Add pip install mirror:

RUN echo "[global] \
index-url = http://pypi.tuna.tsinghua/simple \
trusted-host = \
    pypi.tuna.tsinghua \
timeout = 120 \
" > /etc/pip.conf
# ====================================================================

# ====================================================================
USER $NB_UID

RUN pip install --upgrade pip 
RUN pip install bs4 && \
    pip install lxml && \
    pip install ipyleaflet && \
    pip install py4j && \
    pip install pyspark && \
    pip install mlflow && \
    pip install airflow && \
    pip install tushare

RUN conda update -n base conda
RUN conda install -y -c conda-forge nodejs=8.10.0 && \
    conda install -y -c conda-forge tensorflow=1.8.0 && \
    jupyter labextension install jupyter-leaflet

# ====================================================================
ENV HTTP_PROXY ""
ENV HTTPS_PROXY ""
ENV http_proxy ""
ENV https_proxy ""
# ====================================================================

注意:docker

  • 注意要加到docker里面,在宿主机设置没有用的,由于docker build是在独立的容器中运行的。
  • build时使用docker build --build-arg "HTTP_PROXY=http://192.168.199.99:9999"将proxy参数传进去,结束时将http_proxy等参数清除。这样能够避免将proxy信息写入镜像。注意改为本身的服务地址。
  • 这里使用了清华的pip和conda镜像,也能够换成其它的。参考下面的信息:

更多的参考:Databook-数据之书 服务器

相关文章
相关标签/搜索