一、dockerfile 概述
二、USER\WORKDIR 命令-----创建一个dockerfile文件[root@docker dockerfile]# vi Dockerfile #必须文件为DockerfileFROM nginx:latest #使用那个镜像作为底包 USER nginx #指定那个用户运行WORKDIR /usr/share/nginx/html #指定运行的时的目录位置-----生成一个镜像[root@docker dockerfile]# docker build . -t myselfnginx:latestSending build context to Docker daemon 2.048kBStep 1/3 : FROM nginx:latest ---> f7bb5701a33cStep 2/3 : USER nginx ---> Running in 846447177e73Removing intermediate container 846447177e73 ---> 98d5ad96fdfaStep 3/3 : WORKDIR /usr/share/nginx/html ---> Running in e500aea99e2cRemoving intermediate container e500aea99e2c ---> a6e377307258Successfully built a6e377307258Successfully tagged myselfnginx:latest[root@docker dockerfile]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyselfnginx latest a6e377307258 26 seconds ago 126MBzhoumingkang/nginx curl d1b81c37853c 25 hours ago 152MBmyalpine latest_with_hello.txt 3ce9b8b899ba 2 days ago 5.59MBnginx latest f7bb5701a33c 11 days ago 126MBzhoumingkang/alpine v3.10.3 cc0abc535e36 2 weeks ago 5.59MBalpine latest cc0abc535e36 2 weeks ago 5.59MBalpine 3.9.4 055936d39205 8 months ago 5.53MBhello-world latest fce289e99eb9 12 months ago 1.84kB[root@docker dockerfile]# docker run -ti --rm myselfnginx:latest /bin/bashnginx@51ddb8d7c3e3:/usr/share/nginx/html$ whoaminginxnginx@51ddb8d7c3e3:/usr/share/nginx/html$ pwd/usr/share/nginx/htmlnginx@51ddb8d7c3e3:/usr/share/nginx/html$ 三、ADD\EXPOSE 命令-----创建一个dockerfile文件[root@docker dockerfile]# cat Dockerfile FROM nginx:latestADD index.html /usr/share/nginx/htmlEXPOSE 80-----生成一个镜像docker build . -t nginx:with_add_exposeSending build context to Docker daemon 5.12kBStep 1/3 : FROM nginx:latest ---> f7bb5701a33cStep 2/3 : ADD index.html /usr/share/nginx/html ---> 7e2b905eca85Step 3/3 : EXPOSE 80 ---> Running in f13c2538a707Removing intermediate container f13c2538a707 ---> d44bd64abd82Successfully built d44bd64abd82Successfully tagged nginx:with_add_expose[root@docker dockerfile]# docker build . -t centos7:with_install_bind-----通过该镜像运行容器[root@docker dockerfile]# docker run --rm -d -P nginx:with_add_expose #大写P为宿主机随机生成一个端口指向内部80端口f19df2f5124538faf3660079e9d6bd5dbb10d54a75f3233847164caa0ef7c44c[root@docker dockerfile]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf19df2f51245 nginx:with_add_expose "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:32770->80/tcp nervous_colden-----登陆容器查看index.html文件是否存在[root@docker dockerfile]# docker exec -ti nervous_colden /bin/bashroot@f19df2f51245:/# cd /usr/share/nginx/html/root@f19df2f51245:/usr/share/nginx/html# ls50x.html index.html四、RUN\ENV 命令-----创建Dockerfile文件[root@docker dockerfile]# cat Dockerfile FROM centos:centos7ENV VER 9.11.4RUN yum -y install bind-$VER #RUN 命令是在我们构建镜像时运行的命令,并不是指在容器里运行命令-----通过该镜像运行容器,并查环境变量与安装包是否已经成功[root@docker dockerfile]# docker run -ti centos7:with_install_bind /bin/bash[root@647dbdb37662 /]# envHOSTNAME=647dbdb37662TERM=xtermLS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01r=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42w=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36ATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binPWD=/SHLVL=1HOME=/rootVER=9.11.4_=/usr/bin/env[root@647dbdb37662 /]# rpm -qa bindbind-9.11.4-9.P2.el7.x86_64[root@647dbdb37662 /]# 五、CMD 命令-----创建Dockerfile文件[root@docker dockerfile]# cat Dockerfile FROM centos:centos7RUN yum -y install httpdCMD ["httpd","-D","FOREGROUND"]#关于RUN与CMD的区别#RUN是创建镜像是运行的命令#CMD是启动容器后,帮你运行的命令[root@docker dockerfile]# docker build . -t centos7:httpd #通过Dockerfile文件创建镜像-----通过该容器运行镜像[root@docker dockerfile]# docker run -d --rm -p80:80 --name myhttpd centos7:httpd2f2c3328befbbd6fffe14d2887c7d3c334e50218de027881659aa30276a0e8bf[root@docker dockerfile]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2f2c3328befb centos7:httpd "httpd -D FOREGROUND" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp myhttpd六、ENTRYPOINT 命令-----创建Dockerfile文件[root@docker dockerfile]# cat Dockerfile FROM centos:centos7ADD entrypoint.sh /entrypoint.sh #上传当前目录上的entrypoint.sh文件,要有执行权限RUN yum -y install epel-release -q && yum -y install nginx #镜像构建时执行命令ENTRYPOINT /entrypoint.sh #定义ENTRYPOINT,并开机运行-----定义entrypoint.sh文件[root@docker dockerfile]# cat entrypoint.sh #!/bin/bash/sbin/nginx -g "daemon off;"-----构建镜像 [root@docker dockerfile]# docker build . -t centos7:mynginx-----运行容器查看效果[root@docker dockerfile]# docker run -d -p80:80 --name mynginx centos7:mynginxb8c8899bf0eb1012c925746f8d544159bbca4b99013f0aea7a7a625483d11868[root@docker dockerfile]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb8c8899bf0eb centos7:mynginx "/bin/sh -c /entrypo…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp mynginx |