dockerfile

知识储备

命令解释
FROM来自于哪一个基础镜像
MAINTAINER镜像维护者的姓名和邮箱地址
RUN容器构建时需要运行的命令
EXPOSE当前容器对外暴露出的端口
WORKDIR终端默认登陆的进来工作目录
ENV沟建镜像过程中设置环境变量
ADD拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包
COPY类似ADD,拷贝文件和目录到镜像中。将从构建吓录中<源路径>的文件/相复制到新的一层的镜像内的<目标路径>位置
VOLUME容器数据卷,于数据保存和持久化工作
CMD只有最后一个生效,CM0会被docker run之后的参数替换
ENTRYPOINTENTRYPOINT的目的和CMD一样,都是在指定容器启动程序及参数
ONBUILD当构建一个 被继承的Dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发

示例dockerfile代码

[root@linux docker]# cat dockerfile1
FROM mycentos:v1.0
MAINTAINER jingwei<857920461@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum install -y lsof
RUN yum install -y tree

EXPOSE 3355

CMD echo $MYPATH
CMD echo "------end------"
CMD /bin/bash
You have new mail in /var/spool/mail/root
[root@linux docker]#

docker bulid 创建镜像

[root@linux docker]# docker build -f dockerfile1 -t mycentos:0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM mycentos:v1.0
 ---> 63c7858bd884
Step 2/10 : MAINTAINER jingwei<857920461@qq.com>
 ---> Running in 9fe0ac333565
Removing intermediate container 9fe0ac333565
 ---> 6b276bcae451
Step 3/10 : ENV MYPATH /usr/local
 ---> Running in 5fe42e132575
Removing intermediate container 5fe42e132575
 ---> b440f78e4e1a
Step 4/10 : WORKDIR $MYPATH
 ---> Running in 1ffe14c78357
Removing intermediate container 1ffe14c78357
 ---> 2706ca29d28d
Step 5/10 : RUN yum install -y lsof
 ---> Running in db97dc517107
tuna                                            4.5 kB/s | 3.6 kB     00:00
Dependencies resolved.
================================================================================
 Package         Architecture      Version                Repository       Size
================================================================================
Installing:
 lsof            x86_64            4.87-6.el7             tuna            331 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 331 k
Installed size: 927 k
Downloading Packages:
lsof-4.87-6.el7.x86_64.rpm                      478 kB/s | 331 kB     00:00
--------------------------------------------------------------------------------
Total                                           477 kB/s | 331 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.

查看

[root@linux docker]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mycentos              0.1                 46e49f05cc34        2 minutes ago       293MB
mysql                 latest              30f937e841c8        3 days ago          541MB
mycentos              v1.0                63c7858bd884        7 days ago          263MB
tomcat                latest              927899a31456        4 weeks ago         647MB
ubuntu                latest              1d622ef86b13        4 weeks ago         73.8MB
nginx                 latest              602e111c06b6        4 weeks ago         127MB
httpd                 latest              b2c2ab6dcf2e        4 weeks ago         166MB
busybox               latest              be5888e67be6        5 weeks ago         1.22MB
portainer/portainer   latest              2869fc110bf7        2 months ago        78.6MB
centos                latest              470671670cac        4 months ago        237MB
[root@linux docker]#
[root@linux docker]# docker run -it --name mycentos mycentos:0.1 /bin/bash
[root@7e2e7492a813 local]# tree
.
|-- bin
|-- etc
|-- games
|-- include
|-- lib
|-- lib64
|-- libexec
|-- sbin
[root@7e2e7492a813 local]# pwd
/usr/local
[root@7e2e7492a813 local]#

docker history 查看历史

[root@linux docker]# docker history mycentos:0.1
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
46e49f05cc34        7 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B
9fa9ceb1230c        7 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
c284c4b1cdcb        7 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
3559befc8131        7 minutes ago       /bin/sh -c #(nop)  EXPOSE 3355                  0B
ef352091a30b        7 minutes ago       /bin/sh -c yum install -y tree                  13.1MB
aa42345aa4b9        7 minutes ago       /bin/sh -c yum install -y lsof                  16.9MB
2706ca29d28d        7 minutes ago       /bin/sh -c #(nop) WORKDIR /usr/local            0B
b440f78e4e1a        7 minutes ago       /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B
6b276bcae451        7 minutes ago       /bin/sh -c #(nop)  MAINTAINER jingwei<857920…   0B
63c7858bd884        7 days ago                                                          263MB               dockertest
[root@linux docker]#

CMD与ENTERYPOINT的区别

[root@linux docker]# cat dockerfile2
FROM mycentos:v1.0
CMD ["ls","-a"]
[root@linux docker]# docker build -f dockerfile2 -t mycentos:v0.3 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM mycentos:v1.0
 ---> 63c7858bd884
Step 2/2 : CMD ["ls","-a"]
 ---> Running in ac677913ce13
