用Docker构建Nginx镜像

1构建Nginx镜像

1建立工作目录

[root@localhost ]# mkdir 1nginx

[root@localhost 1nginx]# cd 1nginx/

[root@localhost 1nginx]# ls

Dockerfile  nginx-1.14.2.tar.gz  run.sh

 

2,创建并编写Dockfile文件

[root@localhost 1nginx]# vim Dockerfile

#创建者的基本信息

MAINTAINER adc@163.com

#解决依赖关系

RUN yum install -y  make gcc openssl-devel pcre-devel zlib-devel

#添加nginx用户

RUN  groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx

#添加nginx安装包,并默认解压

ADD nginx-1.14.2.tar.gz /usr/local/src/

#添加启动nginx服务脚本

ADD run.sh /run.sh

#定义工作目录

WORKDIR /usr/local/src/nginx-1.14.2/

#执行命令进行安装

RUN ./configure \

          --prefix=/usr/local/nginx \

            --sbin-path=/usr/sbin/nginx \

              --conf-path=/etc/nginx/nginx.conf \

                --error-log-path=/var/log/nginx/error.log \

                  --http-log-path=/var/log/nginx/access.log \

                    --pid-path=/var/run/nginx/nginx.pid  \

                      --lock-path=/var/lock/nginx.lock \

                        --user=nginx \

                          --group=nginx \

                            --with-http_ssl_module \

                              --with-http_flv_module && make && make install

 

#定义输出命令

CMD ["/run.sh"]

#定义输出端口

EXPOSE 80

 

3.编写执行脚本内容

[root@localhost 1nginx]# vim run.sh

#!/bin/bash

sed -i '/daemon off/d' /etc/nginx/nginx.conf

#设置nginxdaemon守护进程

echo "daemon off;" >> /etc/nginx/nginx.conf

#杀掉nginx服务

killall nginx

#启动nginx服务

/usr/sbin/nginx -c /etc/nginx/nginx.conf

 

  1. 生成镜像

[root@localhost 1nginx]# docker build -t nginx.web .

[root@localhost 1nginx]# docker images

 

  1. 启动容器进行测试

 

[root@localhost 1nginx]# docker run -d -p 81:80 nginx.web

[root@localhost 1nginx]# docker ps

[root@localhost 1nginx]# curl 192.168.30.24:81

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

 

posted @ 2019-05-29 15:05  #赵程#  阅读(184)  评论(0编辑  收藏  举报