Docker 使用Dockerfile构建镜像

来源:本站原创 Docker 超过105 views围观 0条评论

编写Dockerfile

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

——–开始

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

#Maintainer
MAINTAINER jeff jeff@jeff.com

#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"]

———-结速

[root@node1 nginx]# pwd
/opt/dockerfiles/nginx
[root@node1 nginx]# mv nginx_v3 Dockerfile
[root@node1 nginx]#
[root@node1 nginx]#
[root@node1 nginx]# ll
total 4
-rw-r–r–. 1 root root 347 Jan 13 02:59 Dockerfile
[root@node1 nginx]# vim Dockerfile
[root@node1 nginx]#
[root@node1 nginx]#
[root@node1 nginx]#
[root@node1 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 jeff@jeff.com
—> 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 jeff@jeff.com

#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"]

——–执行验证————
[root@node1 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 jeff@jeff.com
—> 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
[root@node1 ~]# cd /opt/dockerfiles/nginx/
[root@node1 nginx]# echo "jeff" > jeff.html   ——增加文件
[root@node1 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
[root@node1 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 jeff@jeff.com
—> 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        ——-成功
[root@node1 nginx]#

验证
[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jeff/nginx_1        v5                  ad7465e37bd8        2 minutes ago       403.1 MB

启动构建的镜像
注意:需要对应EXPOSE里面的端口
[root@node1 ~]# docker run -d -p 88:80  jeff/nginx_1:v6  ——88:80是我后期改过的.按原文件是88:880
b9014e39c1b03a762089a68cc56f0e38cf141b151442e41433ad56e558ecda68

http验证
http://192.168.142.128:88/jeff.html  看是否能打开

——–重启构建新镜像——–
[root@node1 ~]# vim /opt/dockerfiles/nginx/
Dockerfile  jeff.html  
[root@node1 ~]# vim /opt/dockerfiles/nginx/jeff.html    —修改内容为V7
[root@node1 ~]# docker build -t jeff/nginx_1:v7 /opt/dockerfiles/nginx/

构建一次后下面的会很快
[root@node1 ~]# vim /opt/dockerfiles/nginx/
Dockerfile  jeff.html  
[root@node1 ~]# vim /opt/dockerfiles/nginx/jeff.html
[root@node1 ~]# 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 jeff@jeff.com
—> 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
[root@node1 ~]#

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

启动构建的镜像
[root@node1 ~]# docker run -d -p 89:880  jeff/nginx_1:v7
412141f05616532b11506706aa1b1a0e955e185dd8bf7965e290caa0c1de8b5a
[root@node1 ~]#

验证端口
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        
[root@node1 ~]#

http://192.168.142.128:89/ 无法打开

所有需要重新理解EXPOSE 是啥意思
——–注意——–
本质上说,EXPOSE或者–expose只是为其他命令提供所需信息的元数据,意思是没毛用。

重新进行测试

[root@node1 ~]# docker run -d -p 89:80  jeff/nginx_1:v7
2bc6b042cfbc31219f2716005f7db223dc01af98a630d86eb2b2b41634ebc375

[root@node1 ~]# curl http://1291.68.142.128:89/jeff.html

^C
[root@node1 ~]# curl http://192.68.142.128:89/jeff.html
^C
[root@node1 ~]# curl http://192.168.142.128:89/jeff.html
jeff v7
[root@node1 ~]#  成功

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=3682转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点

暂时还木有人评论,坐等沙发!
发表评论

快捷键:Ctrl+Enter