Removing intermediate container ac677913ce13
 ---> 5090a9b29e86
Successfully built 5090a9b29e86
Successfully tagged mycentos:v0.3
[root@linux docker]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mycentos              v0.3                5090a9b29e86        11 seconds ago      263MB
mycentos              0.1                 46e49f05cc34        15 minutes ago      293MB
mysql                 latest              30f937e841c8        3 days ago          541MB
mycentos              v1.0                63c7858bd884        7 days ago          263MB
tomcat                latest              927899a31456        4 weeks ago         647MB
ubuntu                latest              1d622ef86b13        4 weeks ago         73.8MB
nginx                 latest              602e111c06b6        4 weeks ago         127MB
httpd                 latest              b2c2ab6dcf2e        4 weeks ago         166MB
busybox               latest              be5888e67be6        5 weeks ago         1.22MB
portainer/portainer   latest              2869fc110bf7        2 months ago        78.6MB
centos                latest              470671670cac        4 months ago        237MB
[root@linux docker]# docker run 5090a9b29e86
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@linux docker]#

测试发现发现ls -a是生效了的 要是我们在后面添加一个-l就会报错因为CMD只执行最后的一个命令而-l命令是不存在的所以直接报错。要是ls -la就会直接成功。

[root@linux docker]# docker run 5090a9b29e86 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $PATH": unknown.
[root@linux docker]# docker run 5090a9b29e86 ls -la
total 32
drwxr-xr-x.  17 root root 4096 May 25 02:10 .
drwxr-xr-x.  17 root root 4096 May 25 02:10 ..
-rwxr-xr-x.   1 root root    0 May 25 02:10 .dockerenv
lrwxrwxrwx.   1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x.   5 root root  340 May 25 02:10 dev
drwxr-xr-x.  51 root root 4096 May 25 02:10 etc
drwxr-xr-x.   2 root root    6 May 11  2019 home
lrwxrwxrwx.   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwx------.   2 root root    6 Jan 13 21:48 lost+found
drwxr-xr-x.   2 root root    6 May 11  2019 media
drwxr-xr-x.   2 root root    6 May 11  2019 mnt
drwxr-xr-x.   2 root root    6 May 11  2019 opt
dr-xr-xr-x. 223 root root    0 May 25 02:10 proc
dr-xr-x---.   2 root root 4096 Jan 13 21:49 root
drwxr-xr-x.  11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx.   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 May 11  2019 srv
dr-xr-xr-x.  13 root root    0 May 25 00:22 sys
drwxrwxrwt.   7 root root 4096 May 17 14:27 tmp
drwxr-xr-x.  12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x.  20 root root 4096 Jan 13 21:49 var
[root@linux docker]#

测试ENTERYPOINT

[root@linux docker]# cat dockerfile3
FROM mycentos:v1.0
ENTRYPOINT ["ls","-a"]
[root@linux docker]# docker build -f dockerfile3 -t enterypoint:v0.1 .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM mycentos:v1.0
 ---> 63c7858bd884
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in d0b1c949265f
Removing intermediate container d0b1c949265f
 ---> a6cb66cf38f7
Successfully built a6cb66cf38f7
Successfully tagged enterypoint:v0.1
[root@linux docker]# cat dockerfile3
FROM mycentos:v1.0
ENTRYPOINT ["ls","-a"]

现在添加-l参数测试


[root@linux docker]# docker run a6cb66cf38f7 -l
total 32
drwxr-xr-x.  17 root root 4096 May 25 02:15 .
drwxr-xr-x.  17 root root 4096 May 25 02:15 ..
-rwxr-xr-x.   1 root root    0 May 25 02:15 .dockerenv
lrwxrwxrwx.   1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x.   5 root root  340 May 25 02:15 dev
drwxr-xr-x.  51 root root 4096 May 25 02:15 etc
drwxr-xr-x.   2 root root    6 May 11  2019 home
lrwxrwxrwx.   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwx------.   2 root root    6 Jan 13 21:48 lost+found
drwxr-xr-x.   2 root root    6 May 11  2019 media
drwxr-xr-x.   2 root root    6 May 11  2019 mnt
drwxr-xr-x.   2 root root    6 May 11  2019 opt
dr-xr-xr-x. 223 root root    0 May 25 02:15 proc
dr-xr-x---.   2 root root 4096 Jan 13 21:49 root
drwxr-xr-x.  11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx.   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 May 11  2019 srv
dr-xr-xr-x.  13 root root    0 May 25 00:22 sys
drwxrwxrwt.   7 root root 4096 May 17 14:27 tmp
drwxr-xr-x.  12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x.  20 root root 4096 Jan 13 21:49 var
[root@linux docker]#

再次的验证 CMD域ENTRYPONT的区别
CMD命令会替换之前的命令而ENTRYPOINT会追加的执行当你发现执行的命令可以直接加参数可以判断是ENTRYPOINT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值