美文网首页
基于ubuntu镜像制作nginx镜像的DockerFile

基于ubuntu镜像制作nginx镜像的DockerFile

作者: 枕梦_a280 | 来源:发表于2021-05-20 16:19 被阅读0次
    • 可以根据自己的需求更改 nginx 版本进行编译,修改nginx版本请修改Dockerfile文件中的变量 NGINX_VERSION
    • apt 采用清华源。
    • nginx 默认安装路径为 /opt/nginx
    • 端口根据自己的需求进行更改即可
    • nginx.conf 文件中删除了默认的端口配置和项目文件路径等,只保留了必要的配置,可在运行容器时将自己的配置挂载到 /opt/nginx/conf.d 目录下
    • 项目文件路径可根据自身需求进行挂载
    • DockerFile 如下

    这个Dockerfile采用多阶段构建
    第一个 FROM 阶段只添加了source.list文件
    第二个 FROM 阶段主要编译了nginx
    第三个 FROM 阶段主要配置了容器语言、时间区域及nginx配置文件

    cat >Dockerfile<<"AOF"
    FROM ubuntu:latest as sources.list
    
    RUN set -x && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse \n \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse \n \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse \n \
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse \n" \
    > /etc/apt/sources.list
    
    FROM ubuntu:latest as nginx.install
    ARG NGINX_VERSION="1.20.2"
    COPY --from=sources.list /etc/apt/sources.list /etc/apt/sources.list
    RUN set -x \
    && export DEBIAN_FRONTEND=noninteractive \
    && apt update -y \
    && apt install curl build-essential libtool libpcre3 libpcre3-dev zlib1g-dev libssl-dev openssl -y \
    && cd / \
    && curl -O http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz \
    && tar -xvf nginx-${NGINX_VERSION}.tar.gz \
    && cd nginx-${NGINX_VERSION} \
    && ./configure --prefix=/opt/nginx --with-http_ssl_module \
    && make \
    && make install \
    && rm -rf /opt/nginx/html/* \
    && mkdir -p /opt/nginx/conf.d
    
    FROM ubuntu:latest as nginx.config
    COPY --from=sources.list /etc/apt/sources.list /etc/apt/sources.list
    COPY --from=nginx.install /opt/nginx /opt/nginx
    RUN set -x && apt update -y && apt install language-pack-zh-hans tzdata telnet libpcre3 libpcre3-dev zlib1g-dev libssl-dev openssl -y \
    && locale-gen zh_CN.UTF-8 \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && apt -y remove language-pack-zh-hans \
    && rm -rf /var/lib/apt/lists/* \
    && cp /test.conf /opt/nginx/conf.d/test.conf \
    && echo "worker_processes 8; \n \
    events { \n \
    worker_connections 65535; \n \
    } \n \
    daemon off; \n \
    http { \n \
        include mime.types; \n \
        default_type application/octet-stream; \n \
        sendfile on; \n \
        keepalive_timeout 65; \n \
        server_tokens off; \n \
        tcp_nopush on; \n \
        tcp_nodelay on; \n \
        server_names_hash_bucket_size 128; \n \
        server_names_hash_max_size 512; \n \
        client_body_timeout 15s; \n \
        send_timeout 60s; \n \
        include /opt/nginx/conf.d/*.conf; \n \
        log_format log_json '{ "@timestamp": "$time_iso8601", ' \n \
                            '"remote_addr": "$remote_addr", ' \n \
                            '"referer": "$http_referer", ' \n \
                            '"request": "$request", ' \n \
                            '"status": $status, ' \n \
                            '"bytes": $body_bytes_sent, ' \n \
                            '"agent": "$http_user_agent", ' \n \
                            '"x_forwarded": "$http_x_forwarded_for", ' \n \
                            '"up_addr": "$upstream_addr",' \n \
                            '"up_host": "$upstream_http_host",' \n \
                            '"up_resp_time": "$upstream_response_time",' \n \
                            '"request_time": "$request_time"' \n \
                            ' }'; \n \
    } \n" \
    >/opt/nginx/conf/nginx.conf
    
    ENV SET_CONTAINER_TIMEZONE="true" \
        CONTAINER_TIMEZONE="Asia/Shanghai" \
        LANG="zh_CN.UTF-8" \
        LANGUAGE="zh_CN:zh:en_US:en" \
        LC_ALL="zh_CN.UTF-8"
    
    WORKDIR /opt/nginx
    EXPOSE 80
    ENTRYPOINT ["/opt/nginx/sbin/nginx","-p","/opt/nginx"]
    AOF
    

    相关文章

      网友评论

          本文标题:基于ubuntu镜像制作nginx镜像的DockerFile

          本文链接:https://www.haomeiwen.com/subject/eshbjltx.html