您的位置:制图网首页 -> 新闻动态 -> dockerfile常用指令【dockerfile是什么意思】

热门排行

dockerfile常用指令【dockerfile是什么意思】

2023/1/31 来自于:制图网 点击:39

一、Dockerfile的条条框框

1、什么是Dockerfile?

Dockerfile是一种被Docker程序解释的脚本。Dockerfile由一条一条的指令组成,并且有自己的书写格式和支持的命令。当我们需要指定自己额外的需求时,只需在Dockerfile上添加或修改指令,然后通过docker build生成我们自定义的镜像(image)。

dockerfile常用指令【dockerfile是什么意思】

2、Dockerfile的注意事项

Dockerfile的指令是忽略大小写的,建议使用大写每一行只支持一条指令每条指令可以携带多个参数支持以#开头的注释

3、Dockerfile指令(instruction)

构建指令:用于构建image。其指定的操作不会在运行image的容器上执行(FROM、MAINTAINER、RUN、ENV、ADD、COPY)

设置指令:用于设置image的属性。其指定的操作将在运行image的容器中执行(CMD、ENTRYPOINT、USER 、EXPOSE、VOLUME、WORKDIR、ONBUILD)

4、Dockerfile的基本结构

基础镜像信息、维护者信息、镜像操作指令、容器启动时执行指令

二、Dockerfile指令(instruction)实操

dockerfile常用指令【dockerfile是什么意思】

1、FROM(指定基础image,必须为第一个命令)

格式:

FROM <image> FROM <image>:<tag>

例:

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest [root@localhost docker]# docker build -t my_test . #注意有个“.” Sending build context to Docker daemon 2.048kB Step 1/1 : FROM centos:latest ---> 9f38484d220f Successfully built 9f38484d220f Successfully tagged my_test:latest

2、MAINTAINER(维护者,用来指定创建者信息)

格式:

MAINTAINER <name>

例:(通过inspect验证)

[root@localhost docker]# vim Dockerfile

#FROM centos

FROM centos:latest

MAINTAINER Xuyun Liu <1972163777@qq.com>

[root@localhost docker]# docker build -t my_test .

Sending build context to Docker daemon 2.048kB

Step 1/2 : FROM centos:latest

---> 9f38484d220f

Step 2/2 : MAINTAINER Xuyun Liu <1972163777@qq.com>

---> Using cache

---> be97dff9636f

Successfully built be97dff9636f

Successfully tagged my_test:latest

[root@localhost docker]# docker inspect -f {{.Author}} my_test

Xuyun Liu <1972163777@qq.com>

3、RUN(安装软件用)

格式:

RUN command param1 param2 #使用shell终端运行命令(/bin/sh、/bin/bash等) RUN ['executable(可执行的))','param1(参数)','param2'...] #不通过shell执行,而是通过内建命令exec去执行

例:

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest MAINTAINER Xuyun Liu <1972163777@qq.com> RUN touch /home/a.txt RUN ["touch","/home/b.txt"] #这里双引号和单引号也是有讲究的,单引号会报错 [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : MAINTAINER Xuyun Liu <1972163777@qq.com> ---> Using cache ---> be97dff9636f Step 3/4 : RUN touch /home/a.txt ---> Using cache ---> 5af84bebdda1 Step 4/4 : RUN ["touch","/home/b.txt"] ---> Running in 541b7c7da76a Removing intermediate container 541b7c7da76a ---> 6e19811bbf18 Successfully built 6e19811bbf18 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls /home a.txt b.txt

4、CMD(设置container启动时执行的操作)

格式:

CMD command param1 param2 CMD ["executable","param1","param2"...] CMD ["param1","param2"...] #当CMD和ENTRYPOINT配合使用时的写法

