定义
Dockerfile is nothing but the source code for building Docker image.
- Docker can build images automatically by reading the instructions from Dockerfile.
- A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
- Using docker build ,user can create an automated build that executes several command line instructions in succession.
Dockerfile只是构建Docker镜像的源代码。
- Docker可以通过读取Dockerfile中的指令自动生成镜像。
- Dockerfile是一个文本文档,包含用户可以在命令行上调用的所有命令来组装镜像。
- 使用docker build,用户可以创建一个连续执行多个命令行指令的自动构建。
4组核心的Dockerfile指令(8个)
USER/WORKDIR指令
[root@db01 ~]# vim /data/dockerfile/Dockerfile
FROM docker.io/zkinogg/nginx:v1.12.2 #镜像来源
USER nginx # 规定用什么用户起
WORKDIR /usr/share/nginx/html # 在这个目录下执行WORKDIR可理解为shell中的cd
[root@db01 dockerfile]# docker build . -t docker.io/zkinogg/nginx:v1.12.2_with_user_workdir #构建本目录下的dockerfile,-t是加一个tag标签with_user_workdir 加全镜像
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM docker.io/zkinogg/nginx:v1.12.2
---> 4037a5562b03
Step 2/3 : USER nginx
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in e914132359e3
Removing intermediate container e914132359e3
---> 3bd7d552ff14
Step 3/3 : WORKDIR /usr/share/nginx/html
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in fbc08157b933
Removing intermediate container fbc08157b933
---> 69e30e9b8c37
Successfully built 69e30e9b8c37
Successfully tagged zkinogg/nginx:v1.12.2_with_user_workdir
[root@db01 dockerfile]# docker run --rm -ti --name nginx123 zkinogg/nginx:v1.12.2_with_user_workdir /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
nginx@db33bc26a53b:/usr/share/nginx/html$ whoami
nginx
nginx@db33bc26a53b:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
ADD/EXPOSE指令
[root@db01 ~]# vim /data/dockerfile/Dockerfile
FROM zkinogg/nginx:v1.12.2 #镜像来源
ADD index.html /usr/share/nginx/html/index.html # 把当前目录的index.html放到镜像里的nginx 的html目录下
EXPOSE 80 # 容器起来默认监听80端口
[root@db01 dockerfile]# cp /root/html/index.html .
[root@db01 dockerfile]# ll
total 8
-rw-r--r-- 1 root root 95 Aug 25 17:40 Dockerfile
-rw-r--r-- 1 root root 2381 Aug 25 17:48 index.html
[root@db01 dockerfile]# docker build . -t zkinogg/nginx:v1.12.2_with_index_exposeSending build context to Docker daemon 5.12kB
Step 1/3 : FROM docker.io/zkinogg/nginx:v1.12.2
---> 4037a5562b03
Step 2/3 : ADD index.html /usr/share/nginx/html/index.html
---> f441fe9bd8c9
Step 3/3 : EXPOSE 80
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 86d4ea49e0c9
Removing intermediate container 86d4ea49e0c9
---> b1ed1b54051b
Successfully built b1ed1b54051b
Successfully tagged zkinogg/nginx:v1.12.2_with_index_expose
# 启动容器发现警告
[root@db01 dockerfile]# docker run --rm -d --name nginx123 -P zkinogg/nginx:v1.12.2_with_index_expose
WARNING: IPv4 forwarding is disabled. Networking will not work.
0c723fc9d9004d3ff6f2ef45d40ff6c6d99a33c957def9b0ba5d8b08be0bfd36
# WARNING: IPv4 forwarding is disabled. Networking will not work.
#警告:IPv4转发已禁用。网络是行不通的。
# 修改网卡配置
[root@db01 dockerfile]# vim /usr/lib/sysctl.d/00-system.conf
##添加如下代码
net.ipv4.ip_forward=1
# 重启网卡
[root@db01 dockerfile]# systemctl restart network
# 查看启动端口
[root@db01 dockerfile]# netstat -lntp
tcp6 0 0 :::32769 :::* LISTEN 15486/docker-proxy
## 浏览器访问10.0.0.51:32769
RUN/ENV指令
[root@db01 dockerfile]# vim Dockerfile
FROM centos:7
ENV VER 9.11.4 #环境变量
RUN yum install bind-$VER -y #构建镜像时执行的操作
[root@db01 dockerfile]# docker build . -t zkinogg/bind:v9.11.4_with_env_run
Sending build context to Docker daemon 5.12kB
Step 1/3 : FROM centos:7
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
---> 7e6257c9f8d8
Step 2/3 : ENV VER 9.11.4
---> Running in a5afe97480bc
Removing intermediate container a5afe97480bc
---> 9640cfac309b
Step 3/3 : RUN yum install bind-$VER -y
Complete!
Removing intermediate container fb51013af5dc
---> 480da1414393
Successfully built 480da1414393
Successfully tagged zkinogg/bind:v9.11.4_with_env_run
[root@db01 dockerfile]# docker run -it --rm zkinogg/bind:v9.11.4_with_env_run /bin/bash
[root@08f816f33615 /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@08f816f33615 /]# printenv
HOSTNAME=08f816f33615
TERM=xterm
LS_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;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=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;36:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/root
VER=9.11.4
_=/usr/bin/printenv
[root@08f816f33615 /]# rpm -qa bind
bind-9.11.4-16.P2.el7_8.6.x86_64
CMD/ENTRYPOINT指令
[root@db01 dockerfile]# vim Dockerfile
FROM centos:7
RUN yum install httpd -y # 构建镜像时安装apache
CMD ["httpd","-D","FOREGROUND"] # 启动httpd时用httpd -d foreground 指定命令启动不用bash启动
[root@db01 dockerfile]# docker build . -t zkinogg/httpd:test
Sending build context to Docker daemon 5.12kB
Step 1/3 : FROM centos:7
---> 7e6257c9f8d8
Step 2/3 : RUN yum install httpd -y
---> Running in 0939817461b5
Complete!
Removing intermediate container 0939817461b5
---> eeb0e6b4c264
Step 3/3 : CMD ["httpd","-D","FOREGROUND"]
---> Running in 7b22e7568783
Removing intermediate container 7b22e7568783
---> 613739d5caab
Successfully built 613739d5caab
Successfully tagged zkinogg/httpd:test
[root@db01 dockerfile]# docker run -d --rm --name myhttpd -p83:80 zkinogg/httpd:test
2e34c9932332551bf72cb8c2fe04fbb96e1b03247dc251dce811a208aa29550c
[root@db01 dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e34c9932332 zkinogg/httpd:test "httpd -D FOREGROUND" 24 seconds ago Up 24 seconds 0.0.0.0:83->80/tcp myhttpd
0c723fc9d900 zkinogg/nginx:v1.12.2_with_index_expose "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:32769->80/tcp nginx123
3b59532c1937 zkinogg/nginx:v1.12.2 "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:82->80/tcp nginx_html
97e1bc05d976 zkinogg/nginx:v1.12.2 "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:81->80/tcp mynginx
#ENTRYPOINT指令
默认的启动命令
[root@db01 dockerfile]# vim Dockerfile
FROm centos:7
ADD entrypoint.sh /entrypoint.sh
RUN yum install epel-release -q -y && yum install nginx -y
ENTRYPOINT /entrypoint.sh
[root@db01 dockerfile]# vim entrypoint.sh
/sbin/nginx -g "daemon off;"
[root@db01 dockerfile]# chmod +x entrypoint.sh
[root@db01 dockerfile]# docker build . -t zkinogg/nginx:mynginx
Sending build context to Docker daemon 6.144kB
Step 1/4 : FROm centos:7
---> 7e6257c9f8d8
Step 2/4 : ADD entrypoint.sh /entrypoint.sh
---> 3640b3206704
Step 3/4 : RUN yum install epel-release -q -y && yum install nginx -y
---> Running in ceff5685c77a
Complete!
Removing intermediate container ceff5685c77a
---> 47e985275c3d
Step 4/4 : ENTRYPOINT /entrypoint.sh
---> Running in 33df1ed4c2c6
Removing intermediate container 33df1ed4c2c6
---> 84b3730b8241
Successfully built 84b3730b8241
Successfully tagged zkinogg/nginx:mynginx
[root@db01 dockerfile]# docker images |grep mynginx
zkinogg/nginx mynginx 84b3730b8241 3 minutes ago 405MB
[root@db01 dockerfile]# docker run --rm -p84:80 zkinogg/nginx:mynginx
[root@db01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a9fda336744 zkinogg/nginx:mynginx "/bin/sh -c /entrypo…" 15 minutes ago Up 15 minutes 0.0.0.0:84->80/tcp pensive_driscoll
2e34c9932332 zkinogg/httpd:test "httpd -D FOREGROUND" 53 minutes ago Up 53 minutes 0.0.0.0:83->80/tcp myhttpd
0c723fc9d900 zkinogg/nginx:v1.12.2_with_index_expose "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:32769->80/tcp nginx123
3b59532c1937 zkinogg/nginx:v1.12.2 "nginx -g 'daemon of…" 15 hours ago Up 15 hours 0.0.0.0:82->80/tcp nginx_html
97e1bc05d976 zkinogg/nginx:v1.12.2 "nginx -g 'daemon of…" 16 hours ago Up 16 hours 0.0.0.0:81->80/tcp mynginx
综合实验
运行一个docker容器,在浏览器打开demo.od.com能访问到百度首页
准备Docker镜像
-
Dockerfile
[root@db01 dockerfile]# vim /data/dockerfile/Dockerfile FROM zkinogg/nginx:v1.12.2 # 镜像来源 USER root # 用root 用户起 ENV WWW /usr/share/nginx/html # 定义变量WWW为/usr/share/nginx/html ENV CONF /etc/nginx/conf.d # 定义变量CONF为/etc/nginx/conf.d RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone # 统一时区 WORKDIR $WWW # 进入/usr/share/nginx/html目录下 ADD index.html $WWW/index.html # 将当前目录下的index.html文件添加到/usr/share/nginx/html目录下 ADD demo.od.com.conf $CONF/demo.od.com.conf # 添加nginx配置文件到/etc/nginx/conf.d/下 EXPOSE 80 # 默认docker内端口是80 CMD ["nginx","-g","daemon off;"] # 启动nginx [root@db01 dockerfile]# vim demo.od.com.conf server { listen 80; server_name demo.od.com; root /usr/share/nginx/html; } [root@db01 dockerfile]# cat index.html <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html> [root@db01 dockerfile]# ll total 12 -rw-r--r-- 1 root root 87 Aug 25 20:34 demo.od.com.conf -rw-r--r-- 1 root root 298 Aug 25 20:31 Dockerfile -rw-r--r-- 1 root root 2381 Aug 25 17:48 index.html [root@db01 dockerfile]# docker build . -t zkinogg/nginx:baidu Sending build context to Docker daemon 6.144kB Step 1/9 : FROM zkinogg/nginx:v1.12.2 ---> 4037a5562b03 Step 2/9 : USER root ---> Running in cc0106ff5c09 Removing intermediate container cc0106ff5c09 ---> 043182a1026f Step 3/9 : ENV WWW /usr/share/nginx/html ---> Running in 08862b85196c Removing intermediate container 08862b85196c ---> 01ad1046666b Step 4/9 : RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone ---> Running in bffe1583a6d8 Removing intermediate container bffe1583a6d8 ---> 914e396af9ce Step 5/9 : WORKDIR $WWW ---> Running in 6bb224376a76 Removing intermediate container 6bb224376a76 ---> cb9ea6fba67a Step 6/9 : ADD index.html $WWW/index.html ---> bd82d3280c27 Step 7/9 : ADD demo.od.com.conf $CONF/demo.od.com.conf ---> 1e2355348038 Step 8/9 : EXPOSE 80 ---> Running in 9ee1d986b33c Removing intermediate container 9ee1d986b33c ---> 9d23fff44218 Step 9/9 : CMD ["nginx","-g","daemon off;"] ---> Running in 5bda84abf5a2 Removing intermediate container 5bda84abf5a2 ---> 3438c0392773 Successfully built 3438c0392773 Successfully tagged zkinogg/nginx:baidu [root@db01 dockerfile]# docker run --rm -p80:80 zkinogg/nginx:baidu ##添加本地域名解析 windows系统的hosts文件中添加 10.0.0.51 demo.od.com 保存退出 ,然后访问浏览器
Docker网络模型
-
NAT(默认)
-
NONE
-
docker run –net=none
-
-
Host
-
docker run –net=host
-
-
联合网络
-