Docker 使用Dockerfile構建鏡像

來源:本站原創 Docker 超過421 views圍觀 0條評論

編寫Dockerfile

"/opt/dockerfiles/nginx/Dockerfile" 16L, 347C

——–開始

#this is docker file
#Version 1
#Author: Jeff
#Base image
FROM centos

#Maintainer
MAINTAINER jeff [email protected]

#Commands
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx
ADD index.html /usr/share/nginx/html/index.html
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]

———-結速

[[email protected] nginx]# pwd
/opt/dockerfiles/nginx
[[email protected] nginx]# mv nginx_v3 Dockerfile
[[email protected] nginx]#
[[email protected] nginx]#
[[email protected] nginx]# ll
total 4
-rw-r–r–. 1 root root 347 Jan 13 02:59 Dockerfile
[[email protected] nginx]# vim Dockerfile
[[email protected] nginx]#
[[email protected] nginx]#
[[email protected] nginx]#
[[email protected] nginx]# docker build -t jeff/nginx_1:v3 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM centos
—> ff426288ea90
Step 2 : MAINTAINER jeff [email protected]
—> Running in 1498cac44a24
—> 8676e2b6fd8f
Removing intermediate container 1498cac44a24
Step 3 : RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
—> Running in c0afc77f9850
warning: /var/tmp/rpm-tmp.MqtlDv: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Retrieving https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
Preparing…                          ########################################
Updating / installing…
epel-release-7-11                     ########################################
—> 27aab1031bd5
Removing intermediate container c0afc77f9850
Step 4 : RUN yum install -y nginx
—> Running in 61da26df94fa
Loaded plugins: fastestmirror, ovl

 

 

———以下是騷操作—————

#this is docker file
#Version 1
#Author: Jeff
#Base image
FROM centos

#Maintainer
MAINTAINER jeff [email protected]

#Commands
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx
ADD index.html /usr/share/nginx/html/index.html
               /usr/share/nginx/html/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80   ——證明是沒毛用
CMD ["nginx"]

——–執行驗證————
[[email protected] nginx]# docker build -t jeff/nginx_1:v3 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM centos
—> ff426288ea90
Step 2 : MAINTAINER jeff [email protected]
—> Running in 1498cac44a24
—> 8676e2b6fd8f
Removing intermediate container 1498cac44a24
Step 3 : RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
—> Running in c0afc77f9850
warning: /var/tmp/rpm-tmp.MqtlDv: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Retrieving https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
Preparing…                          ########################################
Updating / installing…
epel-release-7-11                     ########################################
—> 27aab1031bd5
Removing intermediate container c0afc77f9850
Step 4 : RUN yum install -y nginx
—> Running in 61da26df94fa
Loaded plugins: fastestmirror, ovl
http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/7/x86_64/repodata/673486a8042817cf7ce895d3d4e4f3fe25e12e41b63dce06f580d733673ea950-primary.sqlite.bz2: [Errno 12] Timeout on http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/7/x86_64/repodata/673486a8042817cf7ce895d3d4e4f3fe25e12e41b63dce06f580d733673ea950-primary.sqlite.bz2: (28, ‘Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds’)
Trying other mirror.

================================================================================
Package                         Arch       Version              Repository
                                                                           Size
================================================================================
Installing:
nginx                           x86_64     1:1.12.2-1.el7       epel     529 k

Transaction Summary
================================================================================
Install  1 Package (+54 Dependent packages)

Total download size: 18 M
Installed size: 55 M

  nginx.x86_64 1:1.12.2-1.el7                                                  
                              

Complete!
—> e4fac0822687
Removing intermediate container 61da26df94fa
Step 5 : ADD index.html /usr/share/nginx/html/index.html————-
lstat index.html: no such file or directory

妥妥的失敗

原因如下
Step 6 : ADD jeff.html /usr/share/nginx/html/ ——–原文件不存在,需要指定路徑即可解決———-
lstat jeff.html: no such file or directory
[[email protected] ~]# cd /opt/dockerfiles/nginx/
[[email protected] nginx]# echo "jeff" > jeff.html   ——增加文件
[[email protected] nginx]# ll
total 8
-rw-r–r–. 1 root root 337 Jan 13 04:03 Dockerfile
-rw-r–r–. 1 root root   5 Jan 13 04:05 jeff.html
[[email protected] nginx]# docker build -t jeff/nginx_1:v5 /opt/dockerfiles/nginx/
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM centos
—> ff426288ea90
Step 2 : MAINTAINER jeff [email protected]
—> Using cache
—> 8676e2b6fd8f
Step 3 : RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
—> Using cache
—> 27aab1031bd5
Step 4 : RUN yum install -y nginx
—> Using cache
—> e4fac0822687
Step 5 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
—> Using cache
—> ae261671bde6
Step 6 : ADD jeff.html /usr/share/nginx/html/
—> a2c0d52161b2
Removing intermediate container e80f83b386f2
Step 7 : EXPOSE 880
—> Running in ce08659e82c6
—> d4ffb8d7752d
Removing intermediate container ce08659e82c6
Step 8 : CMD nginx
—> Running in d280154efa46
—> ad7465e37bd8
Removing intermediate container d280154efa46
Successfully built ad7465e37bd8        ——-成功
[[email protected] nginx]#

