Docker(七)

60 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第22天,点击查看活动详情

DockerFile的指令

FROM 基础镜像,一切从这里开始构建
MAINTAINER 镜像是谁写的 姓名+邮箱
RUN 镜像构建的时候需要运行的命令
ADD 步骤:tomcat镜像,这个tomcat压缩包!添加内容
WORKDIR 镜像的工作目录
VOLUME 挂载的目录
EXPOST 保留端口配置\ CMD 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被代替
ENTRYPOINT 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD 当构建一个被继承Dockerfile,这个时候就会运行ONBUILD的指令,触发指令
COPY 类似ADD,将我们文件拷贝到镜像中
ENV 构建的时候设置环境变量\

在这里插入图片描述

CMD和ENTRYPOINT的区别

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

测试CMD

编写dockerfile文件 
 [root@VM-0-12-centos home]# ls
ceshi  dockerfile  docker-test-volume  mysql  www
[root@VM-0-12-centos home]# cd dockerfile
[root@VM-0-12-centos dockerfile]# ls
[root@VM-0-12-centos dockerfile]# vim dockerfile-cmd-test
[root@VM-0-12-centos dockerfile]# cat dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
构建镜像
[root@VM-0-12-centos dockerfile]# docker build -f dockerfile-cmd-test
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
[root@VM-0-12-centos dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 419baf5895ff
Removing intermediate container 419baf5895ff
 ---> 1539fed5670d
Successfully built 1539fed5670d
Successfully tagged cmdtest:latest

run运行

[root@VM-0-12-centos dockerfile]# docker run 1539fed5670d
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

想追加一个命令 -l ls -al 出现报错

[root@VM-0-12-centos dockerfile]# docker run 1539fed5670d -l
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.
在cmd清理下  -l 替换了CMD   ["ls","-a"] 命令,-l 不是命令所以报错!

测试ENTRYPOINT

[root@VM-0-12-centos dockerfile]# cat dockerfile-cmd-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
[root@VM-0-12-centos dockerfile]# vim dockerfile-cmd-entrypoint
[root@VM-0-12-centos dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test .
Sending build context to Docker daemon  15.87kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 897e33c72832
Removing intermediate container 897e33c72832
 ---> 91eaa82e9928
Successfully built 91eaa82e9928
Successfully tagged entrypoint-test:latest
[root@VM-0-12-centos dockerfile]# docker run ^C
[root@VM-0-12-centos dockerfile]# docker run 91eaa82e9928
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@VM-0-12-centos dockerfile]# docker run 91eaa82e9928 -l
total 56
drwxr-xr-x   1 root root 4096 Dec 17 03:20 .
drwxr-xr-x   1 root root 4096 Dec 17 03:20 ..
-rwxr-xr-x   1 root root    0 Dec 17 03:20 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  340 Dec 17 03:20 dev
drwxr-xr-x   1 root root 4096 Dec 17 03:20 etc
drwxr-xr-x   2 root root 4096 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 4096 Sep 15 14:17 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 158 root root    0 Dec 17 03:20 proc
dr-xr-x---   2 root root 4096 Sep 15 14:17 root
drwxr-xr-x  11 root root 4096 Sep 15 14:17 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Dec 11 02:48 sys
drwxrwxrwt   7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x  12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x  20 root root 4096 Sep 15 14:17 var

Dockerfile中很多命令都是十分的相似,