Docker系列(二十)——Docker实例二Docker安装Nginx实例

                                   《 Docker实例二Docker安装Nginx实例 》

Docker系列(二十)——Docker实例二Docker安装Nginx实例_第1张图片

前言

在前面一篇文章种,完成了 《 Docker安装Apache实例 》,本篇将继续镜像安装教程,并完成Docker安装Nginx实例。

 

Docker实例二Docker安装Nginx实例

第一种:使用 docker pull 镜像名 拉取镜像

1、查询星级数大于30的Nginx镜像(注:星级数越高,越受欢迎的,默认在 Docker Hub 查询的),

输入命令 “ docker search -s 30 nginx” ,查询内容如下:

[root@localhost ~]# docker search -s 30 nginx
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        10449               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1480                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   657                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   448                                     [OK]
kong                                                   Open-source Microservice & API Management la…   254                 [OK]                
webdevops/php-nginx                                    Nginx with PHP-FPM                              118                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   112                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   79                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      59                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          47                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   44                              

 

2、拉取第一个镜像,也是官方版的镜像,输入命令 “ docker pull nginx”,默认拉取的为最新版本 latest ,内容如下:

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f17d81b4b692: Already exists 
82dca86e04c3: Pull complete 
046ccb106982: Pull complete 
Digest: sha256:d59a1aa7866258751a261bae525a1842c7ff0662d4f34a355d5f36826abc0341
Status: Downloaded newer image for nginx:latest

 

3、拉取完成后,输入命令 “ docker images nginx ”,可以在本地镜像列表里查到REPOSITORY为nginx的镜像,内容如下:

[root@localhost ~]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              62f816a209e6        3 weeks ago         109MB

 

第二种:通过 Dockerfile 构建Docker镜像

1、新建Nginx的Dockerfile文件存放目录,输入命令 “ mkdir -p /opt/dockerfile/nginx ” ,并进入到目录下。

[root@localhost ~]# mkdir -p /opt/dockerfile/nginx
[root@localhost ~]# cd /opt/dockerfile/nginx/
[root@localhost nginx]# 

2、创建dockerfile文件,输入命令 “ vi nginxdockerfile ” ,并键入如下内容:

Ubuntu 版本:

# Base 基础镜像
FROM debian:stretch-slim

LABEL maintainer="NGINX Docker Maintainers "

ENV NGINX_VERSION 1.15.7-1~stretch
ENV NJS_VERSION   1.15.7.0.2.6-1~stretch

RUN set -x \
	&& apt-get update \
	&& apt-get install --no-install-recommends --no-install-suggests -y gnupg1 apt-transport-https ca-certificates \
	&& \
	NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
	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 $NGINX_GPGKEY from $server"; \
		apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
	done; \
	test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
	apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \
	&& dpkgArch="$(dpkg --print-architecture)" \
	&& nginxPackages=" \
		nginx=${NGINX_VERSION} \
		nginx-module-xslt=${NGINX_VERSION} \
		nginx-module-geoip=${NGINX_VERSION} \
		nginx-module-image-filter=${NGINX_VERSION} \
		nginx-module-njs=${NJS_VERSION} \
	" \
	&& case "$dpkgArch" in \
		amd64|i386) \
# arches officialy built by upstream
			echo "deb https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list \
			&& apt-get update \
			;; \
		*) \
# we're on an architecture upstream doesn't officially build for
# 从已发布的源包构建二进制文件
			echo "deb-src https://nginx.org/packages/mainline/debian/ stretch nginx" >> /etc/apt/sources.list.d/nginx.list \
			\
# new directory for storing sources and .deb files
# 用于存储源文件和.deb文件的新目录
			&& tempDir="$(mktemp -d)" \
			&& chmod 777 "$tempDir" \
# (777 to ensure APT's "_apt" user can access it too) 授权
			\
# 保存当前安装的包的列表,以最后可以干净地删除构建依赖项
			&& savedAptMark="$(apt-mark showmanual)" \
			\
