本文共 2908 字,大约阅读时间需要 9 分钟。
因为比赛原因,需要上传docker
镜像文件,没办法,又自学了一把docker
,研究了一天,总算大概知道是怎么一回事了,其实说得明白点就是通过写一个Dockerfile
把你的程序里需要用到的所有依赖项封装起来,此外再把你的程序也一并封装进去,就这些东西构成一个image
镜像文件,然后把这个镜像文件push
到云端,其他人就可以用了,就这么一回事,难点在于怎么写这个Dockerfile
,这个挺废时间的,因为被指定使用CentOS
构建环境,其实如果不是为了比赛,可以选择Ubuntu
操作环境下进行的。
Dockerfile
代码: FROM nvidia/cuda:8.0-cudnn6-devel-centos7# You can use alternative base mirror from https://hub.docker.com/r/nvidia/cudaMAINTAINER Will_Ye# 安装你程序需要用到的所有依赖项,如Python,numpy,tensorflow等等RUN set -ex \ && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make initscripts \ && wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz \ && tar -zxvf Python-3.5.0.tgz \ && cd Python-3.5.0 \ && ./configure prefix=/usr/local/python3 \ && make \ && make install \ && make clean \ && cd .. \ && rm -rf /Python-3.5.0* \ && yum install -y epel-release \ && yum install -y python-pipRUN set -ex \ # 备份旧版本python && mv /usr/bin/python /usr/bin/python27 \ && mv /usr/bin/pip /usr/bin/pip-python2.7 \ # 配置默认为python3 && ln -s /usr/local/python3/bin/pip3 /usr/bin/pip \ && pip install scipy \ #如果要用到scipy这个包,就需要用python2.7安装,python3.5安装会失败 && ln -s /usr/local/python3/bin/python3.5 /usr/bin/python \# 修复因修改python版本导致yum失效问题RUN set -ex \ && sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/bin/yum \ && sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/libexec/urlgrabber-ext-down \ && yum install -y deltarpmRUN yum -y install python-devel scipyRUN pip install --upgrade pipRUN pip install matplotlibRUN pip install --upgrade setuptoolsRUN pip install tensorflow-gpuRUN pip install Pillow#RUN pip install moviepyRUN pip install kerasRUN pip install cmake#安装opencv的这一段有点问题,我还没解决,因为后来发现写的这个版本程序不需要用到cv2,暂时搁置,如果之后解决了,再重新补充,问题出在unzip上,可能要补充安装解压文件的工具就行了,还没试#RUN set -ex \# &&wget https://github.com/opencv/opencv/archive/2.4.13.zip \# &&unzip opencv-2.4.13.zip \# &&cd opencv-2.4.13 \# &&cmake CMakeLists.txt \# &&mkdir build \# &&cd build \# &&cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_FFMPEG=OFF -D CMAKE_INSTALL_PREFIX=/usr/local .. \# && make \# && make install \RUN pip install waveRUN pip install scikit-image# Add your project file#注意这里的路径是相对路径,前面的是本地文件,后面的参数是目标存储路径,指镜像中ADD ./competition/application.py /data/application.pyADD ./competition/model_weights20190430.h5 /data/model_weights20190430.h5# Define the entry process command#这个CMD操作只能有一个,要注意这点CMD python /data/application.py
说一下其中里面一些语法的用途,一边看一边收集来的,整理一下:
RUN set -ex \
的作用:set
是shell
的一个命令,因为shell
的执行的过程中,如果有某个出错了,也会继续往下执行,set -ex
作用就是,当下面的命令执行出错后,就退出执行,不在继续往下执行,因为构建python环境很重要,有些问题报了可能被输出的信息一下就刷上去了,没留意到,后面就各种奇怪的报错。mkdir -p /urs/local/python3.5
中的-p
:平时可能大家也会用到,在构建Dockerfile
特别方便,少写不少代码,因为不加这个-p
是不能在没有上一级文件夹的前提下创建目标文件/文件夹的,有了这个就会把上一级的文件夹也一并创建了。ADD
和COPY
的区别:区别很简单,ADD
强大一点,可以通过链接下载文件到指定路径,COPY
就不行,只能复制本地的文件到指定路径。转载地址:http://cxgl.baihongyu.com/