Dockerfile 以变量方式配置Nginx

一、Docker filebea目录文件

  • Dockerfile #Dockerfile语法文件
  • entrypoint.sh #Nginx配置文件,以变量方式注入
  • nginx-1.20.1.tar.gz #Nginx源码包
    在这里插入图片描述
[root@docker make]# cat Dockerfile 
FROM centos:7
#Set Nginx env
ENV NGINX_VERSION="nginx-1.20.1" NGINX_DIR="/usr/local/nginx" 

#Copy Nginx source package
ADD ${NGINX_VERSION}.tar.gz /usr/local/src/
ADD entrypoint.sh /bin/

#Install compilation environment and create nginx user
RUN yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel net-tools \
    && useradd -s/bin/nologin nginx

#Set working directory
WORKDIR /usr/local/src/${NGINX_VERSION}

#Compile and install Nginx
RUN ./configure \
    --prefix=${NGINX_DIR} \
    --user=nginx \
    --group=nginx \
    --with-http_stub_status_module\
    --with-http_gzip_static_module\
    --with-http_realip_module\
    --with-http_ssl_module \
    && make \
    && make install \
    && mkdir ${NGINX_DIR}/conf/conf.d/ \
    && chown -R nginx:nginx ${NGINX_DIR}

#Set environment variables
ENV PATH="${NGINX_DIR}/sbin:${PATH}"

#Expose port 80 of the container
EXPOSE 80/tcp

WORKDIR ${NGINX_DIR}

#Run Nginx
CMD ["/bin/sh","-c","nginx -g 'daemon off;'"]
ENTRYPOINT ["entrypoint.sh"]
[root@docker make]# cat entrypoint.sh 
#!/bin/sh
#
sed -i '116 i include conf.d/*.conf;' ${NGINX_DIR}/conf/nginx.conf
if [ ${NGINX_DOC_ROOT} != "/usr/local/nginx/html" ];then
        mkdir -p ${NGINX_DOC_ROOT}
        chown -R nginx:nginx ${NGINX_DOC_ROOT}
fi

cat > ${NGINX_DIR}/conf/conf.d/www.conf << EOF
server {
        listen          ${HOST_IP:-0.0.0.0}:${HOST_PORT:-80};
        server_name     ${HOSTNAME};
        root    ${NGINX_DOC_ROOT:-/usr/local/nginx/html};
}
EOF
exec "$@"

配置变量解释:(在启动此容器时,支持以下变量配置)
NGINX_DOC_ROOT #网站根目录,默认为/usr/local/nginx/html
HOST_IP #监听的IP地址,默认为0.0.0.0
HOST_PORT #监听的端口,默认为80


二、构建镜像

[root@docker make]# docker build -t nginx:v1.20.1 ./
在这里插入图片描述

[root@docker make]# docker image ls nginx:v1.20.1
在这里插入图片描述


三、启动容器

[root@docker ~]# docker run --name nginx \
-h nginx \
-e NGINX_DOC_ROOT="/data/www_html/" \
-e HOST_PORT="8181" \
-p 8181:8181 \
-v /app/nginx/www_html/:/data/www_html/ \
-d nginx:v1.20.1

在这里插入图片描述

[root@docker ~]# docker ps -a | grep nginx
[root@docker ~]# docker port nginx

在这里插入图片描述

这样子的话,可以更快捷方便的修改Nginx的配置了,以变量的方式注入到容器中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值