Dockerfile构建镜像( 三 )

8、ENV(用于设置环境变量)
格式:
ENV <key> <value>例:
[root@localhost docker]# vim Dockerfile#FROM centosFROM 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 80ENV a Hello World!![root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/2 : FROM centos:latest ---> 9f38484d220fStep 2/2 : ENV a Hello World!! ---> Running in af33c5e13754Removing intermediate container af33c5e13754 ---> dd22ab298385Successfully built dd22ab298385Successfully tagged my_test:latest[root@localhost docker]# docker run -it my_test /bin/bash[root@7dca23648303 /]# echo $aHello 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]# lsDockerfile nginx-1.16.1.tar.gz test.txt[root@localhost docker]# vim Dockerfile#FROM centosFROM 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 /tmpADD nginx-1.16.1.tar.gz /tmpADD 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.036MBStep 1/4 : FROM centos:latest ---> 9f38484d220fStep 2/4 : ADD test.txt /tmp ---> 7fa385db5eefStep 3/4 : ADD nginx-1.16.1.tar.gz /tmp ---> 24c9c0e0877dStep 4/4 : ADD http://nginx.org/download/nginx-1.16.1.tar.gz /homeDownloading 1.033MB/1.033MB ---> b842677c7912Successfully built b842677c7912Successfully 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]# lsDockerfile nginx-1.16.1.tar.gz test.html test.txt[root@localhost docker]# vim Dockerfile FROM centos:latestCOPY test.html /[root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 1.037MBStep 1/2 : FROM centos:latest ---> 9f38484d220fStep 2/2 : COPY test.html / ---> e48a7c46ded6Successfully built e48a7c46ded6Successfully 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.html11、VOLUME(卷,指定挂载点)
格式:
VOLUME ["<mountpoint>"]12、WORKDIR(切换目录,可以多次切换,相当于cd命令)
格式:
WORKDIR Directory_url例:
[root@localhost docker]# vim Dockerfile FROM centos:latestWORKDIR /tmpRUN mkdir testWORKDIR testRUN ["touch","a.txt","b.txt"][root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 1.037MBStep 1/5 : FROM centos:latest ---> 9f38484d220fStep 2/5 : WORKDIR /tmpRemoving intermediate container 9258f95093db ---> 6b8f6f6c69f6Step 3/5 : RUN mkdir test ---> Running in e44f8d897714Removing intermediate container e44f8d897714 ---> 1dc77dd96431Step 4/5 : WORKDIR testRemoving intermediate container df09ed12198d ---> 94cb324c6118Step 5/5 : RUN ["touch","a.txt","b.txt"] ---> Running in 63f6bdcdd475Removing intermediate container 63f6bdcdd475 ---> 1ae6996b6573Successfully built 1ae6996b6573Successfully tagged my_test:latest[root@localhost docker]# docker run -it my_test ls -l /tmp/testtotal 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.txt13、ONBUILD(在子镜像中执行)
格式:
ONBUILD INSTRUCTION例:
[root@localhost docker]# vim Dockerfile [root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 1.038MBStep 1/2 : FROM centos:latest ---> 9f38484d220fStep 2/2 : ONBUILD RUN touch /home/abc.txt ---> Running in b7f920c860a3Removing intermediate container b7f920c860a3 ---> 991234fb1d8eSuccessfully built 991234fb1d8eSuccessfully tagged my_test:latest[root@localhost docker]# docker run -it my_test ls -l /hometotal 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.048kBStep 1/1 : FROM my_test:latest# Executing 1 build trigger ---> Running in b035d0948b03Removing intermediate container b035d0948b03 ---> 21e0f69c2eedSuccessfully built 21e0f69c2eedSuccessfully tagged mytest2:latest[root@localhost test]# docker run -it mytest2 ls -l /hometotal 0-rw-r--r-- 1 root root 0 Aug 29 18:58 abc.txt #此时会创建abc.txt文件


推荐阅读