设为首页 收藏本站
查看: 778|回复: 0

[经验分享] 如何创建Nginx服务的Centos Docker镜像

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-2 09:17:13 | 显示全部楼层 |阅读模式

Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性,下面我就分步骤向大家介绍如何创建带Nginx服务的Centos Docker镜像.

基础镜像:

[iyunv@localhost ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

sshd-centos         latest              64136bdc0cc8        45 hours ago        261.8 MB

centos              latest              0f73ae75014f        5 weeks ago         172.3 MB

其中镜像sshd-centos是以镜像centos为基础的开放SSH服务的镜像。

第一部分,手工配置并生成镜像

一 、以镜像sshd-centos为基础新建容器,并指定容器的ssh端口22映射到宿主机的2222端口上
docker run -d -p 2222:22 sshd-centos /usr/sbin/sshd -D
查看容器运行情况:
[iyunv@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                  NAMES
ed9361b598c8        sshd-centos         "/usr/sbin/sshd -D"   16 minutes ago      Up 16 minutes       0.0.0.0:2222->22/tcp   distracted_mclean
二、在宿主机上通过ssh登录容器
ssh localhost -p 222211

如果提示没有ssh命令请安装openssh-clients
yum install -y openssh-clients11

三、下载Nginx源码包,编译安装
1、安装wget
yum install -y wget11

2、下载源码包
cd /usr/local/src
wget http://nginx.org/download/nginx-1.8.0.tar.gz1212

3、解压源码包
tar -zxvf nginx-1.8.0.tar.gzcd nginx-1.8.0

4、安装gcc 、make编译器和Nginx依赖包
由于下载的docker镜像是简化版,所以连最基本的gcc和make都没有带,只好自已安装; 同时需要安装Nginx依赖包pcre和zlib
yum install -y gcc make pcre-devel zlib-devel11

5、编译
./configure   --prefix=/usr/local/nginx   --with-pcremake
make install12341234

四,启动nginx服务
/usr/local/nginx/sbin/nginx

查看是安装是否正常,能否访问默认页:

[iyunv@ed9361b598c8 nginx-1.8.0]# curl localhost<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>

测试成功!

五,修改Ngnix配置文件
设置生成容器时,Nginx以非daemon启动
echo "\ndaemon off;">>/usr/local/nginx/conf/nginx.conf11

六、编写启动ssh和Nginx服务的脚本
cd /usr/local/sbin
vi run.sh1212

脚本内容
#!/bin/bash/usr/sbin/sshd &
/usr/local/nginx/sbin/nginx123123

改变脚本权限,使其可以运行
chmod 755 run.sh11

七、创建带有Nginx和ssh服务的镜像
1、查看当前容器的 Container ID
[iyunv@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                  NAMES
ed9361b598c8        sshd-centos         "/usr/sbin/sshd -D"   16 minutes ago      Up 16 minutes       0.0.0.0:2222->22/tcp   distracted_mclean123123

2、根据容器CONTAINER ID生成新的镜像
docker commit ed9361b598c8 nginx:centos

3、查看新生成的镜像
[iyunv@localhost ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEnginx               centos              b61204959427        10 seconds ago      369.9 MBsshd-centos         latest              64136bdc0cc8        45 hours ago        261.8 MBcentos              latest              0f73ae75014f        5 weeks ago         172.3 MB1234512345

八、根据新生成的镜像生成容器
分别映射容器的22端口和80端口到宿主机的2223端口和8000端口
docker run -d -p 2223:22 -p 8000:80 nginx:centos /usr/local/sbin/run.sh11

查看生成的容器:
[iyunv@localhost ~]#docker ps -aCONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                        NAMES
f5a87e085a0b        nginx:centos        "/usr/local/sbin/run   6 seconds ago       Up 5 seconds        0.0.0.0:2223->22/tcp, 0.0.0.0:8000->80/tcp   

stoic_kirch
ed9361b598c8        sshd-centos         "/usr/sbin/sshd -D"    37 minutes ago      Up 37 minutes       0.0.0.0:2222->22/tcp                        

distracted_mclean


测试Nginx服务:
[iyunv@localhost ~]# curl localhost:8000<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>12345678910111213141516171819202122232425261234567891011121314151617181920212223242526

测试ssh服务
[iyunv@localhost ~]# ssh localhost -p 2223The authenticity of host '[localhost]:2223 ([::1]:2223)' can't be established.RSA key fingerprint is d7:fd:3d:40:46:b6:0c:c9:ee:f1:fb:9e:08:c4:12:57.Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:2223' (RSA) to the list of known hosts.root@localhost's password:123456123456

测试通过!
第二部分,通过Dockerfile生成镜像
在宿主机上准备的文件清单:
Dockerfile#启动ssh和apache服务的角本run.sh123123

以上文件都放到/root/nginx_centos目录下
mkdir -p /root/nginx_centoscd /root/nginx_centos1212

一、准备run.sh文件
在/root/nginx_centos目录新建run.sh
vim run.sh11

角本内容如下:
#!/bin/bash/usr/sbin/sshd &
/usr/local/nginx/sbin/nginx123123

二、准备Dockerfile
在/root/nginx_centos目录新建Dockerfile
vim Dockerfile11

文件内容如下:
#新生成的镜像是基于sshd:dockerfile镜像FROM sshd-centos
MAINTAINER by cmzsteven
WORKDIR /usr/local/src#安装wgetRUN yum install -y  wget#下载并解压源码包RUN wget http://nginx.org/download/nginx-1.8.0.tar.gzRUN tar -zxvf nginx-1.8.0.tar.gz
WORKDIR nginx-1.8.0#编译安装nginxRUN yum install -y gcc make pcre-devel zlib-devel
RUN ./configure   --prefix=/usr/local/nginx   --with-pcre
RUN make
RUN make install#启动Nginx服务RUN /usr/local/nginx/sbin/nginx#修改Nginx配置文件,以非daemon方式启动RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf#复制服务启动脚本并设置权限ADD run.sh /usr/local/sbin/run.sh
RUN chmod 755 /usr/local/sbin/run.sh#设置生成容器时需要执行的脚本CMD ["/usr/local/sbin/run.sh"]#开放22、80、443端口EXPOSE 22EXPOSE 80EXPOSE 4431234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728

需要注意的是:在Dockerfile文件中更换当前目录不可以用“cd”命令,而要改用“WORKDIR”.
三、根据Dockerfile生成镜像

docker build -t nginx_dockerfile:centos .

查看镜像:
[iyunv@localhost nginx_centos]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEnginx_dockerfile    centos              9ad55461b2fe        5 minutes ago       386.1 MBnginx               centos              b738cec02b29        47 minutes ago      369.9 MBsshd-centos         latest              64136bdc0cc8        46 hours ago        261.8 MBcentos              latest              0f73ae75014f        5 weeks ago         172.3 MB123456123456

四、根据镜像生成的容器并进行测试
1、生成新的容器

[iyunv@localhost nginx_centos]#docker run -d -p 2224:22 -p 8001:80 -p 4443:443 nginx_dockerfile:centos /usr/local/sbin/run.sh

将容器的22端口、80端口和443端口分别映射到到宿主机上的2224端口、8001端口和4443端口,并运行服务脚本。
也可以使用-P参数来让系统随机指定端口映射到22、80和443端口:
docker run -d -P nginx_dockerfile:centos11

因为在Dockerfile中指定了EXPOSE所以系统会自动将指定的端口映射出来;同时使用CMD来指定生成容器时所需要执行的角本,所以这里省略了“/usr/local/sbin/run.sh”。
2、查看新生成的容器:
[iyunv@localhost nginx_centos]# docker ps -aCONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS                                       

NAMES
c69d42541f52        nginx_dockerfile:centos   "/usr/local/sbin/run   26 seconds ago      Up 25 seconds       0.0.0.0:2224->22/tcp, 0.0.0.0:8001->80/tcp, 0.0.0.0:4443->443/tcp

high_colden
f5a87e085a0b        nginx:centos              "/usr/local/sbin/run   49 minutes ago      Up 49 minutes       0.0.0.0:2223->22/tcp, 0.0.0.0:8000->80/tcp  

stoic_kirch
ed9361b598c8        sshd-centos               "/usr/sbin/sshd -D"    About an hour ago   Up About an hour    0.0.0.0:2222->22/tcp                        

distracted_mclean12345678910111213141234567891011121314

3、测试
测试nignx:
[iyunv@localhost nginx_centos]# curl localhost:8001<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
    body {        width: 35em;        margin: 0 auto;        font-family: Tahoma, Verdana, Arial, sans-serif;    }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>12345678910111213141516171819202122232425261234567891011121314151617181920212223242526

测试成功!
测试ssh
[iyunv@localhost nginx_centos]# ssh localhost -p 2224The authenticity of host '[localhost]:2224 ([::1]:2224)' can't be established.RSA key fingerprint is d7:fd:3d:40:46:b6:0c:c9:ee:f1:fb:9e:08:c4:12:57.Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:2224' (RSA) to the list of known hosts.root@localhost's password:12345671234567

测试成功!



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-294585-1-1.html 上篇帖子: CentOS6.8编译安装Nginx1.10.2+MySQL5.7.16+PHP7.0.12 下篇帖子: Nginx查看 并发连接数 如何
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表