translated wi-cuckoo

This commit is contained in:
wi-cuckoo 2015-06-15 10:52:46 +08:00
parent 7ef21cc716
commit ebc81d816b
2 changed files with 279 additions and 283 deletions

View File

@ -1,283 +0,0 @@
translating wi-cuckoo
How to Configure Apache Containers with Docker on Fedora 22
================================================================================
In this article we are going to learn about Docker, how to use it to deploy Apache httpd and how can you share it on Docker Hub. First we are going to learn how to pull and use images hosted on Docker Hub, and then install Apache on an image of Fedora 22 interactively, after that we are going to learn how to use a Dockerfile to make an image in a faster and more elegant way, finally we are going to publish the image we've created in Docker Hub, so anyone will be able download and use it later.
### Installing Docker and saying hello world ###
**Requirements**
You will need atleast these things to run Docker:
- You need a 64bit Kernel version 3.10 or higher
- Iptables 1.4 - This will be used by Docker to make the network wizardry, such as Network Address Translation.
- Git 1.7 - Docker uses Git to make it transactions to repositories, like Docker Hub
- ps - This utility is present in most environments and is provided in the procps package.
- root - despite normal users can run docker client by TCP and other means, we will assume that you are root, for sake of simplicity
#### Install docker using dnf ####
The following commands will install Docker
dnf update && dnf install docker
**Note**: You can still use Yum in Fedora 22, but it's deprecated in favor of DNF and is not present in a clean install.
#### Check your install ####
The first command we are going to use is docker info, this give you many information:
docker info
Also try **docker version**:
docker version
#### Starting Docker as daemon ####
You should start a docker instance that will take care of our requests.
docker -d
Now set docker to start with the system, so you don't need to run the previous command every time you reboot.
chkconfig docker on
Let's say hello world with Busybox:
docker run -t busybox /bin/echo "hello world"
In this command, we tell Docker to execute /bin/echo "hello world" in an instance/container of the Busybox image, which is a minimal POSIX environment based in a single binary and links to it.
If Docker can't find a local image of Busybox on your system, it will pull the image automatically from Docker Hub, as you can see in the following screenshot:
![Hello world with Busybox](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-hello-world-busybox-complete.png)
Hello world with Busybox
Try the same command again, this time as Docker already have the Busybox image locally, all you will see is the output of echo:
docker run -t busybox /bin/echo "hello world"
Try also the following to enter in the container environment:
docker run -it busybox /bin/sh
To leave and stop the container use the **exit** command
### Dockerizing Apache interactively ###
Pull/Download the Fedora image:
docker pull fedora:22
Run a container dettached from the console:
docker run -d -t fedora:22 /bin/bash
List running containers and identify by name as follows
docker ps
![listing with docker ps and attaching with docker attach](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-ps-with-docker-attach-highlight.png)
listing with docker ps and attaching with docker attach
The name angry_nobel was given to our instance, so let's attach:
docker attach angry_nobel
Note: Every time you run a container a new name will be given, if you need a constant name for you container you should use the -name parameter to docker run command.
#### Installing Apache ####
The following command will update DNF database, download install Apache (httpd package) and clean DNF cache to make the image small
dnf -y update && dnf -y install httpd && dnf -y clean all
Configuring Apache
The only thing we are going to change httpd.conf is the ServerName, this makes Apache stops to complain.
sed -i.orig 's/#ServerName/ServerName/' /etc/httpd/conf/httpd.conf
**Set the environment**
To make Apache run in standalone mode, you must provide some information in the form of enviroenment variables, and also you will need the directories set in these variables, so we are going to make this with a small shell script that will also start Apache
vi /etc/httpd/run_apache_foreground
----------
#!/bin/bash
#set variables
APACHE_LOG_DI=R"/var/log/httpd"
APACHE_LOCK_DIR="/var/lock/httpd"
APACHE_RUN_USER="apache"
APACHE_RUN_GROUP="apache"
APACHE_PID_FILE="/var/run/httpd/httpd.pid"
APACHE_RUN_DIR="/var/run/httpd"
#create directories if necessary
if ! [ -d /var/run/httpd ]; then mkdir /var/run/httpd;fi
if ! [ -d /var/log/httpd ]; then mkdir /var/log/httpd;fi
if ! [ -d /var/lock/httpd ]; then mkdir /var/lock/httpd;fi
#run Apache
httpd -D FOREGROUND
**Alternatively**, you can past and run this snippet on the container shell:
dnf -y install git && git clone https://github.com/gaiada/run-apache-foreground.git && cd run-apach* && ./install && dnf erase git
The inline script above will, install Git, clone [this repository][1], put the script in place and ask you if you want uninstall Git.
**Saving your container state**
Your container is now ready to run Apache, now it is time to save the current state of this container in an image to be able use whenever you need.
To leave the container environment, you must press **Ctrl+p** followed by **Ctrl+q**, if you just call exit in the shell, you will also stop the container and lost what you have done so far.
Once you are back to the Docker host, use **docker commit** followed by the container and the repository name/tag you desire:
docker commit angry_nobel gaiada/apache
Now that you saved the container status into a image, you can use **docker stop** on the running container:
docker stop angry_nobel
**Run and test your image**
Finally, run a container from your new image and redirect connections on port 80 to it with:
docker run -p 80:80 -d -t gaiada/apache /etc/httpd/run_apache_foreground
At this point, you are already running Apache in your container, open your browser to access the service in [http://localhost][2] and you will see the Apache default page as follows
![Apache default page running from Docker container](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-apache-running.png)
Apache default page running from Docker container
### Dockerizing Apache with a Dockerfile ###
Now we are going to create a new Apache image, this time the steps will be written in a Dockerfile, that will be processed to form the image.
First, create a directory on which we will put the Dockerfile and enter this directory:
mkdir apachedf; cd apachedf
And then create a file named Dockerfile with the following content:
FROM fedora:22
MAINTAINER Carlos Alberto
LABEL version="0.1"
RUN dnf -y update && dnf -y install httpd && dnf -y clean all
RUN [ -d /var/log/httpd ] || mkdir /var/log/httpd
RUN [ -d /var/run/httpd ] || mkdir /var/run/httpd
RUN [ -d /var/lock/httpd ] || mkdir /var/lock/httpd
RUN sed -i.orig 's/#ServerName/ServerName/' /etc/httpd/conf/httpd.conf
ENV APACHE_RUN_USER apache
ENV APACHE_RUN_GROUP apache
ENV APACHE_LOG_DIR /var/log/httpd
ENV APACHE_LOCK_DIR /var/lock/httpd
ENV APACHE_RUN_DIR /var/run/httpd
ENV APACHE_PID_FILE /var/run/httpd/httpd.pid
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
Let's see what is on this Dockerfile:
**FROM** - This tells docker that we are going to use Fedora 22 as base image
**MANTAINER** and **LABEL** - these commands are informative and have no direct influence on the image
**RUN** - Automate the steps we've done interactively, install Apache, create directories and edit httpd.conf
**ENV** - Set the environment variables, now we don't need the run_apache_foreground script anymore.
**EXPOSE** - Expose the port 80 to the world
**CMD** - Set the default command to httpd, so we don't need to do this every time we start a new container
**Build the image**
Now we are going to build the image and put the TAG gaiada/apachedf on it:
docker build -t gaiada/apachedf:0.1 .
![docker build complete](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-build-complete.png)
docker build complete
Use **docker images** list the local images and see if your new image is there:
docker images
And then run you new image:
docker run -t -p 80:80 gaiada/apachedf
That is it for the Dockerfile, using this feature make things much easier, faster and reproducible.
### Publishing your images ###
Until now, you just pulled images from Docker Hub, but you can also push you image and pull them later as needed. In fact other can also download your image and use it in their systems without the need of change anything and now we are going to learn how to make our image available for others worldwide.
**Creating account**
For you to be able to push your image on Docker Hub, you need to create an account. Access [https://hub.docker.com/account/signup/][3] and fill the following form:
![Docker Hub signup page](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-hub-signup.png)
Docker Hub signup page
**Login**
Enter the following command and proceed with the login name, password and email you provided in your account creation
docker login
After you do the first login, your account information will be recorded in **~/.dockercfg**
**Pushing**
Push the page to the server with the **docker push [registry/]your_login/repository_name[:tag]**
docker push docker.io/gaiada/apachedf
You might see something like this on your console:
![Docker push Apache image complete](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-pushing-apachedf-complete.png)
Docker push Apache image complete
### Conclusion ###
Now that you know how to Dockerize Apache, try to include some modules, Perl, PHP, proxy, HTTPS, or whatever you need. I hope you guys liked it, and push your own images on Docker Hub.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-how-to/configure-apache-containers-docker-fedora-22/
作者:[Carlos Alberto][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/carlosal/
[1]:https://github.com/gaiada/run-apache-foreground
[2]:http://localhost/
[3]:https://hub.docker.com/account/signup/

View File

@ -0,0 +1,279 @@
如何在Fedora 22上面配置Apache的Docker容器
=============================================================================
在这篇文章中,我们将会学习关于Docker的一些知识如何使用Docker部署Apache httpd服务并且共享到Docker Hub上面去。首先我们学习怎样拉取和使用Docker Hub里面的镜像然后交互式地安装Apache到一个Fedora 22的镜像里去之后我们将会学习如何用一个Dockerfile文件来制作一个镜像以一种更快更优雅的方式。最后我们会在Docker Hub上公开我们创建地镜像这样以后任何人都可以下载并使用它。
### 安装Docker运行hello world ###
**要求**
运行Docker里至少需要满足这些
- 你需要一个64位的内核版本3.10或者更高
- Iptables 1.4 - Docker会用来做网络配置如网络地址转换NAT
- Git 1.7 - Docker会使用Git来与仓库交流如Docker Hub
- ps - 在大多数环境中这个工具都存在在procps包里有提供
- root - 防止一般用户可以通过TCP或者其他方式运行Docker为了简化我们会假定你就是root
### 使用dnf安装docker ###
以下的命令会安装Docker
dnf update && dnf install docker
**注意**在Fedora 22里你仍然可以使用Yum命令但是被DNF取代了而且在纯净安装时不可用了。
### 检查安装 ###
我们将要使用的第一个命令是docker info这会输出很多信息给你
docker info
也可以试着用**docker version**
docker version
### 启动Dcoker为守护进程 ###
你应该启动一个docker实例然后她会处理我们的请求。
docker -d
让我们用Busybox来打印hello world
dockr run -t busybox /bin/echo "hello world"
这个命令里我们告诉Docker执行 /bin/echo "hello world"在Busybox镜像的一个实例/容器里。Busybox是一个小型的POSIX环境将许多小工具都结合到了一个单独的可执行程序里。
如果Docker不能在你的系统里找到本地的Busybox镜像她就会自动从Docker Hub里拉取镜像正如你可以看下如下的快照
![Hello world with Busybox](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-hello-world-busybox-complete.png)
Hello world with Busybox
再次尝试相同的命令这次由于Docker已经有了本地的Busybox镜像所有你将会看到的就是echo的输出
docker run -t busybox /bin/echo "hello world"
也可以尝试以下的命令进入到容器环境里去:
docker run -it busybox /bin/sh
使用**exit**命令可以离开容器并停止它
### 交互式地Docker化Apache ###
拉取/下载 Fedora 镜像:
docker pull fedora:22
起一个容器在后台运行:
docker run -d -t fedora:22 /bin/bash
列出正在运行地容器,并用名字标识,如下
docker ps
![listing with docker ps and attaching with docker attach](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-ps-with-docker-attach-highlight.png)
使用docker ps列出并使用docker attach进入一个容器里
angry_noble是docker分配给我们容器的名字所以我们来附上去
docker attach angry_noble
注意:每次你起一个容器,就会被给与一个新的名字,如果你的容器需要一个固定的名字,你应该在 docker run 命令里使用 -name 参数。
### 安装Apache ###
下面的命令会更新DNF的数据库下载安装Apachehttpd包并清理dnf缓存使镜像尽量小
dnf -y update && dnf -y install httpd && dnf -y clean all
配置Apache
我们需要修改httpd.conf的唯一地方就是ServerName这会使Apache停止抱怨
sed -i.orig 's/#ServerName/ServerName/' /etc/httpd/conf/httpd.conf
**设定环境**
为了使Apache运行为单机模式你必须以环境变量的格式提供一些信息并且你也需要在这些变量里的目录设定所以我们将会用一个小的shell脚本干这个工作当然也会启动Apache
vi /etc/httpd/run_apache_foreground
----------
#!/bin/bash
#set variables
APACHE_LOG_DI=R"/var/log/httpd"
APACHE_LOCK_DIR="/var/lock/httpd"
APACHE_RUN_USER="apache"
APACHE_RUN_GROUP="apache"
APACHE_PID_FILE="/var/run/httpd/httpd.pid"
APACHE_RUN_DIR="/var/run/httpd"
#create directories if necessary
if ! [ -d /var/run/httpd ]; then mkdir /var/run/httpd;fi
if ! [ -d /var/log/httpd ]; then mkdir /var/log/httpd;fi
if ! [ -d /var/lock/httpd ]; then mkdir /var/lock/httpd;fi
#run Apache
httpd -D FOREGROUND
**另外地**你可以粘贴这个片段代码到容器shell里并运行:
dnf -y install git && git clone https://github.com/gaiada/run-apache-foreground.git && cd run-apach* && ./install && dnf erase git
上面的内嵌脚本会安装Git克隆[这个仓库][1]到文件里去运行脚本并询问你是否卸载Git。
**保存你的容器状态**
你的容器现在可以运行Apache是时候保存容器当前的状态为一个镜像以备你需要的时候使用。
为了离开容器环境,你必须顺序按下 **Ctrl+q****Ctrl+p**如果你仅仅在shell执行exit你同时也会停止容器失去目前为止你做过的所有工作。
回到Docker主机使用 **docker commit** 加容器和你期望的仓库名字/标签:
docker commit angry_noble gaiada/apache
现在,你保存了容器的状态到一个镜像里,可以使用 **docker stop** 停止容器了:
docker stop angry_noble
**运行并测试你的镜像**
最后从你的新镜像起一个容器并且重定向80端口到容器
docker run -p 80:80 -d -t gaiada/apache /etc/httpd/run_apache_foreground
到目前你正在你的容器里运行Apache打开你的浏览器访问该服务在[http://localhost][2]你将会看到如下Apache默认的页面
![Apache default page running from Docker container](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-apache-running.png)
在容器里运行的Apache默认页面
### 使用Dockerfile Docker化Apache ###
现在我们将要去创建一个新的Apache镜像这次所有步骤会写在一个Dockerfile文件里文件将会被用于生成该镜像。
首先新建一个目录在里面放Dockerfile文件并进入该目录
mkdir apachedf; cd apachedf
然后创建一个名为Dockerfile的文件添加以下内容
FROM fedora:22
MAINTAINER Carlos Alberto
LABEL version="0.1"
RUN dnf -y update && dnf -y install httpd && dnf -y clean all
RUN [ -d /var/log/httpd ] || mkdir /var/log/httpd
RUN [ -d /var/run/httpd ] || mkdir /var/run/httpd
RUN [ -d /var/lock/httpd ] || mkdir /var/lock/httpd
RUN sed -i.orig 's/#ServerName/ServerName/' /etc/httpd/conf/httpd.conf
ENV APACHE_RUN_USER apache
ENV APACHE_RUN_GROUP apache
ENV APACHE_LOG_DIR /var/log/httpd
ENV APACHE_LOCK_DIR /var/lock/httpd
ENV APACHE_RUN_DIR /var/run/httpd
ENV APACHE_PID_FILE /var/run/httpd/httpd.pid
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
我们一起来看看Dockerfile里面有什么
**FROM** - 这告诉docker我们将要使用Fedora 22作为基础镜像
**MAINTAINER** 和 **LABLE** - 这些命令对镜像没有直接作用,属于标记信息
**RUN** - 自动完成我们之前交互式做的工作安装Apache新建目录并编辑httpd.conf
**ENV** - 设置环境变量现在我们再不需要run_apache_foreground脚本
**EXPOSE** - 暴露80端口给外网
**CMD** - 设置默认的命令启动httpd服务这样我们就不需要每次起一个新的容器都重复这个工作
**建立该镜像**
现在我们将要建立这个镜像并为其添加tag gaiada/apachedf
docker build -t gaiada/apachedf:0.1 .
![docker build complete](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-build-complete.png)
docker完成创建
使用 **docker images** 列出本地镜像,查看是否存在你新建的镜像:
docker images
然后运行新的镜像:
docker run -t -p 80:80 gaiada/apachedf
这就是Dockerfile的工作使用这项功能会使得事情更加容易快速并且可重复生成。
### 公开你的镜像 ###
直到现在你仅仅是从Docker Hub拉取了镜像但是你也可以推送你的镜像以后需要也可以再次拉取他们。实际上其他人也可以下载你的镜像在他们的系统中使用它而不需要改变任何东西。现在我们将要学习如何使我们的镜像对世界上的其他人可用。
**创建帐号**
为了能够在Docker Hub上推送你的镜像你需要创建一个帐号。访问 [https://hub.docker.com/account/signup/][3],填写下面的表格:
![Docker Hub signup page](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-hub-signup.png)
Docker Hub 注册页面
**登录**
输入下面的命令,接着输入你注册时提供的用户名,密码和邮箱
docker login
第一次登录过后,你的帐号信息会被记录在 **/.dockercfg**
**推送**
推送镜像,使用 **docker push [registry/]your_login/repository_name[:tag]**
docker push docker.io/gaiada/apachedf
你可能会看见像这样的输出,在你的控制台上:
![Docker push Apache image complete](http://blog.linoxide.com/wp-content/uploads/2015/06/docker-pushing-apachedf-complete.png)
Docker推送Apache镜像完成
### 结论 ###
现在你知道如何Docker化Apache试一试包含其他一些组块PerlPHPproxyHTTPS或者任何你需要的东西。我希望你们这些家伙喜欢她并推送你们自己的镜像到Docker Hub。
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-how-to/configure-apache-containers-docker-fedora-22/
作者:[Carlos Alberto][a]
译者:[wi-cuckoo](https://github.com/wi-cuckoo)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://linoxide.com/author/carlosal/
[1]:https://github.com/gaiada/run-apache-foreground
[2]:http://localhost/
[3]:https://hub.docker.com/account/signup/