docker cmd和entrypoint

FROM scratch 很多镜像都是从他开始
创建自己的centos

FROM centos 基于官方的centos开始写
构建自己的centos
[root@docker dockerfile]# cat mydockerfile
FROM centos (没有镜像时会拉取)
MAINTAINER SUPERMAO2567169491@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

FROM默认拉最新的centos可是centos8无法上网
指定拉centos7.0

构建镜像
docker build -f mydockerfile -t mycentos0.1 .

我们可以列出本地镜像的变更历史
docker history 950ba3687977
可以查看这个镜像如何一步步做起来

CMD与ENTRYPOINT只执行最后一个
CMD ENTRYPOINT
测试cmd
[root@docker dockerfile]# cat
dockerfile.cmd mydockerfile
[root@docker dockerfile]# cat dockerfile.cmd
FROM centos:7
CMD ["ls","-a"]
[root@docker dockerfile]# docker run cmdtest
.
..
.dockerenv
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@docker dockerfile]#
启动时执行此命令

[root@docker dockerfile]# docker run cmdtest -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@docker dockerfile]# 

cmd的情况下,用-l替换了cmd的命令 -l不是命令所以报错
["ls","-a"] (双引号逗号严格写)

entrypoint可以追加-l   ls  -al   
[root@docker dockerfile]# docker run entrypoint -l
total 0
drwxr-xr-x.   1 root root   6 Mar 30 06:59 .
drwxr-xr-x.   1 root root   6 Mar 30 06:59 ..
-rwxr-xr-x.   1 root root   0 Mar 30 06:59 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 340 Mar 30 06:59 dev
drwxr-xr-x.   1 root root  66 Mar 30 06:59 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  2021 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. 141 root root   0 Mar 30 06:59 proc
dr-xr-x---.   2 root root 162 Sep 15  2021 root
drwxr-xr-x.  11 root root 163 Sep 15  2021 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 Mar 30 03:51 sys
drwxrwxrwt.   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var
[root@docker dockerfile]# cat docker-entrypoint 
FROM centos
ENTRYPOINT  ["ls","-a"]
[root@docker dockerfile]# 

对比测试发现CMD与ENTRYPOINT的区别

posted @ 2022-03-30 22:43  supermao12  阅读(250)  评论(0编辑  收藏  举报