DockerFile实战测试

DockerFile实战测试,第1张

DockerFile实战测试

创建一个自己的centos

1.编写dockerfile的文件

[root@localhost dockerfile]# cat mydockerfile 
FROM centos
MAINTAINER gaole<490135586@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH    #一开始的时候这里写错了,没有写$,所有进去以后到了/MYPATH的路径
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo "welcome to gaole's centos"
CMD /bin/bash

2.通过这个文件构建镜像,最后有一个点

[root@localhost dockerfile]# docker build -f mydockerfile -t mycentos:1.0 .

[root@localhost dockerfile]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
mycentos           1.0       09170a90d5db   15 seconds ago   326MB
kuangshen/centos   latest    915412bdf8be   19 hours ago     231MB
mysql              latest    3218b38490ce   5 weeks ago      516MB
hello-world        latest    feb5d9fea6a5   4 months ago     13.3kB
centos             latest    5d0da3dc9764   4 months ago     231MB

3.运行测试

docker run -it mycentos:1.0   #直接就进去了,然后就会到工作目录,注意必须要加版本号,不加默认下载最新版本

4.docker history +imagesID  可以看到镜像构建的过程

[root@localhost dockerfile]# docker history 09170a90d5db
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
09170a90d5db   7 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin鈥  0B        
b010236262ea   7 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo鈥  0B        
550f564f074f   7 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
885e05709640   7 minutes ago   /bin/sh -c yum -y install net-tools             28.4MB    
2ea22529d68c   7 minutes ago   /bin/sh -c yum -y install vim                   66.3MB    
bf270a9b7e4a   8 minutes ago   /bin/sh -c #(nop) WORKDIR /MYPATH               0B        
47e6ec06bfe6   8 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B        
aaef5fafb053   8 minutes ago   /bin/sh -c #(nop)  MAINTAINER gaole<49013558鈥  0B        
5d0da3dc9764   4 months ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
      4 months ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc鈥  0B        
      4 months ago    /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0鈥  231MB     
[root@localhost dockerfile]# 

 CMD:指定这个容器启动的时候要运行的命令,只有最后一个会生效,可以被替代

ENTRYPOINT:指定这个容器启动的时候要运行的命令,可以追加命令

CMD 的例子

[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t cmd-test .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
 ---> Running in f52a632e1e4d
Removing intermediate container f52a632e1e4d
 ---> 73e6698def47
Successfully built 73e6698def47
Successfully tagged cmd-test:latest
[root@localhost dockerfile]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
cmd-test           latest    73e6698def47   4 seconds ago    231MB
mycentos           1.0       09170a90d5db   25 minutes ago   326MB
kuangshen/centos   latest    915412bdf8be   20 hours ago     231MB
mysql              latest    3218b38490ce   5 weeks ago      516MB
hello-world        latest    feb5d9fea6a5   4 months ago     13.3kB
centos             latest    5d0da3dc9764   4 months ago     231MB
[root@localhost dockerfile]# docker run cmd-test
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost dockerfile]# cat dockerfile-cmd-test 
FROM centos
CMD ["ls","-a"]
[root@localhost dockerfile]# docker run cmd-test -l    #直接写-l报错,因为CMD没有办法追加
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
[root@localhost dockerfile]# docker run cmd-test ls -al   #必须写成ls -al
total 0
drwxr-xr-x.   1 root root   6 Jan 31 04:54 .
drwxr-xr-x.   1 root root   6 Jan 31 04:54 ..
-rwxr-xr-x.   1 root root   0 Jan 31 04:54 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 340 Jan 31 04:54 dev
drwxr-xr-x.   1 root root  66 Jan 31 04:54 etc
drwxr-xr-x.   2 root root   6 Nov  3  2020 home
lrwxrwxrwx.   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------.   2 root root   6 Sep 15 14:17 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 108 root root   0 Jan 31 04:54 proc
dr-xr-x---.   2 root root 162 Sep 15 14:17 root
drwxr-xr-x.  11 root root 163 Sep 15 14:17 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 Jan 27 18:25 sys
drwxrwxrwt.   7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x.  12 root root 144 Sep 15 14:17 usr
drwxr-xr-x.  20 root root 262 Sep 15 14:17 var

ENTRYPOINT的例子

[root@localhost dockerfile]# docker build -f dockerfile-entrypoint-test -t mycentos_entry:1.0 .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 1e50ceea74b7
Removing intermediate container 1e50ceea74b7
 ---> fd27bd2e1724
Successfully built fd27bd2e1724
Successfully tagged mycentos_entry:1.0
[root@localhost dockerfile]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
mycentos_entry     1.0       fd27bd2e1724   8 seconds ago    231MB
cmd-test           latest    73e6698def47   11 minutes ago   231MB
mycentos           1.0       09170a90d5db   37 minutes ago   326MB
kuangshen/centos   latest    915412bdf8be   20 hours ago     231MB
mysql              latest    3218b38490ce   5 weeks ago      516MB
hello-world        latest    feb5d9fea6a5   4 months ago     13.3kB
centos             latest    5d0da3dc9764   4 months ago     231MB
[root@localhost dockerfile]# docker run mycentos_entry:1.0
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost dockerfile]# docker run mycentos_entry:1.0 -l  #-l可以直接追加到ls -a后面
total 0
drwxr-xr-x.   1 root root   6 Jan 31 05:03 .
drwxr-xr-x.   1 root root   6 Jan 31 05:03 ..
-rwxr-xr-x.   1 root root   0 Jan 31 05:03 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 340 Jan 31 05:03 dev
drwxr-xr-x.   1 root root  66 Jan 31 05:03 etc
drwxr-xr-x.   2 root root   6 Nov  3  2020 home
lrwxrwxrwx.   1 root root   7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root   9 Nov  3  2020 lib64 -> usr/lib64
drwx------.   2 root root   6 Sep 15 14:17 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 108 root root   0 Jan 31 05:03 proc
dr-xr-x---.   2 root root 162 Sep 15 14:17 root
drwxr-xr-x.  11 root root 163 Sep 15 14:17 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 Jan 27 18:25 sys
drwxrwxrwt.   7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x.  12 root root 144 Sep 15 14:17 usr
drwxr-xr-x.  20 root root 262 Sep 15 14:17 var
l[root@localhost dockerfile]# cat dockerfile-entrypoint-test 
FROM centos
ENTRYPOINT ["ls","-a"]

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715470.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存