caffe docker镜像使用及制作

1、给系统安装docker

2、下载caffe 的镜像
caffe 镜像的dockerfile,基于ubuntu16.04镜像

FROM ubuntu:16.04
LABEL maintainer [email protected]

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        git \
        wget \
        libatlas-base-dev \
        libboost-all-dev \
        libgflags-dev \
        libgoogle-glog-dev \
        libhdf5-serial-dev \
        libleveldb-dev \
        liblmdb-dev \
        libopencv-dev \
        libprotobuf-dev \
        libsnappy-dev \
        protobuf-compiler \
        python-dev \
        python-numpy \
        python-pip \
        python-setuptools \
        python-scipy && \
    rm -rf /var/lib/apt/lists/*

ENV CAFFE_ROOT=/opt/caffe
WORKDIR $CAFFE_ROOT

# FIXME: use ARG instead of ENV once DockerHub supports this
# https://github.com/docker/hub-feedback/issues/460
ENV CLONE_TAG=1.0

RUN git clone -b ${CLONE_TAG} --depth 1 https://github.com/BVLC/caffe.git . && \
    pip install --upgrade pip && \
    cd python && for req in $(cat requirements.txt) pydot; do pip install $req; done && cd .. && \
    mkdir build && cd build && \
    cmake -DCPU_ONLY=1 .. && \
    make -j"$(nproc)"

ENV PYCAFFE_ROOT $CAFFE_ROOT/python
ENV PYTHONPATH $PYCAFFE_ROOT:$PYTHONPATH
ENV PATH $CAFFE_ROOT/build/tools:$PYCAFFE_ROOT:$PATH
RUN echo "$CAFFE_ROOT/build/lib" >> /etc/ld.so.conf.d/caffe.conf && ldconfig

WORKDIR /workspace

# 从 docker hub仓库pull caffe 的镜像
docker pull bvlc/caffe:cpu

3、运行caffe 镜像,并且启动bash

docker run -it --name test bvlc/caffe:cpu bash

docker run -it -p 7000:7000 --rm --name test docker_image_id

4、自带的caffe镜像基本上没有什么工具,因此如果需要可以安装。
首先需要设计软件源,因为caffe镜像自带的软件源是国外的,国内无法访问。

mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.163.com/debian/ jessie main non-frzee contrib" >/etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list

#更新安装源
apt-get update 

1.修改dns 
sudo vi /etc/resolv.conf 
在里面插入: 
nameserver 8.8.8.8 
nameserver 8.8.4.4

5、安装vim

apt-get install  vim

6、安装一些必要的包,测试单独安装能够正常安装,然后在根据docker file 进行构建镜像。

pip install -U pip 
pip install -U scipy  --ignore-installed scipy
pip install -U scikit-learn 
pip install -U matplotlib 
pip install -U jupyter 
pip install -U lmdb 
pip install -U python-mnist
pip install --upgrade scikit-image

7、容器启动,以及启动jumpter notebook

#commit 以test为name的容器
docker commit test caffe_add_jumpyter:v1
#启动刚才提交的镜像,并且挂在目录,挂在的目录必须存在雨docker,以及本地目录需要使用全路径
#挂在目录  -v [宿主机目录(本地目录)]:[docker 容器目录]  不存在会自动创建
docker run -it -v /Users/gpf/Documents/tools/docker/notebooks:/caffe/notebook -p 8888:8888 --rm --name caffe_jumpyter caffe_add_jumpyter:v1 /bin/bash

# 启动jumpter notebook
jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token='science'  --notebook-dir=/caffe/notebook/

8、在镜像里编译安装opencv2.4.13.6

OpenCV 官网下载源码, 建议选择2.4.13或者3.2.0版本
这里有下载好的源码,在映射的文件下
cp -r /caffe/notebook/opencv-2.4.13.6  /opt/
mkdir /opt/opencv2
cd /caffe/notebook/opencv-2.4.13.6

mkdir build

cd build

cmake -DCMAKE_INSTALL_PREFIX=/opt/opencv2/ -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF ..

make -j8

make install

编译成功后,配置环境变量

vim ~/.zshrc (我用的是zsh)

添加
export OpenCV_DIR="/opt/opencv_2.4.13.6"
export PATH="$OpenCV_DIR/bin":$PATH
export LD_LIBRARY_PATH="$OpenCV_DIR/lib":$LD_LIBRARY_PATH
export PKG_CONFIG_PATH="$OpenCV_DIR/lib/pkgconfig:${PKG_CONFIG_PATH}"

# 将cv2.so 的路径加入pythonpath路径中,否则会出现找不到cv2 module
export PYTHONPATH=/opt/opencv2/lib/python2.7/dist-packages:$PYTHONPATH

在制作镜像时,要尽量保证镜像是轻量的,因此把不必要的文件都删除,比如opencv编译的中间文件,都删除
rm -rf   /opt/opencv-2.4.13.6/

8、docker file

FROM bvlc/caffe_revise_source:v1

RUN apt-get clean && apt-get install -y --allow-unauthenticated vim\
    && pip install -U pip\
    && pip install -U scipy  --ignore-installed scipy \
    && pip install -U scikit-learn \
    && pip install -U matplotlib \
    && pip install -U jupyter \
    && pip install -U lmdb \
    && pip install -U python-mnist \
    && pip install --upgrade scikit-image

COPY notebooks/opencv-2.4.13.6 /opt/opencv-2.4.13.6

WORKDIR /opt

RUN mkdir opencv2 \
    && cd opencv-2.4.13.6 \
    && mkdir build \
    && cd build \
    && cmake -DCMAKE_INSTALL_PREFIX=/opt/opencv2/ -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF .. \
    && make -j8 \
    && make install \
    && rm -rf /opt/opencv-2.4.13.6/

ENV OpenCV_DIR="/opt/opencv_2.4.13.6"
ENV PATH="$OpenCV_DIR/bin":$PATH
ENV LD_LIBRARY_PATH="$OpenCV_DIR/lib":$LD_LIBRARY_PATH
ENV PKG_CONFIG_PATH="$OpenCV_DIR/lib/pkgconfig:${PKG_CONFIG_PATH}"
ENV PYTHONPATH=/opt/opencv2/lib/python2.7/dist-packages:$PYTHONPATH

# Set the working directory to /caffe
WORKDIR /caffe
VOLUME ["/caffe/data", "/caffe/notebooks"]
# Expose port 8888 (jupyter uses)
EXPOSE 8888
CMD jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token='science'

注:COPY命令:是在上下文环境中的,简单来说就是需要复制的文件必须在上下文环境中,也就是在dockerfile同样的目录下,使用相对路径就可以。

9、构建docker image时,删除所有的无用的文件,与环境运行无关的文件皆可删除

#清楚pip 的缓存文件
~/.cache/pip
rm -rf  ~/.cache/pip
#
rm -rf /var/lib/apt/lists/*

你可能感兴趣的:(caffe docker镜像使用及制作)