Dockerfile
编写Dockerfile
[root@server01 docker]# cd /data/dockerfile/
[root@server01 dockerfile]# more Dockerfile
FROM docker.io/dockerstudy007/nginx:curl
USER nginx
WORKDIR /usr/share/nginx/html
[root@server01 dockerfile]# docker build . -t docker.io/dockerstudy007/nginx:curl_with_user_workdir
ADD和EXPOSE命令
[root@server01 dockerfile]# cp /data/html/index.html ./
[root@server01 dockerfile]# vim Dockerfile
[root@server01 dockerfile]# docker build . -t dockerstudy007/nginx:curl_with_index_expose
[root@server01 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>
启动这个我们添加了index.html的容器
[root@server01 dockerfile]# docker run --rm -ti --name nginx_index -P dockerstudy007/nginx:curl_with_index_expose /bin/bash
root@9084a031b9a1:/# nginx -g "daemon off;"
启动容器
[root@server01 dockerfile]# docker run --rm -d --name nginx_index -P dockerstudy007/nginx:curl_with_index_expose
如果-p(小写映射端口,EXPOSE不生效)
[root@server01 dockerfile]# more Dockerfile
FROM dockerstudy007/centos:mycentos
ENV VER 9.11.4-16.P2.el7_8.6
RUN yum install bind-$VER -y
# mycentos是以centos7为基础设置网络后的系统
Vim /etc/profile
export ftp_proxy=http://10.11.0.148:808
export socks_proxy="socks://10.11.0.148:808/"
export http_proxy=http://10.11.0.148:808
export https_proxy=https://10.11.0.148:808
[root@server01 dockerfile]# docker build . -t dockerstudy007/bind:v9.9.4_with_env_run
[root@server01 ~]# cd /data/dockerfile/
[root@server01 dockerfile]# more Dockerfile
FROM dockerstudy007/centos:mycentos
ENV VER 9.11.4-16.P2.el7_8.6
RUN yum install bind-$VER -y
# 打包生成镜像
[root@server01 dockerfile]# docker build . -t dockerstudy007/bind:v9.11.4_with_env_run
# 交互式进入这个镜像为基准的容器
[root@server01 dockerfile]# docker run -it --rm dockerstudy007/bind:v9.11.4_with_env_run /bin/bash
Run是在构建镜像的时候执行的指令
cmd是启动容器时运行的指令
CMD指令
# RUN指令在镜像中安装apache,cmd指令是启动容器时执行的指令
[root@server01 dockerfile]# more Dockerfile
FROM dockerstudy007/centos:mycentos
RUN yum install httpd -y
CMD ["httpd","-D","FOREGROUND"]
[root@server01 dockerfile]# docker build . -t dockerstudy007/httpd:test
# 启动带httpd的容器
[root@server01 dockerfile]# docker run -d --rm --name myhttpd -p83:80 dockerstudy007/httpd:test
Entrypoint指令
# 使用nginx测试,使用entrypoint.sh启动nginx服务,注意要添加entrypoint.sh的可执行权限
[root@server01 dockerfile]# cat Dockerfile
FROM dockerstudy007/centos:mycentos
ADD entrypoint.sh /entrypoint.sh
RUN yum install epel-release -q -y && yum install nginx -y
ENTRYPOINT /entrypoint.sh
[root@server01 dockerfile]# cat entrypoint.sh
#!/bin/bash
/sbin/nginx -g "daemon off;"
[root@server01 dockerfile]# chmod +x entrypoint.sh
[root@server01 dockerfile]# docker build . -t dockerstudy007/nginx:mynginx
# 运行这个容器
[root@server01 dockerfile]# docker run --rm --name=mynginx01 -p84:80 dockerstudy007/nginx:mynginx
虽然不能打开首页,实际上nginx已经运行成功
综合示例
[root@server01 dockerfile]# ls
demo.od.com.conf Dockerfile entrypoint.sh index.html
准备文件,Nginx虚拟主机配置:
[root@server01 dockerfile]# cat demo.od.com.conf
server {
listen 80;
server_name demo.od.com;
root /usr/share/nginx/html;
}
网站首页文件:
[root@server01 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>
主配置Dockerfile文件
[root@server01 dockerfile]# more Dockerfile
FROM dockerstudy007/nginx:mynginx
USER root
ENV WWW /usr/share/nginx/html
ENV CONF /etc/nginx/conf.d
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
echo 'Asia/Shanghai' >/etc/timezone
WORKDIR $WWW
ADD index.html $WWW/index.html
ADD demo.od.com.conf $CONF/demo.od.com.conf
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
[root@server01 dockerfile]# docker build . -t dockerstudy007/nginx:nginx_baidu
测试
[root@server01 dockerfile]# docker run --rm -p80:80 dockerstudy007/nginx:nginx_baidu
创建联合网络的镜像
制作了一个包含ip命令基于centos7的mycentos镜像
创建centos07这个容器
[root@server01 ~]# docker run -it --rm --name=centos07 mycentos /bin/bash
运行基于centos07容器网络的容器,这样就生成了一个联合网络的容器centos08
[root@server01 ~]# docker run -it --rm --name=centos08 --net=container:centos07 mycentos /bin/bash
这是共享网络命名空间的一种网络组织方式
有时候也需要共享存储或者共享目录的命名空间