本文已弃用,不再维护。
本文中的技术已经过时,可能不再反映 Docker 最佳实践。
- Docker 生态系统:常用组件简介
- 如何在 Ubuntu 14.04 上的 Docker 容器中运行 Nginx
当涉及到处理 Web 应用程序服务器的请求和提供静态内容时,久经考验的 Nginx 是当今非常流行的选择。当你与docker并将您的应用程序容器化,让 Nginx 为它们提供服务对于大多数情况来说同样有意义。毕竟,这些容器使您能够轻松移植应用程序、快速扩展并为主机(即 Droplet)的安全性添加另一层。
在这篇 DigitalOcean 文章中,我们将学习如何快速设置 docker,从基础镜像创建 docker 容器,并构建它以逐层运行 Nginx。然后,按照我们从头开始的步骤,我们将创建一个 Dockerfile 来自动化整个过程。最后,使用此 Nginx docker 映像,您将能够创建运行 Nginx 的独立沙箱,该沙箱可用于为您的“dockerized”应用程序提供服务。
- 运行 docker 守护进程和 CLI 用法
- Docker 命令
- 从 Ubuntu 创建基础 Docker 容器
- 为 Nginx 安装准备基础容器
- 安装 Nginx
- 配置 Nginx
- Dockerfile 基础知识
- Dockerfile 命令概述
- 创建 Dockerfile
- 定义基本原理
- Nginx 安装说明
- 自举
- 最终的 Dockerfile
- 使用Dockerfile自动构建Nginx容器
The 码头工程提供更高级的工具,可以协同工作,这些工具构建在一些 Linux 内核功能之上。目标是帮助开发人员和系统管理员移植应用程序(及其所有依赖项)并让它们跨系统和机器运行 -无头痛.
Docker 通过为称为“docker 容器”的应用程序创建安全的、基于 LXC(即 Linux 容器)的环境来实现这一目标。这些容器是使用 docker 镜像创建的,可以通过手动执行命令或通过自动执行命令来构建这些容器Dockerfile.
Note:要了解有关 docker 及其部件(例如 docker daemon、CLI、images 等)的更多信息,请查看我们的项目介绍文章:docker 解释: 入门.
Nginx 是一个非常高性能的 Web 服务器/(反向)代理)。它因其重量轻、相对易于使用且易于扩展(使用附加组件/插件)而广受欢迎。由于其架构,它能够处理a lot请求(几乎无限),这取决于您的应用程序或网站负载,使用旧的替代方案可能真的很难解决。可以考虑the选择用于提供静态文件(例如图像、脚本或样式表)的工具。
随着其最新版本(0.7.1.日期:12 月 5 日),docker 可以部署在各种 Linux 操作系统上,包括 Ubuntu / Debian 和 CentOS / RHEL。
请记住,您可以通过使用 DigitalOcean 在 Ubuntu 13.04 上构建的即用型 docker 镜像来快速入门。
我们将快速回顾一下 Ubuntu(最新)的安装过程。
更新你的液滴:
sudo aptitude update
sudo aptitude -y upgrade
确保 aufs 支持可用:
sudo aptitude install linux-image-extra-`uname -r`
将 docker 存储库密钥添加到 apt-key 以进行包验证:
sudo sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -"
将 docker 存储库添加到 aptitude 源:
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main\
> /etc/apt/sources.list.d/docker.list"
使用新添加的内容更新存储库:
sudo aptitude update
最后,下载并安装docker:
sudo aptitude install lxc-docker
Ubuntu 的默认防火墙(UFW:简单的防火墙)默认拒绝所有转发流量,这是docker所需要的。
启用 UFW 转发:
使用 Nano 文本编辑器编辑 UFW 配置。
sudo nano /etc/default/ufw
向下滚动并找到以以下内容开头的行DEFAULT_FORWARD_POLICY.
Replace:
DEFAULT_FORWARD_POLICY="DROP"
With:
DEFAULT_FORWARD_POLICY="ACCEPT"
Press CTRL+X并批准Y保存并关闭。
最后,重新加载 UFW:
sudo ufw reload
在开始使用 docker 之前,让我们快速浏览一下它的可用命令,以刷新我们的记忆入门文章。
安装后,docker 守护进程应在后台运行,准备接受 docker CLI 发送的命令。对于某些可能需要手动运行 docker 的情况,请使用以下命令:
运行 docker 守护进程:
sudo docker -d &
docker CLI 用法:
sudo docker [option] [command] [arguments]
Note:docker 需要 sudo 权限才能工作。
这是当前可用的摘要(版本0.7.1)泊坞窗命令:
attach: Attach to a running container
build: Build a container from a Dockerfile
commit: Create a new image from a container's changes
cp: Copy files/folders from the containers filesystem to the host path
diff: Inspect changes on a container's filesystem
events: Get real time events from the server
export: Stream the contents of a container as a tar archive
history: Show the history of an image
images: List images
import: Create a new filesystem image from the contents of a tarball
info: Display system-wide information
insert: Insert a file in an image
inspect: Return low-level information on a container
kill: Kill a running container
load: Load an image from a tar archive
login: Register or Login to the docker registry server
logs: Fetch the logs of a container
port: Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
ps: List containers
pull: Pull an image or a repository from the docker registry server
push: Push an image or a repository to the docker registry server
restart: Restart a running container
rm: Remove one or more containers
rmi: Remove one or more images
run: Run a command in a new container
save: Save an image to a tar archive
search: Search for an image in the docker index
start: Start a stopped container
stop: Stop a running container
tag: Tag an image into a repository
top: Lookup the running processes of a container
version: Show the docker version information
===
在我们的 VPS 上安装了 docker 并快速浏览了它的命令后,我们准备开始实际工作来创建运行 Nginx 的 docker 容器。
Note:尽管在完成本节之后,我们将拥有一个安装了 Nginx 的正在运行的 docker 容器,但由于其复杂性,这绝对不是推荐的方法。然而,它为您提供了一个学习如何使用实时容器并熟悉我们稍后需要定义的命令来自动化该过程的机会。要以更好的方式创建安装了 Nginx 的 docker 映像,请参阅下一节:创建 Dockerfile 自动构建 Nginx 镜像.
使用 docker 的 RUN 命令,我们将开始基于 Ubuntu 映像创建一个新容器。我们将使用“-t”标志将终端附加到它。
sudo docker run -i -t -p 80:80 ubuntu /bin/bash
Note:执行此命令后,docker可能需要pull在为您创建新容器之前先创建 Ubuntu 映像。
记住:您将被附加到您创建的容器。为了脱离并返回到主终端接入点,请运行转义序列:CTRL+P,然后是 CTRL+Q。连接到 Docker 容器就像从另一个容器内部连接到新的 Droplet 一样。
要将自己重新附加到此容器:
- 使用列出所有正在运行的容器须藤 docker ps
- 找到它的ID
- Use sudo docker 附加 [id]连接回其终端
重要的:请不要忘记,由于我们位于容器中,因此以下所有命令都将在那里执行,而不会影响主机。
为了安装 Nginx 以及我们在该过程中需要的工具,相关的应用程序存储库必须可供下载。
让我们添加 Ubuntu 的universe到基础镜像的默认列表。
echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
使用新添加的源更新列表。
apt-get update
在我们继续安装 Nginx 之前,我们应该安装一些工具,例如 nano - 以防万一。
apt-get install -y nano \
wget \
dialog \
net-tools
由于存储库中提供了它,我们可以简单地使用 apt-get 下载并安装 nginx。
apt-get install -y nginx
使用我们在上一步中安装的文本编辑器 nano,让我们创建一个示例 Nginx 配置来代理与应用程序服务器的连接。
# Delete the default configuration
rm -v /etc/nginx/nginx.conf
# Create a blank one using nano text editor
nano /etc/nginx/nginx.conf
首先,必须在文件顶部添加一行not让 Nginx 生成其进程然后退出。
我们不能允许这种情况发生的原因是因为 docker 依赖于单个进程来运行(它甚至可以是进程管理器),并且当该进程停止时(即在生成工作程序后退出),容器就会停止。
从以下内容开始作为第一行nginx.conf
:
daemon off;
我们将使用一个简单的示例配置来让 Nginx 作为反向代理运行。将以下内容复制并粘贴到daemon off;
操作说明。
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# List of application servers
upstream app_servers {
server 127.0.0.1:8080;
}
# Configuration for the server
server {
# Running port
listen 80;
# Proxying the connections connections
location / {
proxy_pass http://app_servers;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
按 CTRL+X 保存并退出,然后按 Y 确认。
要运行 Nginx,您可以执行以下命令:
service nginx start
就是这样!现在,Nginx 运行在 docker 容器中,可以从外部端口访问80当我们设置使用-p 80:80
flag.
记住:该 Nginx 文件虽然配置正确,但不会执行任何操作,因为当前服务器上没有运行应用程序服务器。您可以复制并使用另一个示例来代替这个示例,该示例仅用作测试 HTTP 标头的转发代理,直到您安装了应用程序服务器并正常工作。
正如我们在上一步中提到的,为了可扩展的生产,这种方式创建容器当然不是推荐的方法。正确的方法可以被认为是使用 Dockerfiles 以结构化的方式自动化构建过程。
在完成了在容器内下载和安装 Nginx 的必要命令之后,我们可以使用相同的知识来编写 Dockerfile,docker 可以使用该文件来构建镜像,然后可以使用该镜像轻松运行 Nginx 实例。
在开始处理 Dockerfile 之前,让我们快速回顾一下基础知识。
Dockerfile 是包含以下内容的脚本commands依次声明,docker 按顺序执行,自动创建新的 docker 镜像。它们对部署有很大帮助。
这些文件总是以使用以下命令定义基础镜像开始:FROM
命令。从那时起,构建过程开始,接下来执行的每个操作都会形成将在主机上提交的最终映像。
Usage:
# Build an image using the Dockerfile at current location
# Tag the final image with [name] (e.g. *nginx*)
# Example: sudo docker build -t [name] .
sudo docker build -t nginx_img .
Note:要了解有关 Dockerfile 的更多信息,请查看我们的文章:Docker 解释:使用 Dockerfile 自动构建镜像.
- ADD:将文件从主机复制到容器中
- CMD:设置要执行的默认命令,或传递到 ENTRYPOINT
- ENTRYPOINT:设置容器内应用程序的默认入口点
- ENV:设置环境变量(例如key = value)
- EXPOSE:将端口暴露给外部
- FROM:设置要使用的基础镜像
- MAINTAINER:设置 Dockerfile 的作者/所有者数据
- RUN:运行命令并提交最终结果(容器)图像
- USER:设置从镜像运行容器的用户
- VOLUME:将目录从主机挂载到容器
- WORKDIR:设置指令的目录CMD待执行
要使用 nano 文本编辑器在当前位置创建 Dockerfile,请执行以下命令:
sudo nano Dockerfile
Note:将以下所有行逐一附加以形成要保存并用于构建的 Dockerfile。
让我们通过定义基础知识(基础知识)开始我们的 Dockerfile,例如FROM
图像(即Ubuntu)和MAINTAINER
.
############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Maintaner Name
按照上一节中的步骤,让我们形成安装 Nginx 的块。
# Install Nginx
# Add application repository URL to the default sources
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx
添加安装 Nginx 的说明后,让我们完成配置 Nginx 并使用 Dockerfile 将默认配置文件替换为我们在构建期间提供的配置文件。
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute
# when creating a new container
CMD service nginx start
最后,Dockerfile 应该是这样的:
############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Maintaner Name
# Install Nginx
# Add application repository URL to the default sources
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute
# when creating a new container
CMD service nginx start
按 CTRL+X 再次保存并退出文件,然后按 Y 确认。
正如我们首先在“基础知识”部分中讨论的那样,Dockerfiles 的用法包括使用“docker build”命令调用它们。
由于我们指示 docker 从当前目录复制配置(即 nginx.conf)以替换默认配置,因此我们需要确保在开始构建过程之前将其与此 Dockerfile 一起放置。
Note:上面解释了复制的过程inNginx 配置为您提供了极大的灵活性,并且无需处理与容器的连接和分离来创建配置文件,从而节省大量时间。现在您可以简单地使用一个来直接构建和运行图像。
创建样本nginx.conf
使用文本编辑器 nano:
sudo nano nginx.conf
并替换其内容以将其用作转发代理进行测试:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 80;
location / {
proxy_pass http://httpstat.us/;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
让我们以同样的方式保存并退出 nginx.conf,按 CTRL+X 并按 Y 确认。
这个 docker 镜像将允许我们移植所有进度并使用单个命令快速创建运行 Nginx 的容器。
要开始使用它,请使用以下内容构建一个新的容器映像:
sudo docker build -t nginx_img_1 .
使用该图像(我们标记为 nginx_img_1),我们可以运行一个新容器:
sudo docker run -name nginx_cont_1 -p 80:80 -i -t nginx_img_1
现在您可以访问 Droplet 的 IP 地址,并且运行 docker 容器的 Nginx 将完成其工作,将您转发到 HTTP 状态测试页面。
Example:
# Usage: Visit http://[my droplet's ip]
http://95.85.10.236/200
响应示例:
200 OK
有关安装 docker(包括其他操作系统)的完整说明,请查看docker 安装文档位于docker.io.
提交者:https://twitter.com/ostezer”>I.S.泰泽尔