dockerfile 是用来构建docker镜像的文件! 命令参数脚本!
构建步骤:
1、编写一个 dockerfile 文件
2、docker build 构建成为一个镜像
3、docker run 运行镜像
4、docker push 发布镜像(dockerHub 、 阿里云镜像仓库)
基础知识:
1、每个保留关键字(指令)都必须是大写字母
2、执行从上到下顺序执行
3、#表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交!
dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写Dockerfile文件,这个文件十分简单
Docker镜像逐渐成为了企业交付的标准,必须要掌握!
步骤:开发,部署,运维 缺一不可
DockerFile:构建文件,定义了一切的步骤,源代码
Dockerimages:通过DockerFile 构建生成的镜像,最终发布和运行的产品
Docker容器:镜像运行起来提供服务的
FROM #基础镜像,一切从这里开始构建
MAINTAINER # 镜像是谁写的,姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD #步骤 tomcat镜像 这个tomcat压缩包! 添加内容
WORKDIR #镜像的工作目录
VOLUME # 挂载到哪儿个目录位置
EXPOST # 暴露端口配置
CMD # 指定这个容器启动的时候要运行的命令 只有最后一个会生效,可被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD 的指令 , 触发指令
COPY # 类似 ADD , 将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量!
Docker Hub 中 99% 镜像都是从这个基础镜像过来的from scratch 然后配置需要的软件和配置来进行的构建
创建一个自己的centos 编写一个 工作路径在/usr/local ,安装了vim 和 ifconfig 功能的镜像
1、编写Dockerfile 的文件vim /home/dockerfile/mydockerfile-centos
[root@localhost dockerfile]# cat mydockerfile-centos
FROM centos:7
MAINTAINER laoba<2658994997@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
2、通过这个文件构建镜像
命令 docker build -f mydockerfile-centos -t mycentos .
(前提有centos:7版本的镜像,没有的 docker pull centos:7 下载一个)
3、测试
# 测试
[root@localhost dockerfile]# docker run -it mycentos
[root@0e2e34546251 local]# pwd
/usr/local
[root@0e2e34546251 local]# vim
~
~ VIM - Vi IMproved
~
~ version 7.4.629
~ by Bram Moolenaar et al.
~ Modified by
~ Vim is open source and freely distributable
~
~ Sponsor Vim development!
~ type :help sponsor for information
~
~ type :q to exit
~ type :help or for on-line help
~ type :help version7 for version info
~
~
[root@71b3213cb3bd local]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 7 bytes 586 (586.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@71b3213cb3bd local]#
docker histroy 镜像ID 查看镜像制作步骤
四、CMD 和 ENTRYPOINT 的区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定这个容器启动的 时候要运行的命令,可以追加命令
1、编写 dockerfile 文件
[root@localhost dockerfile]# cat dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
2、构建镜像
[root@localhost dockerfile]# docker run -it cmdtest
3、run运行 发现我们的ls -a 命令生效
[root@localhost dockerfile]# docker run -it cmdtest
WARNING: IPv4 forwarding is disabled. Networking will not work.
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
4、想追加一个命令 -l ls -al
[root@localhost dockerfile]# docker run cmdtest -l
WARNING: IPv4 forwarding is disabled. Networking will not work.
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
5、# cmd的情况下 -l 替换了CMD ["ls","-a"] 命令 ,-l 不是命令所以报错 !
正确写法是: docker run cmdtext ls -al
[root@localhost dockerfile]# docker run cmdtest ls -al
total 0
drwxr-xr-x. 1 root root 6 May 15 12:31 .
drwxr-xr-x. 1 root root 6 May 15 12:31 ..
-rwxr-xr-x. 1 root root 0 May 15 12:31 .dockerenv
lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 340 May 15 12:31 dev
drwxr-xr-x. 1 root root 66 May 15 12:31 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. 235 root root 0 May 15 12:31 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 May 2 07:13 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@localhost dockerfile]#
测试 ENTRYPOINT
[root@localhost dockerfile]# vim dockerfile-cmd-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
[root@localhost dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 4ada8776f743
Removing intermediate container 4ada8776f743
---> 96d79621f6d6
Successfully built 96d79621f6d6
Successfully tagged entrypoint-test:latest
[root@localhost dockerfile]# docker run -it entrypoint-test
WARNING: IPv4 forwarding is disabled. Networking will not work.
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
# 我们的追加命令,是直接拼接在我们的 ENTRYPOINT 命令的后面!
[root@localhost dockerfile]# docker run entrypoint-test -l
WARNING: IPv4 forwarding is disabled. Networking will not work.
total 0
drwxr-xr-x. 1 root root 6 May 15 11:50 .
drwxr-xr-x. 1 root root 6 May 15 11:50 ..
-rwxr-xr-x. 1 root root 0 May 15 11:50 .dockerenv
lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 340 May 15 11:50 dev
drwxr-xr-x. 1 root root 66 May 15 11:50 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. 233 root root 0 May 15 11:50 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 May 2 07:13 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@localhost dockerfile]#
五、实战2
待更新
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)