# 使用Debian 作为基础镜像
FROM debian:11
# 设置时区
ENV TZ=Asia/Shanghai
# 设置NGINX版本信息变量,通过--build-arg NGINX_VERSION=1.27.0 实现版本设置
ARG NGINX_VERSION
# 设置镜像源加速
RUN sed -i "s@http.*\(com\|org\|cn\)@http://mirrors.cloud.tencent.com@g" /etc/apt/sources.list
# 更新源索引数据
RUN apt-get update
# 安装编译Nginx所需的依赖
RUN apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev
RUN apt install -y make clang openssl ca-certificates wget
# 更新系统时间,确保证书有效
RUN apt-get install -y ntp
# 设置开机自启
RUN systemctl enable ntp
# 时间同步
RUN ntpd -g
# 下载最新版本的Nginx源码
RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -O /tmp/nginx.tar.gz
# 解压源码包并进入源码目录
RUN tar -zxvf /tmp/nginx.tar.gz -C /tmp/
WORKDIR /tmp/nginx-$NGINX_VERSION
RUN ./configure --prefix=/usr/local/nginx --with-http_ssl_module
# 编译并安装Nginx
RUN make && \
make install
# 定义工作目录
WORKDIR /usr/local/nginx
# 设置环境变量
ENV PATH $PATH:/usr/local/nginx/sbin
# # 清除下载的源码包
RUN rm -rf /tmp/nginx-$NGINX_VERSION /tmp/nginx.tar.gz
# 删除deb包
RUN rm -rf rm -rf /var/lib/apt/lists/*
# 暴露80和443端口
EXPOSE 80 443
# 定义启动命令
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
将上面的内容写入到文件Dockerfile
中,然后执行构建命令
docker build --build-arg NGINX_VERSION=1.27.0 -t debian/nginx:1.27.0 .
效果如下
[root@bxy2 nginx]# docker build --build-arg NGINX_VERSION=1.27.0 -t debian/nginx:1.27.0 .
Sending build context to Docker daemon 6.144kB
Step 1/18 : FROM debian:11
---> afbb9e728ec0
Step 2/18 : ARG NGINX_VERSION
---> Using cache
---> 038324d07171
Step 3/18 : RUN sed -i "s@http.*\(com\|org\|cn\)@http://mirrors.cloud.tencent.com@g" /etc/apt/sources.list
---> Using cache
---> cdb88ce4323c
Step 4/18 : RUN apt-get update
---> Using cache
---> b0939ab87773
Step 5/18 : RUN apt-get install -y -q build-essential libpcre3-dev zlib1g-dev libssl-dev
---> Using cache
---> d84c6eab398a
Step 6/18 : RUN apt install -y -q make clang openssl ca-certificates
---> Using cache
---> 8b4a652c2c30
Step 7/18 : RUN apt-get install -y -q ntp
---> Using cache
---> 7283fc6588bd
Step 8/18 : RUN apt-get install -y -q wget && wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -O nginx.tar.gz
---> Using cache
---> 7bf6cbfd85cd
Step 9/18 : RUN tar -zxvf nginx.tar.gz -C /tmp/
---> Using cache
---> 0029dcf6fc75
Step 10/18 : WORKDIR /tmp/nginx-$NGINX_VERSION
---> Using cache
---> e0b9c7b5590e
Step 11/18 : RUN ./configure --prefix=/usr/local/nginx --with-http_ssl_module
---> Using cache
---> 2735a925026f
Step 12/18 : RUN make && make install
---> Using cache
---> 08492a6b0610
Step 13/18 : RUN cd .. && rm -rf nginx-$NGINX_VERSION nginx.tar.gz
---> Using cache
---> 3b45247c1e2f
Step 14/18 : WORKDIR /usr/local/nginx
---> Using cache
---> 6b2a2fa6130e
Step 15/18 : ENV PATH $PATH:/usr/local/nginx/sbin
---> Using cache
---> 91068c181e2c
Step 16/18 : RUN rm -rf /tmp/nginx*
---> Using cache
---> 286060579ddd
Step 17/18 : EXPOSE 80 443
---> Using cache
---> 06ade673e274
Step 18/18 : CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
---> Using cache
---> bdc347c713ec
Successfully built bdc347c713ec
Successfully tagged debian/nginx:1.27.0
[root@bxy2 nginx]#
容器运行
[root@bxy2 nginx]# docker run -tid --name nginx debian/nginx:1.27.0
d810ca8f3c4742873f86d6c593f871b0ab4358396c2ffa91a0c37919c25e1c26
[root@bxy2 nginx]# docker exec -ti nginx bash
root@d810ca8f3c47:/usr/local/nginx# nginx -v
nginx version: nginx/1.27.0
root@d810ca8f3c47:/usr/local/nginx#