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