💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## docker分层 用来构建docker镜像的文件 ![](https://img.kancloud.cn/9f/f2/9ff21ef3c9cc0515162243c0609494e2_555x369.png) ## 常用命令 ![](https://img.kancloud.cn/c4/23/c423af48b3163ae987f5e1025cee6948_472x247.png) ~~~ CMD 指定容器启动的时候运行的命令,只有最后一个会生效,可被替代 ENTRYPOINT 指定容器启动的时候运行的命令,可以追加命令 ONBUILD 当构建一个被继承Dockerfile,这时候会运行ONBUILD指令,触发指令 COPY 将文件拷贝到镜像中,类似ADD ENV 设置环境变量 ~~~ ## 实战测试 DockerHub中大多数镜像都是从这个基础镜像来的,FROM scratch,然后配置需要的软件和配置 示例构建一个centos ~~~ FROM centos MAINTAINER wzj<wusir666666@163.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 ~~~ 列出镜像变更历史(构建步骤):docker history 镜像id  ENTRYPOINT 和 CMD区别 ~~~ CMD 指定容器启动的时候运行的命令,只有最后一个会生效,可被替代 ENTRYPOINT 指定容器启动的时候运行的命令,可以追加命令 ~~~ 测试CMD ~~~ 构建镜像: FROM centos CMD ["ls", "-a"] 运行后显示 E:\study\docker\dockerfile>docker run centos_test:v1 . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var 如果此时想要追加一个命令 -l E:\study\docker\dockerfile>docker run centos_test:v1 -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. CMD情况下 -l 替换了 ["ls", "-a"],但是-l不是一个有效命令,所以报错 ~~~ 测试ENTRYPOINT ~~~ 构建镜像: FROM centos ENTRYPOINT ["ls", "-a"] 运行后显示 E:\study\docker\dockerfile>docker run centos_test:v1 . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var 如果此时想要追加一个命令 -l E:\study\docker\dockerfile>docker run centos_test1:v1 -l total 56 drwxr-xr-x 1 root root 4096 Nov 25 09:23 . drwxr-xr-x 1 root root 4096 Nov 25 09:23 .. -rwxr-xr-x 1 root root 0 Nov 25 09:23 .dockerenv lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x 5 root root 340 Nov 25 09:23 dev drwxr-xr-x 1 root root 4096 Nov 25 09:23 etc drwxr-xr-x 2 root root 4096 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 4096 Aug 9 21:40 lost+found drwxr-xr-x 2 root root 4096 May 11 2019 media drwxr-xr-x 2 root root 4096 May 11 2019 mnt drwxr-xr-x 2 root root 4096 May 11 2019 opt dr-xr-xr-x 124 root root 0 Nov 25 09:23 proc dr-xr-x--- 2 root root 4096 Aug 9 21:40 root drwxr-xr-x 11 root root 4096 Aug 9 21:40 run lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 May 11 2019 srv dr-xr-xr-x 13 root root 0 Nov 25 09:23 sys drwxrwxrwt 7 root root 4096 Aug 9 21:40 tmp drwxr-xr-x 12 root root 4096 Aug 9 21:40 usr drwxr-xr-x 20 root root 4096 Aug 9 21:40 var ~~~