「Dockerfile」手动制作Nginx镜像

100人浏览   2024-08-15 11:32:20

本文章使用centos:7为基础镜像,根据各个公司实际情况而定,也可以使用比较流行的基础镜像alpine或者直接使用nginx官方镜像。

一、制作nginx_base镜像

FROM centos:7
LABEL maintainer mayi

RUN yum -y install wget && \
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak && \
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
    yum makecache && \
    yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    crontabs iproute net-tools telnet curl  && \
    yum clean all && \
    rm -rf /var/cache/yum/* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    groupadd nginx && useradd -g nginx nginx
    
COPY nginx /etc/logrotate.d/
COPY nginx-1.18.0.tar.gz /
RUN tar zxf nginx-1.18.0.tar.gz && \
    cd nginx-1.18.0 && \
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-stream && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/* && \
    echo "ok" >> /usr/local/nginx/html/status.html && \
    rm -rf /nginx* && \ 
    rm -f /usr/local/nginx/conf/nginx.conf && \ 
    mkdir -p /var/log/nginx && \
    chmod 444 /etc/logrotate.d/nginx
    
COPY nginx.conf /usr/local/nginx/conf/nginx.conf

二、封装业务【方便制作CICD】

#通过以上生成基础镜像  harbor_address为变量,可通过jenkins传入
FROM harbor_address/base-image/base_nginx:latest
LABEL maintainer mayi

ENV PATH $PATH:/usr/local/nginx/sbin
ENV LANG en_US.utf8

COPY web.conf /usr/local/nginx/conf/conf.d/web.conf 
COPY dist /usr/local/nginx/html/dist
RUN chown -R nginx:nginx /usr/local/nginx/html/ 

WORKDIR /usr/local/nginx
EXPOSE 80

CMD [ "nginx", "-g","daemon off;" ] 


#备注

以上情况都无法使用docker logs -f 查看输出日志

可以将nginx日志输出到/dev/stdout和/dev/stderr

error_log /dev/stderr;

access_log /dev/stdout log_json;

相关推荐