docker实战测试:构建自己的centos镜像
Docker Hub中的99%镜像都是从这个基础镜像过来的(FROM scratch),然后配置需要的软件和配置来进行构建
创建一个自己的centos
1,在home目录下创建dockerfile目录,并在dockerfile目录下编写脚本mydockerfile
[root@CentOs home]# mkdir dockerfile
[root@CentOs home]# cd dockerfile/
[root@CentOs dockerfile]# ls
[root@CentOs dockerfile]# vim mydockerfile
[root@CentOs dockerfile]#
2,脚本内容
FROM centos
MAINTAINER wz<xxxxx@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
3,通过这个文件构载镜像
docker build -f mydockerfile -t mycentos:0.1 .(. 注意不要忘记)
-f:dockerfile文件的文件路径
-t:镜像名:[版本号]
如果构载时报错::Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
请参考此文档解决:https://www.cnblogs.com/zypdbk/p/16038698.html
解决完报错,我们就会发现构载成功了!
4,测试运行
docker run -it mycentos:0.1
我们发现进入以后直接在刚才设置的工作路径下,官方的centos默认的工作路径是根目录
我们自己设置的ifconfig也可以用了!
同理:我们vim也可以使用!
5,我们可以列出本地镜像的变更历史
docker history 镜像id
[root@CentOs ~]# docker history 881437706017
IMAGE CREATED CREATED BY SIZE COMMENT
881437706017 About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
fe83cac19108 About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
7edbbbd873bd About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
0c67c3512376 About an hour ago /bin/sh -c #(nop) EXPOSE 80 0B
565879c4900e About an hour ago /bin/sh -c yum -y install net-tools 161MB
c22a94b9f3fa About an hour ago /bin/sh -c yum -y install vim 216MB
c484ff737c6f About an hour ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
9d9bcc6d2ca8 About an hour ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
72112a6135d1 About an hour ago /bin/sh -c #(nop) MAINTAINER wz<2086568109@… 0B
eeb6ee3f44bd 6 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 6 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
[root@CentOs ~]#
所以我们平时拿到一个镜像,可以研究一下它是怎么做的!
6,CMD和ENTRYPOINT的区别
CMD 指定这个容器启动运行时的命令,只有最后一个会生效,可被替代
ENTRYPOINT 指定这个容器启动运行时的命令,可以追加命令
CMD测试:
(1)创建文件,编写脚本
(2)构建镜像
docker build -f dockerfile-cmd-test -t cmdtest .
(3)运行镜像生成的容器,发现 “ ls -a”命令生效了
(4)我们想要追加一个命令:-l。最后想要显示“ls -al”,发现报错
[root@CentOs dockerfile]# docker run 38db52f206dd -l
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
[root@CentOs dockerfile]#
原因:在CMD的下,“-l” 替代了CMD ["ls","-a"]命令,但由于 “-l” 不是命令,所以报错了!
再次测试:
这回发现就不报错了!
[root@CentOs dockerfile]# docker run 38db52f206dd ls -al
total 12
drwxr-xr-x. 1 root root 6 Mar 22 05:27 .
drwxr-xr-x. 1 root root 6 Mar 22 05:27 ..
-rwxr-xr-x. 1 root root 0 Mar 22 05:27 .dockerenv
-rw-r--r--. 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 340 Mar 22 05:27 dev
drwxr-xr-x. 1 root root 66 Mar 22 05:27 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 147 root root 0 Mar 22 05:27 proc
dr-xr-x---. 2 root root 114 Nov 13 2020 root
drwxr-xr-x. 11 root root 148 Nov 13 2020 run
lrwxrwxrwx. 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Mar 22 01:39 sys
drwxrwxrwt. 7 root root 132 Nov 13 2020 tmp
drwxr-xr-x. 13 root root 155 Nov 13 2020 usr
drwxr-xr-x. 18 root root 238 Nov 13 2020 var
[root@CentOs dockerfile]#
ENTRYPOINT测试:
步骤同上,细微处有些许改变
接下来就是二者的区别了:
“-l” 之后没有报错!
[root@CentOs dockerfile]# docker run 1acfdaf5e5f5 -l
total 12
drwxr-xr-x. 1 root root 6 Mar 22 05:39 .
drwxr-xr-x. 1 root root 6 Mar 22 05:39 ..
-rwxr-xr-x. 1 root root 0 Mar 22 05:39 .dockerenv
-rw-r--r--. 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 340 Mar 22 05:39 dev
drwxr-xr-x. 1 root root 66 Mar 22 05:39 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 148 root root 0 Mar 22 05:39 proc
dr-xr-x---. 2 root root 114 Nov 13 2020 root
drwxr-xr-x. 11 root root 148 Nov 13 2020 run
lrwxrwxrwx. 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Mar 22 01:39 sys
drwxrwxrwt. 7 root root 132 Nov 13 2020 tmp
drwxr-xr-x. 13 root root 155 Nov 13 2020 usr
drwxr-xr-x. 18 root root 238 Nov 13 2020 var
[root@CentOs dockerfile]#
小结:我们的追加命令,是直接拼接在我们的ENTRYPOINT命令的后面的!
7,不是总结的总结~
dockerfile中很多命令都是十分相似的,我们需要了解它们的区别,我们最后的学习就是对比它们 然后测试效果!