1. 首页
  2. docker教程

22-二十二、Docker 部署 Nginx 环境

Nginx 是当下最流行的 Web 服务器软件之一,以高性能著称。Docker 部署 Nginx 环境有两种方式:通过 Dockerfile 构建 和 从 Docker Hub 仓库拉取

一 使用 docker pull nginx 拉取 Nginx 镜像

这是最简单的方式,开箱即用

1、 使用 docker search nginx 命令列出 docker.io 上所有的 Nginx 有关的镜像


[root@localhost ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 8564 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1337 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 547 [OK] jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 371 [OK] kong Open-source Microservice & API Management la… 188 [OK] webdevops/php-nginx Nginx with PHP-FPM 103 [OK] kitematic/hello-world-nginx A light-weight nginx container that demonstr… 99 bitnami/nginx Bitnami nginx Docker Image 52 [OK] zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 51 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 35 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 35 tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 19 [OK] nginxdemos/nginx-ingress NGINX Ingress Controller for Kubernetes . Th… 11 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 9 [OK] wodby/drupal-nginx Nginx for Drupal container image 9 [OK] webdevops/nginx Nginx container 8 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 6 nginxdemos/hello NGINX webserver that serves a simple page co… 6 [OK] 1science/nginx Nginx Docker images that include Consul Temp… 4 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 3 pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK] travix/nginx NGinx reverse proxy 1 [OK] toccoag/openshift-nginx Nginx reverse proxy for Nice running on same… 1 [OK] mailu/nginx Mailu nginx frontend 0 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK]

有很多版本,我们选择官方的 nginx
2、 拉取最新的 nginx 标签


[root@localhost ~]# docker pull nginx:latest latest: Pulling from library/nginx Digest: sha256:0fb320e2a1b1620b4905facb3447e3d84ad36da0b2c8aa8fe3a5a81d1187b884 Status: Image is up to date for nginx:latest

3、 下载完成后,可以在本地镜像列表里找到 REPOSITORY 为 nginx, 标签为 latest 的镜像


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

二、 通过 Dockerfile 文件构建

这种方式比较复杂,但可以一定程度的定制和熟悉如何编译安装 Nginx

1、 创建文件和目录

创建目录 nginx 用于存放后面的相关东西


[root@localhost ~]# mkdir -p nginx/www nginx/logs nginx/conf
目录 说明
nginx/www 该目录将映射为nginx容器配置的虚拟目录
nginx/logs 该目录将映射为nginx容器的日志目录
nginx conf

2、 创建 nginx/Dockerfile 文件


[root@localhost ~]# touch nginx/Dockerfile [root@localhost ~]# cd nginx [root@localhost nginx]# vi Dockerfile

然后将下面的内容拷贝到 Dockerfile 文件中


FROM debian:stretch-slim LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>" ENV NGINX_VERSION 1.13.12-1~stretch ENV NJS_VERSION 1.13.12.0.2.0-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 # let's build binaries from the published source packages 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 && tempDir="$(mktemp -d)" \ && chmod 777 "$tempDir" \ # (777 to ensure APT's "_apt" user can access it too) \ # save list of currently-installed packages so build dependencies can be cleanly removed later && 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 \ ) \ # we don't remove APT lists here because they get re-downloaded and removed later \ # reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies # (which is done after we install the built packages so we don't have to redownload any overlapping dependencies) && apt-mark showmanual | xargs apt-mark auto > /dev/null \ && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } \ \ # create a temporary local APT repo to install from (so that dependency resolution can be handled by APT, as it should be) && 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 we have leftovers from building, let's purge them (including extra, unnecessary build deps) && if [ -n "$tempDir" ]; then \ apt-get purge -y --auto-remove \ && rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; \ fi # forward request and error logs to docker log collector RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log EXPOSE 80 STOPSIGNAL SIGTERM CMD ["nginx", "-g", "daemon off;"]

3、 使用 docker build 命令通过 Dockerfile 创建镜像 ycbbs/nginx:jessie-1.13.12


[root@localhost ~]# docker build -t ycbbs/nginx:1.13.12-1-stretch . Sending build context to Docker daemon 8.704kB Step 1/9 : FROM debian:stretch-slim stretch-slim: Pulling from library/debian f2aa67a397c4: Already exists Digest: sha256:b7da507acebb30938b84d38e5adec9755fa3a41d78527ecdc2440c31c4730b11 Status: Downloaded newer image for debian:stretch-slim ...

4、 创建完成后,可以在本地的镜像列表里查找到刚刚创建的镜像


[root@localhost ~]# docker images ycbbs/nginx:1.13.12-1-stretch REPOSITORY TAG IMAGE ID CREATED SIZE ycbbs/nginx 1.13.12-1-stretch a5ea477e09b9 About a minute ago 109MB

运行 ycbbs/nginx:1.13.12-1-stretch 镜像创建容器

1、 在当前目录的 www 目录下新建文件 index.html 输入以下内容


<!DOCTYPE html> <meta charset="utf-8" /> <title>研发军团,教程 </title> <h1>Hello 研发军团</h1> <p>研发军团的网址是:www.ycbbs.vip </p>

2、 在当前目录的 conf 目录下新建文件 nginx.conf 输入以下内容


worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location / { root /www; index index.html index.htm index.php; } } }

3、 使用下面的命令运行我们刚刚创建的 ycbbs/nginx:1.13.12-1-stretch 镜像


[root@localhost nginx]# docker run -p 80:80 --name my-nginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d ycbbs/nginx:1.13.12-1-stretch 8c232bd675d742a313ea7e222e3ef1ca864c76815083451da7f89118094c6815
参数 说明
-p80:80 将容器的80端口映射到主机的80端口
–namemynginx 将容器命名为my-nginx
-v$PWD/www:/www 将当前目录下的www挂载到容器的/www
-v$PWD/conf/nginx.conf:/etc/nginx/nginx.conf 将当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf
-v$PWD/logs:/wwwlogs 将主机中当前目录下的logs挂载到容器的/wwwlogs

查看容器启动情况

可以使用 docker ps 命令可以查看容器启动的情况


[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c232bd675d7 ycbbs/nginx:1.13.12-1-stretch "nginx -g 'daemon of…" 6 seconds ago Up 20 seconds 0.0.0.0:80->80/tcp my-nginx d07b599f11e4 jcdemo/flaskapp "python /src/app.py" 11 hours ago Up 11 hours 0.0.0.0:32768->5000/tcp hardcore_lalande

通过浏览器访问 http://localhost 输出如下

img_1.png

看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「方志朋」,公众号后台回复「666」 免费领取我精心整理的进阶资源教程
  4. JS中文网,Javascriptc中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,是给开发者用的 Hacker News,技术文章由为你筛选出最优质的干货,其中包括:Android、iOS、前端、后端等方面的内容。目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。

    本文著作权归作者所有,如若转载,请注明出处

    转载请注明:文章转载自「 Java极客技术学习 」https://www.javajike.com

    标题:22-二十二、Docker 部署 Nginx 环境

    链接:https://www.javajike.com/article/1194.html

« 23-二十三、Docker 部署 PHP 环境
21-二十一、Docker 镜像打标签»

相关推荐

QR code