在本教程中,探索如何使用NGINX Docker镜像在Docker等平台上构建和运行web应用程序!
Docker是一个引人注目的平台,特别适合打包和运行Web应用程序,尤其是与云平台提供的众多平台即服务(PaaS)方案配对使用。NGINX一直为DevOps团队提供Linux上托管Web应用程序的能力,并且还提供了官方的Docker镜像作为自定义Web应用程序的基础。
在本文中,我将解释DevOps团队如何使用NGINX Docker镜像来构建和运行Web应用程序。
开始使用基础镜像
NGINX是一个多功能工具,可用作负载均衡器、反向代理和网络缓存等众多用途。然而,在Docker容器中运行NGINX时,大部分这些高级功能都被委派给其他专门的平台或其他实例的NGINX。通常情况下,当NGINX在Docker容器中运行时,它扮演的是Web服务器的角色。
要创建一个带有默认网站的NGINX容器,请运行以下命令:
docker run -p 8080:80 nginx
此命令将下载 nginx 镜像(如果尚未下载)并创建一个容器,将容器的80端口映射到主机上的8080端口。然后,您可以打开 http://localhost:8080/index.html 来查看默认的“Welcome to nginx!”网站。
为了使NGINX容器能够托管自定义网页资源,您可以将本地目录挂载到Docker容器内部。
将以下HTML代码保存到名为 index.html 的文件中:
<html> <body> Hello from Octopus! </body> </html>
接下来,运行以下命令将当前目录以只读方式挂载到NGINX容器内的 /usr/share/nginx/html 目录下:
docker run -v $(pwd):/usr/share/nginx/html:ro -p 8080:80 nginx
再次打开 http://localhost:8080/index.html,您将看到显示自定义HTML页面。
Docker镜像的一个好处是能够将所有相关文件捆绑到一个可分发的单个构件中。为了实现这一好处,您必须基于NGINX镜像创建一个新的Docker镜像。
基于NGINX创建自定义镜像
要创建您自己的 Docker 映像,请将以下文本保存到名为 的文件中 Dockerfile:
FROM nginx COPY index.html /usr/share/nginx/html/index.html
Dockerfile 包含构建自定义 Docker 映像的说明。这里你使用 FROM 命令将你的镜像基于NGINX镜像,然后使用命令COPY将你的 index.html 文件复制到该 /usr/share/nginx/html 目录下的新镜像中。
使用以下命令构建新映像:
docker build . -t mynginx
这将构建一个名为 的新图像 mynginx 。使用以下命令运行新映像:
docker run -p 8080:80 mynginx
请注意,这次您没有挂载任何目录。但是,当您打开 http://localhost:8080/index.html 自定义 HTML 页面时,会显示该页面,因为它嵌入在您的自定义图像中。
NGINX 的功能远不止托管静态文件。要解锁此功能,您必须使用自定义 NGINX 配置文件。
高级 NGINX 配置
NGINX 通过配置文件公开其功能。默认 NGINX 映像附带一个简单的默认配置文件,旨在托管静态 Web 内容。该文件位于 /etc/nginx/nginx.conf 默认映像中,具有以下内容:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
无需详细了解该配置文件,但有一行有趣的内容指示 NGINX 从该 /etc/nginx/conf.d 目录加载其他配置文件:
include /etc/nginx/conf.d/*.conf;
默认/etc/nginx/conf.d文件将 NGINX 配置为 Web 服务器。具体来说,location / 块加载文件 /usr/share/nginx/html 就是您之前将 HTML 文件安装到该目录的原因:
server { listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
您可以利用说明加载任何 *.conf 配置文件来自 /etc/nginx 定义 NGINX。/nginx-health 在此示例中,您通过侦听端口 90 的自定义位置添加运行状况检查,该位置使用 HTTP 200 OK响应对路径的请求。
将以下文本保存到名为 的文件中 health-check.conf:
server { listen 90; server_name localhost; location /nginx-health { return 200 "healthy\n"; add_header Content-Type text/plain; } }
修改 Dockerfile 将配置文件复制到 /etc/nginx/conf.d:
FROM nginx COPY index.html /usr/share/nginx/html/index.html COPY health-check.conf /etc/nginx/conf.d/health-check.conf
使用以下命令构建图像:
docker build . -t mynginx
使用命令运行新映像。请注意 9090 上公开的新端口:
docker run -p 8080:80 -p 9090:90 mynginx
现在开放 http://localhost:9090/nginx-health。返回健康检查响应以表明 Web 服务器已启动并正在运行。
上面的示例使您的自定义图像基于默认 nginx 图像。然而,还有其他变体可以提供更小的图像尺寸而不牺牲任何功能。
选择 NGINX 变体
默认 nginx 镜像基于Debian。不过NGINX也提供了基于Alpine的镜像。
NGINX:github.com/nginxinc/docker-nginx/blob/master/Dockerfile-debian.template
Alpine:github.com/nginxinc/docker-nginx/blob/master/Dockerfile-alpine.template
Alpine 经常用作 Docker 镜像的轻量级基础。要查看 Docker 镜像的大小,必须首先将它们拉到本地工作站:
docker pull nginx docker pull nginx:stable-alpine
然后您可以使用以下命令查找图像尺寸:
docker image ls
由此,您可以看到 Debian 镜像的大小约为 140 MB,而 Alpine 镜像的大小约为 24 MB。这大大节省了图像尺寸。
要将您的图像基于 Alpine 变体,您需要更新 Dockerfile:
FROM nginx:stable-alpine COPY index.html /usr/share/nginx/html/index.html COPY health-check.conf /etc/nginx/conf.d/health-check.conf
使用以下命令构建并运行图像:
docker build . -t mynginx docker run -p 8080:80 -p 9090:90 mynginx
再次打开 http://localhost:9090/nginx-health 或 http://localhost:8080/index.html 查看网页。一切都像以前一样继续工作,但您的自定义图像现在小得多。
结论
NGINX 是一个功能强大的 Web 服务器,官方 NGINX Docker 镜像允许 DevOps 团队在 Docker 中托管自定义 Web 应用程序。NGINX 还支持高级场景,因为它能够读取复制到自定义 Docker 映像中的配置文件。
在这篇文章中,您学习了如何创建托管静态 Web 应用程序的自定义 Docker 映像,添加高级 NGINX 配置文件以提供运行状况检查端点,并比较 Debian 和 Alpine NGINX 映像的大小。
相关资源
NGINX Docker 镜像源代码(github.com/nginxinc/docker-nginx)文章来源:https://www.toymoban.com/article/515.html
Dockerfile 参考(docs.docker.com/engine/reference/builder/)文章来源地址https://www.toymoban.com/article/515.html
到此这篇关于使用NGINX Docker镜像构建和运行Web应用程序的教程的文章就介绍到这了,更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!