用docker搭建python項目運行環境

Docker Hub鏡像加速器

  1. 安裝docker: curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
  2. 參考這篇註冊阿里開發者,拿到專屬加速器地址,相似於:https://xxxx.mirror.aliyuncs.com
  3. 參考Docker加速器下面的說明配置並重啓docker,我是ubuntu 16.04,以下:
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/mirror.conf <<-'EOF'
[Service]
ExecStart=/usr/bin/docker daemon -H fd:// --registry-mirror=https://xxxx.mirror.aliyuncs.com
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

寫Dockerfile

# image for portfolio
FROM ubuntu:16.04
MAINTAINER wwy version: 0.1

ENV LD_LIBRARY_PATH=/usr/local/lib

COPY ./sources.list /etc/apt/
RUN depends='make r-base r-base-dev' &&\
    apt-get update &&\
    apt-get install -y $depends
    
#anaconda
COPY ./Anaconda2-4.2.0-Linux-x86_64.sh /tmp/
RUN bash /tmp/Anaconda2-4.2.0-Linux-x86_64.sh -b
ENV PATH /root/anaconda2/bin:$PATH

#ta-lib
COPY ./ta-lib-0.4.0-src.tar.gz /tmp/
RUN cd /tmp &&\
    tar -xzf ta-lib-0.4.0-src.tar.gz &&\
    cd /tmp/ta-lib &&\
    ./configure &&\
    make &&\
    make install
    
#pip
COPY ./pip.conf /root/.config/pip/
COPY ./SuiteSparse-4.5.3.tar.gz /tmp/
ENV CVXOPT_SUITESPARSE_SRC_DIR=/tmp/SuiteSparse
RUN cd /tmp &&\
    tar xzf SuiteSparse-4.5.3.tar.gz &&\
    pip install celery flasgger flask rpy2 arch pymongo grpc protobuf cvxopt ta-lib grpcio seaborn
    
#Qi4Trade
COPY Qi4Trade /tmp/Qi4Trade
RUN cd /tmp/Qi4Trade/trunk &&\
    python setup.py install
    
#SITxuk
COPY SITxuk /tmp/SITxuk
RUN cd /tmp &&\
    R CMD build SITxuk &&\
    R CMD INSTALL SITxuk_0.1.0.tar.gz
    
#ndparser
COPY ndparser.so /root/anaconda2/lib/python2.7/site-packages/
COPY ndparser-1.0-py2.7.egg-info /root/anaconda2/lib/python2.7/site-packages/

#clear
RUN apt-get purge -y --auto-remove make &&\
    apt-get clean &&\
    rm -rf /tmp/* &&\
    rm -rf /var/lib/apt/lists/*

#porfolio-web
COPY portfolio-web /root/portfolio-web
COPY libgomp.so.1.0.0 /root/anaconda2/lib/
ENV FLASK_SETTINGS=/root/portfolio-web/trunk/api/dev
ENV PYTHONPATH=/root/portfolio-web/trunk
WORKDIR /root/portfolio-web/trunk
EXPOSE 8686
CMD ["python","api/server.py"]

# Add Tini. Tini operates as a process subreaper for jupyter. This prevents kernel crashes.
ENV TINI_VERSION v0.6.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/bin/tini
RUN chmod +x /usr/bin/tini
ENTRYPOINT ["/usr/bin/tini", "--"]
EXPOSE 8888
CMD ["jupyter","notebook","--port=8888","--no-browser","--ip=0.0.0.0"]

Dockerfile要注意的點

  1. 用&&\把全部命令串起來在一個Run裏執行,避免產生太多層
  2. 能夠把/etc/apt/sources.list拷過去,加速apt-get install; 把pip.conf拷到/root/.config/pip/pip.conf,指向douban的源,加速pip install
  3. 全部安裝都是silent mode, 包括: apt-get -y、anaconda -b等
  4. 報pip not found: anaconda不會自動把bin加入PATH,須要手工加:ENV PATH /root/anaconda2/bin:$PATH
  5. 每一個階段的命令寫在一塊兒,從COPY+ENV+RUN,這樣萬一下面的COPY改了,還能重用之前的cache
  6. COPY要這麼寫:COPY ta-lib /tmp/ta-lib,若是隻是COPY ta-lib /tmp/只會把ta-lib裏面的內容拷過去
  7. 國內的鏡像源有時apt-get install的時候會報"Hash Sum mismatch"的錯誤,不覆蓋/etc/apt/sources.list,用官方的就好
  8. 不少server好比nginx、jupyter都默認監聽localhost,要改爲0.0.0.0,才能在容器外訪問

build + export image

docker build -t test:v0.1 .
docker run -i -p 8686:8686 test:v0.1
docker save test:v0.1 | gzip > portfolio.tar.zip
//複製到其餘機器上,導入
docker load -i portfolio.tar.zip
相關文章
相關標籤/搜索