如何获取Docker用Nginx稳定版?自动拉取最新版编译vts模块的Dockerfile方案
Docker Nginx Stable Version & Auto-Build Dockerfile with VTS Module
Hey there! Let's break down your questions and fix that Dockerfile issue:
1. Where to find a stable Nginx build for Docker?
- Official Docker Image: The easiest way is to use the official Nginx Docker image tagged as
nginx:stable— this image automatically tracks the latest stable Nginx release, so you don't have to compile anything yourself if you don't need custom modules. - Source Code for Compilation: If you need to build with custom modules (like vts here), Nginx's official download page hosts the latest stable source. The trick is to automate fetching the latest version instead of hardcoding it, which we'll solve next.
2. Modify your Dockerfile to auto-pull the latest stable Nginx
The core problem is hardcoding the version number (1.14.2). We can dynamically fetch the latest stable version from Nginx's website using simple shell commands, then use that variable throughout the build process.
Here's the updated Dockerfile with key improvements:
FROM debian:stretch-slim RUN apt-get update && \ # Install dependencies + curl to fetch version info apt-get install -y curl git wget libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential zlib1g-dev && \ # Fetch latest stable Nginx version number from official site NGINX_VERSION=$(curl -s http://nginx.org/en/download.html | grep -o 'nginx-[0-9]\+\.[0-9]\+\.[0-9]\+' | head -n1 | sed 's/nginx-//') && \ cd /tmp/ && \ # Download source using the dynamic version variable wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \ git clone git://github.com/vozlt/nginx-module-vts.git && \ tar zxvf nginx-${NGINX_VERSION}.tar.gz && \ rm -f nginx-${NGINX_VERSION}.tar.gz && \ cd nginx-${NGINX_VERSION} && \ ./configure --prefix=/tmp/nginx-${NGINX_VERSION} --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --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.pid \ --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module \ --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module \ --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module \ --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream \ --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module \ --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-'${NGINX_VERSION}'/debian/debuild-base/nginx-'${NGINX_VERSION}'=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' \ --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \ --add-module=/tmp/nginx-module-vts && \ make && make install && \ cp -f objs/nginx /usr/sbin/nginx && \ # Clean up to reduce image size: remove curl and temp files apt-get purge -y curl && \ apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* CMD ["nginx", "-g", "daemon off;"]
Key Changes Explained:
- Added
curl: We use this to scrape the latest stable version number from Nginx's download page. - Dynamic Version Variable:
NGINX_VERSIONcaptures the latest version, replacing all hardcoded1.14.2instances. - Cleanup Optimizations: We uninstall
curlafter using it and wipe temporary files to keep the Docker image as small as possible.
More Reliable Version Fetching (Alternative)
If Nginx's download page structure ever changes, this method (pulling directly from the download directory) might be more stable:
# Fetch the latest stable tarball URL instead of just the version NGINX_TAR_URL=$(curl -s http://nginx.org/download/ | grep -o 'href="nginx-[0-9]\+\.[0-9]\+\.[0-9]\+.tar.gz"' | head -n1 | sed 's/href="//;s/"//') NGINX_VERSION=$(echo $NGINX_TAR_URL | sed 's/nginx-\(.*\).tar.gz/\1/') # Then download with: wget http://nginx.org/download/${NGINX_TAR_URL}
This grabs the actual tarball link from the download list, which is less likely to break if the homepage HTML changes.
内容的提问来源于stack exchange,提问作者Tyr_90
相关产品推荐
相关产品推荐