# build .deb files from upstream's source packages (which are verified by apt-get)
			&& apt-get update \
			&& apt-get build-dep -y $nginxPackages \
			&& ( \
				cd "$tempDir" \
				&& DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" \
					apt-get source --compile $nginxPackages \
			) \
# 在这里不删除 APT 列表,因为它们会被重新下载并在稍后删除
			\
# 重置 apt-mark 的 “ manual ” 列表,以便 “ purge --auto-remove ” 将删除所有构建依赖项
# (这是在安装构建包之后完成的,这样就不必重新下载任何重叠的依赖项)
			&& apt-mark showmanual | xargs apt-mark auto > /dev/null \
			&& { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } \
			\
# 创建要从其中安装的临时本地 APT repo( 以便 APT 可以处理依赖项解析)
			&& ls -lAFh "$tempDir" \
			&& ( cd "$tempDir" && dpkg-scanpackages . > Packages ) \
			&& grep '^Package: ' "$tempDir/Packages" \
			&& echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list \
# work around the following APT issue by using "Acquire::GzipIndexes=false" (overriding "/etc/apt/apt.conf.d/docker-gzip-indexes")
#   Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied)
#   ...
#   E: Failed to fetch store:/var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages  Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied)
			&& apt-get -o Acquire::GzipIndexes=false update \
			;; \
	esac \
	\
	&& apt-get install --no-install-recommends --no-install-suggests -y \
						$nginxPackages \
						gettext-base \
	&& apt-get remove --purge --auto-remove -y apt-transport-https ca-certificates && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list \
	\
# 如果在构建中有残余项,就清除它们(包括额外的、不必要的构建副版本)
	&& if [ -n "$tempDir" ]; then \
		apt-get purge -y --auto-remove \
		&& rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; \
	fi

# 将请求和错误日志转发到docker日志收集器
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
	&& ln -sf /dev/stderr /var/log/nginx/error.log

# 暴露80端口
EXPOSE 80

STOPSIGNAL SIGTERM

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

 

Linux 版本:

FROM alpine:3.9

LABEL maintainer="NGINX Docker Maintainers "

ENV NGINX_VERSION 1.15.10

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 \
		gnupg1 \
		libxslt-dev \
		gd-dev \
		geoip-dev \
	&& curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \
	&& curl -fSL https://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 -rf "$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 --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
			| tr ',' '\n' \
			| sort -u \
			| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
	)" \
	&& apk add --no-cache --virtual .nginx-rundeps $runDeps \
	&& apk del .build-deps \
	&& apk del .gettext \
	&& mv /tmp/envsubst /usr/local/bin/ \
	\
	# Bring in tzdata so users could set the timezones through the environment
	# variables
	&& apk add --no-cache tzdata \
	\
	# 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;"]

3、通过 Dockerfile 的 docker build 构建一个 nginx 镜像,这儿可以自定义镜像名,输入命令 “ docker build -f /opt/dockerfile/nginx/nginxdockerfile -t huazai/web/nginx:v1.0 . ” 进行构建,部分构建内容如下:

[root@localhost nginx]# docker build -f nginxdockerfile -t huazai/web/nginx:v1.0 .
Sending build context to Docker daemon  6.144kB
Step 1/9 : FROM debian:stretch-slim
stretch-slim: Pulling from library/debian
f17d81b4b692: Already exists 
Digest: sha256:ef6be890318a105f7401d0504c01c2888daa5d9e45b308cf0e45c7cb8e44634f
Status: Downloaded newer image for debian:stretch-slim

# 此处省去 N 行

Step 8/9 : STOPSIGNAL SIGTERM
 ---> Running in 826aa504ed43
Removing intermediate container 826aa504ed43
 ---> 721acf30abb2
Step 9/9 : CMD ["nginx", "-g", "daemon off;"]
 ---> Running in 6a968fa50cf9
Removing intermediate container 6a968fa50cf9
 ---> b217394a78f2
Successfully built b217394a78f2
Successfully tagged huazai/web/nginx:v1.0

 

构建成功后,输入命令 “ docker images huazai/web/nginx  ” ,可以在本地的镜像列表种查找到方才构建 nginx 镜像,内容如下:

[root@localhost nginx]# docker images huazai/web/nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
huazai/web/nginx    v1.0                b217394a78f2        8 minutes ago       109MB

 

 


测试 nginx 镜像

1、创建 nginx 相关目录,后面用于数据转递与共享的容器数据卷,

输入命令 “ mkdir -p /opt/nginx/config /opt/nginx/www /opt/nginx/logs ” ,内容如下:

[root@localhost ~]# mkdir -p /opt/nginx/config /opt/nginx/www /opt/nginx/logs
[root@localhost ~]# cd /opt/nginx/
[root@localhost nginx]# ll
total 0
drwxr-xr-x. 2 root root 6 Dec  4 02:35 config
drwxr-xr-x. 2 root root 6 Dec  4 02:35 logs
drwxr-xr-x. 2 root root 6 Dec  4 02:35 www

目录说明:

config 用于存放 nginx 的配置文件,启动时将文件将映射为 nginx 容器的配置文件;
www 对应 nginx 容器虚拟目录,启动时将映射为 nginx 容器配置的虚拟目录;
logs 用于存放nginx所产生的日志,启动时将映射为 nginx 容器的日志目录;

 

2、启动镜像,创建一个容器实例,输入命令 “ docker run -it -d -p 80:80 --name nginx01 -v /opt/nginx/www:/opt/nginx/www -v /opt/nginx/config:/etc/nginx/nginx.conf -v /opt/nginx/logs:/opt/nginx/logs --privileged=true 镜像ID ” ,启动后会产生一个 ID ,内容如下:

 

3、查看 nginx 服务启动情况,输入命令 “ docker ps ” ,内容如下:

 

4、访问 nginx(这儿需要一些配置,自定义哈),正常启动后,访问如下图:

 

 

 

 


 好了,关于 Docker系列(二十)——Docker实例二Docker安装Nginx实例 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者: 华    仔
联系作者: [email protected]
来        源: CSDN (Chinese Software Developer Network)
原        文: https://blog.csdn.net/Hello_World_QWP/article/details/84763869
版权声明: 本文为博主原创文章,请在转载时务必注明博文出处!

你可能感兴趣的:(Docker,Docker,进阶之路)