撇开docker,在ubuntu上面编译支持Python3的Caffe参考:让Caffe支持Python3
构建docker就是把这一系列编译命令放到Dockerfile中执行。不过,我们用预先编译好的boost_python3,把Anaconda的安装、Caffe的编译放到Dockerfile中。
一、步骤:
1、安装docker:sudo apt install docker.io
添加DNS , 在/etc/network/interfaces 中添加 dns-nameservers 8.8.8.8
,重启生效(这里先不用重启,后面再重启)。其他方法参考:Ubuntu下设置DNS的方法
2、添加用户到docker组(执行docker命令的用户)
sudo usermod -aG docker $USER
sudo reboot
3、编写Dockerfile,下面有示例
4、build:
docker build -t 名字 .
5、运行
docker run -d -p 5000:5000 -v /hostpath/to/logs:/home/app/logs 名字
6、查看运行的docker
查看所有启动的:docker ps -a
查看日志:docker logs --since 30m CONTAINER_ID , 参考https://www.jianshu.com/p/1eb1d1d3f25e
二、需要注意的是:
1、Dockerfile中每个RUN命令启动一个容器,启动容器的时候 .bashrc中的环境变量不生效,必须重新执行一遍 . "your username"/.bashrc
(. 是source, Dockerfile中找不到source)。
2、RUN和CMD等支持shell和exe格式的命令。执行二进制文件的时候可以用shell命令,但是如果的二进制文件不在默认的路径中,或者bash找不到这个文件,这时就不能用shell格式的命令,要用exe格式的命令。
3、COPY命令,带目录拷贝要这样COPY lib ./lib
Docker文档:https://yeasy.gitbooks.io/docker_practice/image/dockerfile/cmd.html
Dockerfile如下:
From bvlc/caffe:cpu
RUN sed -i 's/http:\/\/archive\.ubuntu\.com\/ubuntu\//http:\/\/mirrors\.aliyun\.com\/ubuntu\//g' /etc/apt/sources.list
RUN apt-get update && \
apt-get install -y --no-install-recommends \
libbz2-dev \
cpio \
build-essential \
cmake \
git \
wget \
ssh \
openssh-server \
numactl \
vim \
net-tools \
iputils-ping \
screen \
libmlx4-1 libmlx5-1 ibutils rdmacm-utils libibverbs1 ibverbs-utils perftest infiniband-diags \
openmpi-bin libopenmpi-dev \
ufw \
iptables \
libgflags-dev \
libgoogle-glog-dev \
libhdf5-serial-dev \
libleveldb-dev \
liblmdb-dev \
libopencv-dev \
libprotobuf-dev \
libsnappy-dev \
protobuf-compiler \
python-dev \
python-pip \
python-setuptools \
protobuf-compiler libhdf5-dev libgoogle-glog-dev libopenblas-dev libopencv-dev liblmdb-dev libleveldb-dev python-dev libsnappy-dev \
python-scipy && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV HOME=/home/app
WORKDIR $HOME
COPY boost-py365.tar.gz .
COPY caffe-master-py365.tar.gz .
COPY Anaconda3-2018.12-Linux-x86_64.sh .
COPY conf ./conf
COPY lib ./lib
COPY run.py .
COPY __init__.py .
RUN tar xzf boost-py365.tar.gz && \
rm -rf boost-py365.tar.gz
RUN tar xzf caffe-master-py365.tar.gz && \
rm -rf caffe-master.tar.gz
RUN sh -c '/bin/echo -e "\nyes\n\nyes\nno\n" | sh Anaconda3-2018.12-Linux-x86_64.sh'
RUN echo ". /home/app/anaconda3/etc/profile.d/conda.sh" >> $HOME/.bashrc
RUN . $HOME/.bashrc && \
conda create --name py365 python=3.6.5 && \
echo "conda activate py365" >> $HOME/.bashrc
ENV C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/boost/include:$HOME/anaconda3/envs/py365/include:$HOME/anaconda3/envs/py365/include/python3.6m \
C_PLUS_INCLUDE_PATH=$C_PLUS_INCLUDE_PATH:$HOME/boost/include:$HOME/anaconda3/envs/py365/include:$HOME/anaconda3/envs/py365/include/python3.6m \
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/boost/lib:$HOME/anaconda3/envs/py365/lib
WORKDIR $HOME/caffe-master
RUN . $HOME/.bashrc && \
make all -j4 && \
make pycaffe && \
make distribute && \
pip install numpy==1.14.5 opencv-python==3.4.3.18 scikit-image==0.14.2 protobuf==3.6.1 flask==1.0.2 gunicorn==19.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
ENV PYTHONPATH=$HOME/caffe-master/distribute/python:$PYTHONPATH \
PATH=$HOME/caffe-master/distribute/bin:$HOME/anaconda3/envs/py365/bin:$PATH \
C_INCLUDE_PATH=$HOME/caffe-master/distribute/include:$C_INCLUDE_PATH \
C_PLUS_INCLUDE_PATH=$HOME/caffe-master/distribute/include:$C_PLUS_INCLUDE_PATH \
LD_LIBRARY_PATH=$HOME/caffe-master/distribute/lib:$LD_LIBRARY_PATH
WORKDIR $HOME
CMD ["gunicorn", "-b", ":5000", "--access-logfile", "-", "--error-logfile", "-", "run:app"]