目录
1.copy和add的区别
ADD :支持将远程URL的资源加入到镜像的文件系统
COPY:只复制
2.cmd和entrypoint的区别
CMD 与entrypoint:都是容器启动时要加载的命令/启动程序
如果没有cmd的话,默认情况下加载entrypoint
exec模式与shell模式
shell就是解释器
exec:容器加载时使用的启动的第一个任务进程
shell:容器加载时使用的第一个bash ( /bin/bash /bin/sh /bin/init)
自检完成后,加载第一个pid = 1进程
shell翻译官/解释器,解析
echo $PATH
3.exec模式和shell模式
shell——shell环境,解释性,最后加载翻译
exec——系统环境,系统自己能识别的指令
init——初始化
/usr/bin , /usr/sh是进程
小实验
exec模式
[root@docker ~]# docker pull centos:7
[root@docker ~]# mkdir test
[root@docker ~]# cd test/
[root@docker test]# ls
[root@docker test]# mkdir test1
[root@docker test]# cd test1/
[root@docker test1]# vim dockerfile
FROM centos:7
CMD ["top"]
[root@docker test1]# docker build -t centos:1 .
[root@docker test1]# docker run -itd --name test centos:1
[root@docker test1]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
570480bf3e71 centos:1 "top" 4 minutes ago Up 4 minutes test
##exec
[root@docker test1]# docker exec test ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 56156 1952 pts/0 Ss+ 10:47 0:00 top
root 8 0.0 0.0 51732 1708 ? Rs 10:56 0:00 ps aux
使用exec模式无法输出环境变量
[root@docker test1]# vim dockerfile
FROM centos:7
CMD ["echo","$HOME"]
[root@docker test1]# echo $HOME
/root
[root@docker test1]# docker build -t centos:v2 .
[root@docker test1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v2 816f4dd8ce39 3 seconds ago 204MB
centos 1 1ca347380a3b 18 minutes ago 204MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[root@docker test1]# docker run --rm centos:v2
$HOME
##运行一次之后删除
shell模式
[root@docker test1]# vim dockerfile
FROM centos:7
CMD ["sh","-c","echo $HOME"]
[root@docker test1]# docker build -t centos:v2 .
[root@docker test1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v2 be8f3849d292 18 seconds ago 204MB
centos 1 1ca347380a3b 25 minutes ago 204MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[root@docker test1]# docker run --rm centos:v2
WARNING: IPv4 forwarding is disabled. Networking will not work.
/root
exec无法加载有变量的环境
shell中可以有bash sh加载带变量的环境
/bin/sh -c nginx shell模式
nginx exec模式
cmd和entrypoint的总结
cmd和entrypoint都是容器环境启动时加载的命令/程序
cmd是容器环境启动时默认加载的命令
entrypoint是容器环境启动时第一个加载的命令程序/脚本程序 init
init pid 1
如果 ENTRYPOINT使用了shell模式,CMD指令会被忽略。
如果 ENTRYPOINT使用了exec模式,CMD指定的内容被追加为 ENTRYPOINT指定命令的参数。
如果 ENTRYPOINT使用了exec模式,CMD也应该使用exec模式。
如果有多条cmd命令的话,第一条生效。如果有多条entrypoint的话,最后一条生效。
PS:
entrypoint [ "nginx", "一s"]
CMD [ "reload"]