墨塵的模型部署3--nvidia-docker實現tensorflow serving單模型文件部署(三)

1.手動定製本地已經訓練模型的鏡像(使用可用於開發的鏡像dev)

1.1 下載centos鏡像,啓動容器,進入容器

# 下可用於後期開發的鏡像
[root@aiserver ~]# docker pull tensorflow/serving:latest-dev-gpu
# 啓動容器
[root@aiserver ~]# docker run -it -d --name serving-base tensorflow/serving:latest-dev-gpu
# 進入容器
[root@aiserver ~]# docker exec -it serving-base /bin/bash
[root@d72250ecaa5e /]# ifconfig
bash: ifconfig: command not found
  • /bin/bash: 指進入容器時執行的命令(command)

1.2. 配置容器內軟件包,開啓ssh服務及開機啓動配置

[root@d72250ecaa5e /]#  apt-get install openssh-server net-tools -y
[root@d72250ecaa5e /]# mkdir -pv /var/run/sshd 
# 進入'/var/run/sshd'文件夾  
[root@d72250ecaa5e /]# cat ./auto_sshd.sh 
#增加可執行權限
[root@d72250ecaa5e /]# chmod +x /auto_sshd.sh 

[root@d72250ecaa5e /]# echo "root:iloveworld" | chpasswd

生成ssh 主機dsa 密鑰(不然ssh 該容器時,會出現錯誤。)
遇到輸入Enter same passphrase again:時候,直接回車跳過

[root@d72250ecaa5e /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
[root@d72250ecaa5e /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

我們加一個history記錄的時間功能吧,這樣方便後期查看

echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile

1.3. 將訓練好的模型my model複製到名爲serving_base容器內

docker cp models/<my model> serving_base:/models/<my model>
#例子如下:
docker cp models/yolov3 serving_base:/models/yolov3

1.4. 打包成新鏡像,並驗證

普通打包鏡像

[root@aiserver ~]# docker commit --change "ENV MODEL_NAME yolov3" serving_base serving_base_image
sha256:6e3330b30dfff5f029f102874e54cfffffbc37dcf2a4eb7304c817148fbc944d
  • commit: 提交一個具有新配置的容器成爲鏡像,後面跟容器的名字serving_base 或者容器Id ,最後是生成新鏡像的名字serving_base_image

設置開機自啓動選項打包鏡像

[root@aiserver ~]# docker commit --change='CMD ["/auto_sshd.sh"]' -c "EXPOSE 22" serving_base serving_base_image
sha256:7bb4efd82c4ff1f241cbc57ee45aab1b05d214b1e9fcd51196696c67d480e70b

  • –change : 將後期使用此鏡像運行容器時的命令參數、開放的容器端口提前設置好。開啓的時候自動運行auto_sshd.sh腳本
  • -c “EXPOSE 22” :暴露22端口,默認也是暴露的
    驗證
# 由於上面已經設置了默認端口爲22,此處可以不設置
[root@aiserver ~]# docker run -d -it --name my_container serving_base_image
[root@aiserver ~]# docker ps -a
# 如果上面沒有設置22端口則執行如下指令使用8501端口:
[root@aiserver ~]# docker run -t --rm -p 8501:8501 serving_base_image
# 重開一個終端,驗證是否成功
[root@aiserver ~]# curl http://localhost:8501/v1/models/yolov3

進行ssh測試,先查看一下該容器的ip,之後ssh。ok

[root@aiserver ~]# docker exec -it my_container /bin/bash
[root@aiserver ~]# ssh [email protected]  //或者ssh 127.17.0.4
RSA key fingerprint is 87:88:07:12:ac:0a:90:28:10:e1:9e:eb:1f:d6:c9:9d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.4' (RSA) to the list of known hosts.
root@172.17.0.4's password: 
Last login: Tue Nov 29 16:00:49 2016 from gateway

[root@ec17e553d5c4 ~]# w   
 16:34:17 up 63 days,  7:49,  1 user,  load average: 0.00, 0.02, 0.05 USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    gateway          16:34    1.00s  0.00s  0.00s w 
[root@ec17e553d5c4 ~]# ping gateway
PING gateway (172.17.0.1) 56(84) bytes of data. 64 bytes from gateway (172.17.0.1): icmp_seq=1 ttl=64 time=0.048 ms

2.Dockerfile文件自動定製docker-tf-gpu鏡像

在這裏插入圖片描述

2.1 創建Dockerfile文件

[root@localhost ~]#  mkdir centos7-dockerfile
[root@localhost ~]#  cd centos7-dockerfile/
[root@localhost ~]#  cat Dockerfile 
# The dockerfile has Change add sshd services on Centos7.0
#centos7:latest image
FROM centos:latest

MAINTAINER Yifeng,http://www.cnblogs.com/hanyifeng

#Install sshd net-tools
RUN yum install openssh-server net-tools -y
RUN mkdir /var/run/sshd

#Set password for root
RUN echo 'root:iloveworld' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

#Set history record
ENV HISTTIMEFORMAT "%F %T  "

#Fix sshd service:Read from socket failed: Connection reset by peer?
RUN ssh-keygen -A

#Change timezone CST
RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#Open 22 port
EXPOSE 22

#Auto running sshd service
CMD ["/usr/sbin/sshd","-D"]

2.2 執行build 創建鏡像

執行Dockerfile

```python
docker build -t centos_new:tag .
  • -t: 指定鏡像名稱
  • .:末尾的 點標識“ .”標識dockerfile文件的路徑
  • :備註:RUN命令在 image 文件的構建階段執行,執行結果都會打包進入 image 文件;CMD命令則是在容器啓動後執行。另外,一個 Dockerfile 可以包含多個RUN命令,但是隻能有一個CMD命令。注意,指定了CMD命令以後,docker container run命令就不能附加命令了,否則它會覆蓋CMD命令。

2.3 查看鏡像列表,並創建容器,輸入密碼即可

[root@localhost centos7-dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_new latest 37244727ac6e About a minute ago 283MB
centos latest 1e1148e4cc2c 3 months ago 202MB
[root@localhost centos7-dockerfile]# docker run -it -d --name contain_new centos_new
d99be6d58a1cba6dbcd0d2cd2448a557faf14ee7897bafe1ef60b04875901cd3
[root@localhost centos7-dockerfile]# docker exec contain_new hostname -i
172.17.0.2
[root@localhost centos7-dockerfile]# ssh 127.17.0.2
root@127.17.0.2's password:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章