例1:CMD指令会被docker run提供的参数覆盖

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] CMD ls /dev [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/3 : FROM centos:latest ---> 9f38484d220f Step 2/3 : RUN touch /home/a.txt ---> Running in 384f2554526a Removing intermediate container 384f2554526a ---> cfb30d89ad46 Step 3/3 : CMD ls /dev ---> Running in b568167236d3 Removing intermediate container b568167236d3 ---> 1e1971815006 Successfully built 1e1971815006 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test console fd mqueue ptmx random stderr stdout urandom core full null pts shm stdin tty zero [root@localhost docker]# docker run -it my_test ls /home a.txt

例2:当一个Dockfile文件中有两个或多个CMD指令时,只执行最后一条CMD指令

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] CMD ls /dev CMD ["ls","/usr","/usr/local"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/4 : CMD ls /dev ---> Using cache ---> 1e1971815006 Step 4/4 : CMD ["ls","/usr","/usr/local"] ---> Running in ccfabb87efc5 ^[[ARemoving intermediate container ccfabb87efc5 ---> e439603840ef Successfully built e439603840ef Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /usr: bin etc games include lib lib64 libexec local sbin share src tmp /usr/local: bin etc games include lib lib64 libexec sbin share src

5、ENTRYPOINT(设置container启动时执行的操作)

格式:

ENTRYPOINT command param1 param2 ENTRYPOINT ["executable","param1","param2"]

例1:ENTRYPOINT指令不会被docker run提供的参数覆盖

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] ENTRYPOINT ls /root /sys [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/3 : FROM centos:latest ---> 9f38484d220f Step 2/3 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/3 : ENTRYPOINT ls /root /sys ---> Running in 67be0ac881cb Removing intermediate container 67be0ac881cb ---> e51f45c181ac Successfully built e51f45c181ac Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /root: anaconda-ks.cfg /sys: block class devices fs kernel power bus dev firmware hypervisor module [root@localhost docker]# docker run -it my_test ls /home /root: anaconda-ks.cfg /sys: block class devices fs kernel power bus dev firmware hypervisor module

例2:当一个Dockerfile文件中有两个或多个ENTRYPOINT指令时,只执行最后一条ENTRYPOINT指令

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] ENTRYPOINT ls /root /sys ENTRYPOINT ["ls","/home"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/4 : ENTRYPOINT ls /root /sys ---> Using cache ---> e51f45c181ac Step 4/4 : ENTRYPOINT ["ls","/home"] ---> Running in 95c2491f9b3b Removing intermediate container 95c2491f9b3b ---> 6196eaff66d3 Successfully built 6196eaff66d3 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test a.txt

例3:当一个Dockerfile文件中既有CMD指令又有ENTRYPOINT指令时,CMD指令将被作为ENTRYPOINT指令的参数

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys CMD ["-l"] ENTRYPOINT ["ls","/home"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/4 : CMD ["-l"] ---> Using cache ---> 4c5cf79c279d Step 4/4 : ENTRYPOINT ["ls","/home"] ---> Using cache ---> 14d643d73d81 Successfully built 14d643d73d81 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test total 0 -rw-r--r-- 1 root root 0 Aug 29 17:09 a.txt

6、USER(设置container的用户,默认是root用户,这个root不是那个root)

格式:

USER username/uid

例:

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] RUN useradd admin #需要先创建一个用户,不然会提示用户不存在 USER admin [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/3 : FROM centos:latest ---> 9f38484d220f Step 2/3 : RUN useradd admin ---> Running in a4847a39ab06 Removing intermediate container a4847a39ab06 ---> 3759bdc0589c Step 3/3 : USER admin ---> Running in 80e367ffe699 Removing intermediate container 80e367ffe699 ---> c95196915358 Successfully built c95196915358 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /bin/bash [admin@f0f565e8da5a /]$

7、EXPOSE(暴露,指定容器需要映射到宿主机的端口)

格式:

EXPOSE port1 [port2 ...]

例:

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] #RUN useradd admin #USER admin EXPOSE 80 [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM centos:latest ---> 9f38484d220f Step 2/2 : EXPOSE 80 ---> Running in 75dda1ff5393 Removing intermediate container 75dda1ff5393 ---> f6759e9df32d Successfully built f6759e9df32d Successfully tagged my_test:latest [root@localhost docker]# docker run -dit -p 80 my_test /bin/bash #随机映射 5b790387fd758115204ce0a1f7ed1daa17a983108004948cb26b81f8fa8dc1f6 #随机映射(大P) [root@localhost docker]# docker run -dit -P my_test /bin/bash #映射到宿主机的80 7207f9ae8a2c2932df727854cecb6b16d6b09ffecd1411bf42d03ffb1c271803 [root@localhost docker]# docker run -dit -p 80:80 my_test /bin/bash 83c40826648c13d23c706db3474d1eb541261638e5c1415abaebfb7a37d758ca [root@localhost docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83c40826648c my_test "/bin/bash" About a minute ago Up About a minute 0.0.0.0:80->80/tcp silly_snyder 7207f9ae8a2c my_test "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:32769->80/tcp youthful_kirch 5b790387fd75 my_test "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:32768->80/tcp focused_bose

8、ENV(用于设置环境变量)

格式:

ENV <key> <value>

例:

[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] #RUN useradd admin #USER admin #EXPOSE 80 ENV a Hello World!! [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM centos:latest ---> 9f38484d220f Step 2/2 : ENV a Hello World!! ---> Running in af33c5e13754 Removing intermediate container af33c5e13754 ---> dd22ab298385 Successfully built dd22ab298385 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /bin/bash [root@7dca23648303 /]# echo $a Hello World!!

9、ADD(将源文件复制到container的目标文件)

格式:

ADD <src> <dest>

注:src可以是Dockerfile所在目录的相对路径,也可以是一个绝对路径;

src为tar文件时,自动减压到dest中;

src为一个web的url时,会自动下载到dest中;

写dest路径时注意结尾的“/”,结尾有“/”和没“/”是有区别的

例:

[root@localhost docker]# pwd /root/docker [root@localhost docker]# echo "Hello World!!" > test.txt [root@localhost docker]# wget http://nginx.org/download/nginx-1.16.1.tar.gz [root@localhost docker]# ls Dockerfile nginx-1.16.1.tar.gz test.txt [root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <1972163777@qq.com> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] #RUN useradd admin #USER admin #EXPOSE 80 #ENV a Hello World!! ADD test.txt /tmp ADD nginx-1.16.1.tar.gz /tmp ADD http://nginx.org/download/nginx-1.16.1.tar.gz /home [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.036MB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : ADD test.txt /tmp ---> 7fa385db5eef Step 3/4 : ADD nginx-1.16.1.tar.gz /tmp ---> 24c9c0e0877d Step 4/4 : ADD http://nginx.org/download/nginx-1.16.1.tar.gz /home Downloading 1.033MB/1.033MB ---> b842677c7912 Successfully built b842677c7912 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls /tmp /home /home: nginx-1.16.1.tar.gz /tmp: ks-script-eC059Y nginx-1.16.1 test.txt yum.log #ks-script-eC059Y和yum.log是原有文件

10、COPY(复制本地主机的<src>到容器中的<dest>,与ADD指令相似)

格式:

COPY <src> <dest>

注:COPY指令和ADD指令功能和使用方式类似,但COPY指令只是复制,不会下载和解压

例:

[root@localhost docker]# echo "This is a test." > test.html [root@localhost docker]# ls Dockerfile nginx-1.16.1.tar.gz test.html test.txt [root@localhost docker]# vim Dockerfile FROM centos:latest COPY test.html / [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.037MB Step 1/2 : FROM centos:latest ---> 9f38484d220f Step 2/2 : COPY test.html / ---> e48a7c46ded6 Successfully built e48a7c46ded6 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls -l /test.html -rw-r--r-- 1 root root 16 Aug 29 18:30 /test.html

11、VOLUME(卷,指定挂载点)

格式:

VOLUME ["<mountpoint>"]

12、WORKDIR(切换目录,可以多次切换,相当于cd命令)

格式:

WORKDIR Directory_url

例:

[root@localhost docker]# vim Dockerfile FROM centos:latest WORKDIR /tmp RUN mkdir test WORKDIR test RUN ["touch","a.txt","b.txt"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.037MB Step 1/5 : FROM centos:latest ---> 9f38484d220f Step 2/5 : WORKDIR /tmp Removing intermediate container 9258f95093db ---> 6b8f6f6c69f6 Step 3/5 : RUN mkdir test ---> Running in e44f8d897714 Removing intermediate container e44f8d897714 ---> 1dc77dd96431 Step 4/5 : WORKDIR test Removing intermediate container df09ed12198d ---> 94cb324c6118 Step 5/5 : RUN ["touch","a.txt","b.txt"] ---> Running in 63f6bdcdd475 Removing intermediate container 63f6bdcdd475 ---> 1ae6996b6573 Successfully built 1ae6996b6573 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls -l /tmp/test total 0 -rw-r--r-- 1 root root 0 Aug 29 18:39 a.txt -rw-r--r-- 1 root root 0 Aug 29 18:39 b.txt

13、ONBUILD(在子镜像中执行)

格式:

ONBUILD INSTRUCTION

例:

[root@localhost docker]# vim Dockerfile [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.038MB Step 1/2 : FROM centos:latest ---> 9f38484d220f Step 2/2 : ONBUILD RUN touch /home/abc.txt ---> Running in b7f920c860a3 Removing intermediate container b7f920c860a3 ---> 991234fb1d8e Successfully built 991234fb1d8e Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls -l /home total 0 #并没有创建abc.txt文件 [root@localhost docker]# mkdir test #需要再创建一个子Dockerfile文件,所以创建一个test目录来存放子Dockerfile文件,和父镜像的Dockerfile隔离开来 [root@localhost docker]# cd test [root@localhost test]# pwd /root/docker/test [root@localhost test]# vim Dockerfile FROM my_test:latest #写父镜像 [root@localhost test]# docker build -t mytest2 . Sending build context to Docker daemon 2.048kB Step 1/1 : FROM my_test:latest # Executing 1 build trigger ---> Running in b035d0948b03 Removing intermediate container b035d0948b03 ---> 21e0f69c2eed Successfully built 21e0f69c2eed Successfully tagged mytest2:latest [root@localhost test]# docker run -it mytest2 ls -l /home total 0 -rw-r--r-- 1 root root 0 Aug 29 18:58 abc.txt #此时会创建abc.txt文件

最后用两张张图解释常用指令的意义(有一说一图是从网上ba的,可能有点模糊不知道你们能不能看清,概括的挺好)

dockerfile常用指令【dockerfile是什么意思】

dockerfile常用指令【dockerfile是什么意思】

制图网(www.makepic.net),专业的logo免费设计在线生成网站,全自动智能化logo设计,商标设计,logo在线生成!

欢迎使用制图网制作属于您公司自己的logo,不仅专业而且经济实惠,全方位满足您公司品牌化、视觉化的需求。

输入激活码

只需简单一步,使用微信扫码(或长按识别二维码)并在任一小程序首页根据提示获取激活码! chatGPT中文网页版,无需注册,快来体验全网最火爆的人工智能应用!
刻字印章刻章子定刻光敏盖章印订制(淘宝商家)

输入激活码

只需简单一步,使用微信扫码(或长按识别二维码)并在任一小程序首页根据提示获取激活码! chatGPT中文网页版,无需注册,快来体验全网最火爆的人工智能应用!
刻字印章刻章子定刻光敏盖章印订制(淘宝商家)
请使用手机淘宝进行扫码