如何将深度学习中的项⽬打包成Dockerfile
如何将深度学习中的项⽬打包成镜像,并编写Dockerfile
背景
什么是Dockerfile
Dockerfile中命令
开始吧
⽬录结构
⾸先先看⼀下我的⽬录结构,Dockerfile就不⽤多bb了,后⾯会讲到;start.sh⽂件是我写的⼀个⾃启动的脚本,在容器制作成功后,进⼊容器的⼀个脚本;YOLO⽂件夹下为⼀个简单的⽬标识别代码,我也会将这个简单的代码以及⾃⼰针对⼤尺度遥感影像改进的⽬标识别代码上传⾄我的github,在这部分中mode_data保存yolov3中的相关信息,包括训练好的模型、预测种类、锚等,不了解的可以去看⼀下yolov3的代码,requirements保存的是需要的库,test为测试的图⽚,yolo3为模型代码,yolo_test.py为测试代码。⼤体的⽬录结构介绍完毕。
├── Dockerfile
├── start.sh
└── YOLO
├── font
│├── f
│└── SIL Open
├── model_data
│├──
│├──
│├──
│├── SAR
││├──
││└── trained_weights_final.h5
│├── tiny_
│├── trained_weights_final.h5
│├──
│└── yolo_weights.h5
├──
├── result_oiltank
├── test
│├── 12.tif
│├── guigang.tif
│├── roi5.tif
│├── test2.tif
│├── test3.tif
│└── zhang.tif
├── yolo3
│├── __init__.py
│├── __init__.pyc
│├── model.py
│├── model.pyc
│├── __pycache__
││├── __init__.cpython-35.pyc
││├── __init__.cpython-36.pyc
││├── model.cpython-35.pyc
││├── model.cpython-36.pyc
││├── utils.cpython-35.pyc
││└── utils.cpython-36.pyc
│├── test.py
│├── utils.py
│└── utils.pyc
├── yolo_test.py
├── yolov3.cfg
└── l
Dockerfile
下⾯为Dockerfile,当然以下安装的库是我⾃⼰所需要的库,如果你需要其他库,⾃⾏添加。
1 #导⼊环境
2 FROM centos:latest
##标注信息
3 MAINTAINER Zhangym "zhangym@geovis"
4
5
6 #安装python
7
8 RUN set -ex \
9    && yum install -y wget tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make initscripts \
10    &&wget /ftp/python/3.6.8/Python-3. \
11    &&tar -zxvf Python-3. \
12    &&cd Python-3.6.8 \
13    && ./configure prefix=/usr/local/python3 \
14    &&make \
15    &&make install \
16    &&make clean \
17    &&cd.. \
18    &&rm -rf /Python-3.6.8* \
19    && yum install -y epel-release \
20    && yum install -y python-pip
21 #明明下的为python3.6,这⾥为什么会出现python2.7,我也不晓得,如果你在这⾥打印python -V 会出现python2.7版本
22 RUN set -ex \
23  # 备份旧版本python
24      &&mv /usr/bin/python /usr/bin/python27 \
25      &&mv /usr/bin/pip /usr/bin/pip-python2.7 \
26      # 配置默认为python3
27      &&ln -s /usr/local/python3/bin/pip3 /usr/bin/pip \
28      && pip install scipy -i pypi.douban/simple/ --trusted-host pypi.douban \
29  #如果要⽤到scipy这个包,就需要⽤python2.7安装,python3.5安装会失败
30      &&ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
31
32  ##修复python版本改变,yum问题
33 RUN set -ex \
34      &&sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/bin/yum \
35      &&sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/libexec/urlgrabber-ext-down \
36      && yum install -y deltarpm
37
38 ##配置清华源
39 RUN pip config set global.index-url pypi.tuna.tsinghua.edu/simple
40
41 #在这⾥打印出python版本,正确显⽰python3.6
42 RUN python -V
43 #RUN yum -y install python-devel scipy
44
docker打包镜像45
46 ##查看是否安装版本
47 #RUN python -V
48 #其实我的Dockerfile仅仅写的是以下3⾏⽽已以上都是在安装环境⽽已
49 #定义容器中的⼯作⽬录
50 ENV CODE_DIR=/YOLO
51 RUN mkdir -p $CODE_DIR/yolov3_oiltank/
52 COPY ./YOLO $CODE_DIR/yolov3_oiltank/
53 RUN python -V
54
55
56
57
58 RUN python -m pip install --upgrade pip
59 #RUN pip install --upgrade setuptools
60 RUN pip install keras tensorflow-gpu opencv-python -i pypi.douban/simple/ --trusted-host pypi.douban
61 RUN pip install -r $CODE_DIR/yolov3_
62
63
64 #ADD ./YOLO /data/yolov3_oiltank/
65
66 #CMD执⾏命令
67 COPY ./start.sh /usr/bin/my-start.sh
68 RUN chmod +x /usr/bin/my-start.sh
69 CMD ["my-start.sh"]
下⾯为start.sh
1 #!/bin/bash
2 cd /YOLO/yolov3_oiltank/
3 #nohup python yolo_test.py >>./watch.out &
4 python yolo_test.py
5
6 sleeptime=1000
7 while((1));do
8  now=`date'+%Y-%m-%d %H:%M:%S'`
9  echo$now>>./watch.out
10  sleep$sleeptime
11 done
下⾯开始运⾏Dockerfile
下⾯为执⾏docker build 的命令
docker build -t yolov3:v1 .
当正确执⾏后,会出现以下情况
Sending build context to Docker daemon  779.2MB
Step 1/17 : FROM centos:latest
---> 67fa590cfc1c
Step 2/17 : MAINTAINER Zhangym "zhangym@geovis"
---> Using cache
---> d777e5310598
Step 3/17 : 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 /ftp/python/3.6.8/Python-3.    &&tar -zxvf Python-3.    &&cd Python-3.6.8    && ./configur e prefix=/usr/local/python3    &&make&&make install&&make clean    &&cd..&&rm -rf /Python-3.6.8*    && yum install -y epel-release    && y um install -y python-pip
---> Using cache
---> ab4427be3969
Step 4/17 : RUN set -ex      &&mv /usr/bin/python /usr/bin/python27      &&mv /usr/bin/pip /usr/bin/pip-python2.7      &&ln -s /usr/local/python3/bin/pip3 /usr /bin/pip      && pip install scipy -i pypi.douban/simple/ --trusted-host pypi.douban      &&ln -s /usr/local/python3/bin/python3.6 /usr/bin/python  ---> Using cache
---> 21d37895a71d
Step 5/17 : 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 deltarpm
---> Using cache
---> c8802946d0f2
Step 6/17 : RUN pip config set global.index-url pypi.tuna.tsinghua.edu/simple
---> Using cache
---> 76e2c2f0db95
Step 7/17 : RUN python -V
---> Using cache
---> 308cc6360dec
Step 8/17 : ENV CODE_DIR=/YOLO
---> Using cache
---> 72cfcfca3a56
Step 9/17 : RUN mkdir -p $CODE_DIR/yolov3_oiltank/
---> Using cache
---> b486e1049ace
Step 10/17 : COPY ./YOLO $CODE_DIR/yolov3_oiltank/
---> Using cache
-
--> 95b637f93203
Step 11/17 : RUN python -V
---> Using cache
---> 0c66c6937f2f
Step 12/17 : RUN python -m pip install --upgrade pip
---> Using cache
---> fbbe28f67a18
Step 13/17 : RUN pip install keras tensorflow-gpu opencv-python -i pypi.douban/simple/ --trusted-host pypi.douban
---> Using cache
---> 9a4c82746c10
Step 14/17 : RUN pip install -r $CODE_DIR/yolov3_
---> Using cache
---> c36cb4e77362
Step 15/17 : COPY ./start.sh /usr/bin/my-start.sh
---> Using cache
---> b5b61e0dbba2
Step 16/17 : RUN chmod +x /usr/bin/my-start.sh
---> Using cache
---> b5a51ec26b91
Step 17/17 : CMD ["my-start.sh"]
---> Running in e1ae42e95671
Removing intermediate container e1ae42e95671
-
--> 243e68ee08af
Successfully built 243e68ee08af
Successfully ivis.io/zhangym/yolov3_oiltank:v1
进⼊容器
先看⼀下你的镜像是否已经在了

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。