驗證
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jeff/nginx_1        v5                  ad7465e37bd8        2 minutes ago       403.1 MB

啟動構建的鏡像
注意:需要對應EXPOSE里面的端口
[[email protected] ~]# docker run -d -p 88:80  jeff/nginx_1:v6  ——88:80是我后期改過的.按原文件是88:880
b9014e39c1b03a762089a68cc56f0e38cf141b151442e41433ad56e558ecda68

http驗證
http://192.168.142.128:88/jeff.html  看是否能打開

——–重啟構建新鏡像——–
[[email protected] ~]# vim /opt/dockerfiles/nginx/
Dockerfile  jeff.html  
[[email protected] ~]# vim /opt/dockerfiles/nginx/jeff.html    —修改內容為V7
[[email protected] ~]# docker build -t jeff/nginx_1:v7 /opt/dockerfiles/nginx/

構建一次后下面的會很快
[[email protected] ~]# vim /opt/dockerfiles/nginx/
Dockerfile  jeff.html  
[[email protected] ~]# vim /opt/dockerfiles/nginx/jeff.html
[[email protected] ~]# docker build -t jeff/nginx_1:v7 /opt/dockerfiles/nginx/
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM centos
—> ff426288ea90
Step 2 : MAINTAINER jeff [email protected]
—> Using cache
—> 8676e2b6fd8f
Step 3 : RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
—> Using cache
—> 27aab1031bd5
Step 4 : RUN yum install -y nginx
—> Using cache
—> e4fac0822687
Step 5 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
—> Using cache
—> ae261671bde6
Step 6 : ADD jeff.html /usr/share/nginx/html/
—> 217a4dc6b3a3
Removing intermediate container c4dc7388b839
Step 7 : EXPOSE 880   ————–注意這個端口
—> Running in 372336d45674
—> a6b10f5a95d5
Removing intermediate container 372336d45674
Step 8 : CMD nginx
—> Running in 9750e2726d6d
—> f4c2c76b4884
Removing intermediate container 9750e2726d6d
Successfully built f4c2c76b4884
[[email protected] ~]#

[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jeff/nginx_1        v7—成功                  f4c2c76b4884        50 seconds ago      403.1 MB

啟動構建的鏡像
[[email protected] ~]# docker run -d -p 89:880  jeff/nginx_1:v7
412141f05616532b11506706aa1b1a0e955e185dd8bf7965e290caa0c1de8b5a
[[email protected] ~]#

驗證端口
netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      906/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1007/master        
tcp6       0      0 :::82                   :::*                    LISTEN      21433/docker-proxy-
tcp6       0      0 :::22                   :::*                    LISTEN      906/sshd           
tcp6       0      0 :::88                   :::*                    LISTEN      26769/docker-proxy-
tcp6       0      0 :::89    —–正常        :::*                    LISTEN      27182/docker-proxy-
tcp6       0      0 ::1:25                  :::*                    LISTEN      1007/master        
[[email protected] ~]#

http://192.168.142.128:89/ 無法打開

所有需要重新理解EXPOSE 是啥意思
——–注意——–
本質上說,EXPOSE或者–expose只是為其他命令提供所需信息的元數據,意思是沒毛用。

重新進行測試

[[email protected] ~]# docker run -d -p 89:80  jeff/nginx_1:v7
2bc6b042cfbc31219f2716005f7db223dc01af98a630d86eb2b2b41634ebc375

[[email protected] ~]# curl http://1291.68.142.128:89/jeff.html

^C
[[email protected] ~]# curl http://192.68.142.128:89/jeff.html
^C
[[email protected] ~]# curl http://192.168.142.128:89/jeff.html
jeff v7
[[email protected] ~]#  成功

文章出自:CCIE那點事 http://www.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=3682轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
  • 相關文章
  • 為您推薦
  • 各種觀點
?
暫時還木有人評論,坐等沙發!
發表評論

您必須 [ 登錄 ] 才能發表留言!

?
?
萌宠夺宝游戏