Dockerfile巩固:阅读解析nginx的Dockerfile

Nginx的Dockerfile

下面是nginx的Dockerfile文件,我们来阅读巩固下dockerfile的知识

FROM alpine:3.5

LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"

ENV NGINX_VERSION 1.12.1

RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \
	&& CONFIG="\
		--prefix=/etc/nginx \
		--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-http_ssl_module \
		--with-http_realip_module \
		--with-http_addition_module \
		--with-http_sub_module \
		--with-http_dav_module \
		--with-http_flv_module \
		--with-http_mp4_module \
		--with-http_gunzip_module \
		--with-http_gzip_static_module \
		--with-http_random_index_module \
		--with-http_secure_link_module \
		--with-http_stub_status_module \
		--with-http_auth_request_module \
		--with-http_xslt_module=dynamic \
		--with-http_image_filter_module=dynamic \
		--with-http_geoip_module=dynamic \
		--with-threads \
		--with-stream \
		--with-stream_ssl_module \
		--with-stream_ssl_preread_module \
		--with-stream_realip_module \
		--with-stream_geoip_module=dynamic \
		--with-http_slice_module \
		--with-mail \
		--with-mail_ssl_module \
		--with-compat \
		--with-file-aio \
		--with-http_v2_module \
	" \
	&& addgroup -S nginx \
	&& adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
	&& apk add --no-cache --virtual .build-deps \
		gcc \
		libc-dev \
		make \
		openssl-dev \
		pcre-dev \
		zlib-dev \
		linux-headers \
		curl \
		gnupg \
		libxslt-dev \
		gd-dev \
		geoip-dev \
	&& curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \
	&& curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz.asc  -o nginx.tar.gz.asc \
	&& export GNUPGHOME="$(mktemp -d)" \
	&& found=''; \
	for server in \
		ha.pool.sks-keyservers.net \
		hkp://keyserver.ubuntu.com:80 \
		hkp://p80.pool.sks-keyservers.net:80 \
		pgp.mit.edu \
	; do \
		echo "Fetching GPG key $GPG_KEYS from $server"; \
		gpg --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$GPG_KEYS" && found=yes && break; \
	done; \
	test -z "$found" && echo >&2 "error: failed to fetch GPG key $GPG_KEYS" && exit 1; \
	gpg --batch --verify nginx.tar.gz.asc nginx.tar.gz \
	&& rm -r "$GNUPGHOME" nginx.tar.gz.asc \
	&& mkdir -p /usr/src \
	&& tar -zxC /usr/src -f nginx.tar.gz \
	&& rm nginx.tar.gz \
	&& cd /usr/src/nginx-$NGINX_VERSION \
	&& ./configure $CONFIG --with-debug \
	&& make -j$(getconf _NPROCESSORS_ONLN) \
	&& mv objs/nginx objs/nginx-debug \
	&& mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \
	&& mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \
	&& mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \
	&& mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \
	&& ./configure $CONFIG \
	&& make -j$(getconf _NPROCESSORS_ONLN) \
	&& make install \
	&& rm -rf /etc/nginx/html/ \
	&& mkdir /etc/nginx/conf.d/ \
	&& mkdir -p /usr/share/nginx/html/ \
	&& install -m644 html/index.html /usr/share/nginx/html/ \
	&& install -m644 html/50x.html /usr/share/nginx/html/ \
	&& install -m755 objs/nginx-debug /usr/sbin/nginx-debug \
	&& install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \
	&& install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \
	&& install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \
	&& install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \
	&& ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \
	&& strip /usr/sbin/nginx* \
	&& strip /usr/lib/nginx/modules/*.so \
	&& rm -rf /usr/src/nginx-$NGINX_VERSION \
	\
	# Bring in gettext so we can get `envsubst`, then throw
	# the rest away. To do this, we need to install `gettext`
	# then move `envsubst` out of the way so `gettext` can
	# be deleted completely, then move `envsubst` back.
	&& apk add --no-cache --virtual .gettext gettext \
	&& mv /usr/bin/envsubst /tmp/ \
	\
	&& runDeps="$( \
		scanelf --needed --nobanner /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
			| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
			| sort -u \
			| xargs -r apk info --installed \
			| sort -u \
	)" \
	&& apk add --no-cache --virtual .nginx-rundeps $runDeps \
	&& apk del .build-deps \
	&& apk del .gettext \
	&& mv /tmp/envsubst /usr/local/bin/ \
	\
	# forward request and error logs to docker log collector
	&& ln -sf /dev/stdout /var/log/nginx/access.log \
	&& ln -sf /dev/stderr /var/log/nginx/error.log

COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

STOPSIGNAL SIGTERM

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

解析

1、FROM alpine:3.5

第一条命令一般都是FROM,使用FROM指令指定基础镜像
为了保证镜像精简,可以选用体积较小的镜像如Alpine或Debian作为基础镜像。

2、LABEL maintainer=“NGINX Docker Maintainers docker-maint@nginx.com

LABEL指令可以为生成的镜像添加元数据标签信息,maintainer为作者,用于替代之前版本的MAINTAINER ,这里主要标记了作者信息

3、ENV NGINX_VERSION 1.12.1

设置环境变量,在镜像生成过程中会被后续RUN指令使用,在镜像启动的容器中也会存在,使用方式为$加环境变量名,在例子中就是 $NGINX_VERSION

4、RUN 命令

运行指定命令,格式为RUN <command>,因为每条RUN指令都会在当前镜像基础上执行指定命令,并提交为新的镜像层,所以当命令较多的时候是使用\ && 来换行拼接命令,保证镜像的整洁。

例子中使用RUN命令安装必要的依赖和工具,下载、验证和解压NGINX源代码,配置NGINX,并进行编译安装。

5、COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf

将当前路径下的NGINX的配置文件和虚拟主机配置文件复制到容器中。

COPY
复制文件到镜像内,格式为COPY <src> <dest>。

复制本地主机的<src>(为Dockerfile所在目录的相对路径,文件或目录)下内容到镜像中的<dest>。目标路径不存在时,会自动创建。

在Dockerfile中,还有一个ADD命令也是用于文件复制,COPY与ADD指令功能类似,但COPY不支持URL和压缩包,当使用本地目录为源目录时,推荐使用COPY。

6、EXPOSE 80

暴露端口,该指令只是起到声明作用,并不会自动完成端口映射。

EXPOSE指令用于声明容器运行时将监听的网络端口。它并不会创建实际的端口映射,而是作为一种元数据提供给开发人员和运维人员,在构建镜像时进行文档化。这样可以帮助其他人了解容器应该使用哪些端口。

如果要映射端口出来,在启动容器时可以使用-P参数(Docker主机会自动分配一个宿主机的临时端口)或-p HOST_PORT:CONTAINER_PORT参数(具体指定所映射的本地端口)。

7、STOPSIGNAL SIGTERM

设置停止信号为SIGTERM,当你使用docker stop命令或通过Docker API停止容器时,Docker会发送一个停止信号给容器进程,以便优雅地停止正在运行的应用程序。默认情况下,Docker发送的停止信号是SIGTERM(15号信号)。

8、CMD [“nginx”, “-g”, “daemon off;”]

CMD指令用来指定启动容器时默认执行的命令。每个Dockerfile只能有一条CMD命令。如果指定了多条命令,只有最后一条会被执行。如果用户启动容器时候手动指定了运行的命令(作为run命令的参数),则会覆盖掉CMD指定的命令。

CMD [“nginx”, “-g”, “daemon off;”]指令用于定义容器启动时要执行的默认命令,这里是以非守护进程模式运行NGINX,并将日志输出到标准输出。

CMD的三种格式

  1. CMD [“executable”, “param1”, “param2”]:相当于执行executable param1 param2,推荐方式;
  2. CMD command param1 param2:在默认的Shell中执行,提供给需要交互的应用;
  3. CMD [“param1”, “param2”]:提供给ENTRYPOINT的默认参数。

相关推荐

  1. Dockerfile巩固阅读解析nginxDockerfile

    2023-12-22 14:36:03       36 阅读
  2. Dockerfilenginx.conf文件解读

    2023-12-22 14:36:03       16 阅读
  3. DockerFile

    2023-12-22 14:36:03       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 14:36:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 14:36:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 14:36:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 14:36:03       20 阅读

热门阅读

  1. 数据库连接问题 - ChatGPT对自身的定位

    2023-12-22 14:36:03       35 阅读
  2. 第二十一章网络通讯

    2023-12-22 14:36:03       30 阅读
  3. Curl多线程https访问,崩溃问题修复

    2023-12-22 14:36:03       47 阅读
  4. 基于博弈树的开源五子棋AI教程[5 启发式搜索]

    2023-12-22 14:36:03       40 阅读