Dockerfile构建镜像( 二 )

5、ENTRYPOINT(设置container启动时执行的操作)
格式:
ENTRYPOINT command param1 param2ENTRYPOINT ["executable","param1","param2"]例1:ENTRYPOINT指令不会被docker run提供的参数覆盖
[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[root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/3 : FROM centos:latest ---> 9f38484d220fStep 2/3 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46Step 3/3 : ENTRYPOINT ls /root /sys ---> Running in 67be0ac881cbRemoving intermediate container 67be0ac881cb ---> e51f45c181acSuccessfully built e51f45c181acSuccessfully tagged my_test:latest[root@localhost docker]# docker run -it my_test/root:anaconda-ks.cfg/sys:block class devices fs kernel powerbus dev firmware hypervisor module[root@localhost docker]# docker run -it my_test ls /home/root:anaconda-ks.cfg/sys:block class devices fs kernel powerbus dev firmware hypervisor module例2:当一个Dockerfile文件中有两个或多个ENTRYPOINT指令时,只执行最后一条ENTRYPOINT指令
[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 /sysENTRYPOINT ["ls","/home"][root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/4 : FROM centos:latest ---> 9f38484d220fStep 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46Step 3/4 : ENTRYPOINT ls /root /sys ---> Using cache ---> e51f45c181acStep 4/4 : ENTRYPOINT ["ls","/home"] ---> Running in 95c2491f9b3bRemoving intermediate container 95c2491f9b3b ---> 6196eaff66d3Successfully built 6196eaff66d3Successfully tagged my_test:latest[root@localhost docker]# docker run -it my_testa.txt例3:当一个Dockerfile文件中既有CMD指令又有ENTRYPOINT指令时,CMD指令将被作为ENTRYPOINT指令的参数
[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 /sysCMD ["-l"]ENTRYPOINT ["ls","/home"][root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/4 : FROM centos:latest ---> 9f38484d220fStep 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46Step 3/4 : CMD ["-l"] ---> Using cache ---> 4c5cf79c279dStep 4/4 : ENTRYPOINT ["ls","/home"] ---> Using cache ---> 14d643d73d81Successfully built 14d643d73d81Successfully tagged my_test:latest[root@localhost docker]# docker run -it my_testtotal 0-rw-r--r-- 1 root root 0 Aug 29 17:09 a.txt6、USER(设置container的用户,默认是root用户,这个root不是那个root)
格式:
USER username/uid例:
[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[root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/3 : FROM centos:latest ---> 9f38484d220fStep 2/3 : RUN useradd admin ---> Running in a4847a39ab06Removing intermediate container a4847a39ab06 ---> 3759bdc0589cStep 3/3 : USER admin ---> Running in 80e367ffe699Removing intermediate container 80e367ffe699 ---> c95196915358Successfully built c95196915358Successfully 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 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 adminEXPOSE 80[root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/2 : FROM centos:latest ---> 9f38484d220fStep 2/2 : EXPOSE 80 ---> Running in 75dda1ff5393Removing intermediate container 75dda1ff5393 ---> f6759e9df32dSuccessfully built f6759e9df32dSuccessfully 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 #映射到宿主机的807207f9ae8a2c2932df727854cecb6b16d6b09ffecd1411bf42d03ffb1c271803[root@localhost docker]# docker run -dit -p 80:80 my_test /bin/bash83c40826648c13d23c706db3474d1eb541261638e5c1415abaebfb7a37d758ca[root@localhost docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES83c40826648c my_test "/bin/bash" About a minute ago Up About a minute 0.0.0.0:80->80/tcp silly_snyder7207f9ae8a2c my_test "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:32769->80/tcp youthful_kirch5b790387fd75 my_test "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:32768->80/tcp focused_bose


推荐阅读