创建Docker容器并运行Apache2.2

首先

在新建的CentOS 6.6服务器(使用了最小安装的CentOS)上,安装Docker并创建Docker容器,然后在Docker容器内启动Apache 2.2的步骤。

目标环境

以下是安装Docker所需的环境。

以下是Docker主机的环境:
• CentOS 6.6版本(2.6.32-504.8.1.el6.x86_64)
• docker-io 1.4.1-3

[Docker容器的环境]
· CentOS 发布版本 6.6 (2.6.32-504.8.1.el6.x86_64)
· Apache 2.2.15

参考网页

在安装Docker和创建容器的过程中,我参考了以下的网站和页面。

以下是获取CentOS 6.5上安装Docker的链接:
http://docs.docker.com/installation/centos/#installing-docker—centos-6.5

以下是有关Docker的链接:
http://apatheia.info/blog/2013/06/17/docker/
http://qiita.com/tomy103rider/items/bf4042ad2082a23f393b
http://qiita.com/zwirky/items/991f61a231f4e198a320
http://qiita.com/gologo13/items/1bdba6085ec79153bf1a

Docker在宿主操作系统上的安装步骤。

(1) 使用root用户登录到希望将其转为Docker主机操作系统的CentOS 6.6服务器。

(2) 升级Docker主机的CentOS 6.6服务器的内核和软件包。

[root@example-CentOS-6-6-Docker ~]# hostname
example-CentOS-6-6-Docker
[root@example-CentOS-6-6-Docker ~]# yum -y update
[root@example-CentOS-6-6-Docker ~]#

在希望将CentOS 6.6服务器转为Docker主机操作系统时,将安装Docker。

[root@example-CentOS-6-6-Docker ~]# yum -y remove docker
[root@example-CentOS-6-6-Docker ~]# rpm -qa | grep docker
[root@example-CentOS-6-6-Docker ~]#

[root@example-CentOS-6-6-Docker ~]# rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
[root@example-CentOS-6-6-Docker ~]# yum -y install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@example-CentOS-6-6-Docker ~]# yum -y install docker-io
[root@example-CentOS-6-6-Docker ~]#

(4) 在 CentOS 6.6 服务器上设置自动启动 Docker 进程。

[root@example-CentOS-6-6-Docker ~]# service docker start
Starting cgconfig service:                                 [  OK  ]
Starting docker:                                           [  OK  ]
[root@example-CentOS-6-6-Docker ~]#

[root@example-CentOS-6-6-Docker ~]# ps awux | grep -v grep | grep docker
root      1102  0.8  1.3 364136  8448 pts/0    Sl   02:14   0:00 /usr/bin/docker -d
root      1132  1.6  0.2 114200  1720 pts/0    D    02:14   0:00 mkfs.ext4 -E nodiscard,lazy_itable_init=0 /dev/mapper/docker-202:65-400750-base
[root@example-CentOS-6-6-Docker ~]#

[root@example-CentOS-6-6-Docker ~]# chkconfig docker on
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# chkconfig --list | grep docker
docker          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@example-CentOS-6-6-Docker ~]#

在Docker主机操作系统上创建Docker容器。

在主机操作系统上下载 CentOS 的 Docker 镜像。

[root@example-CentOS-6-6-Docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# docker pull centos:centos6
centos:centos6: The image you are pulling has been verified
511136ea3c5a: Pull complete
5b12ef8fd570: Pull complete
a30bc9f3097e: Pull complete
Status: Downloaded newer image for centos:centos6
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos6             a30bc9f3097e        3 weeks ago         215.8 MB
[root@example-CentOS-6-6-Docker ~]#

(6) 创建并启动Docker容器。

[root@example-CentOS-6-6-Docker ~]# docker run -i -t centos:centos6 /bin/bash
[root@d846367e8860 /]#
[root@d846367e8860 /]# hostname
d846367e8860
[root@d846367e8860 /]#

在Docker容器中安装Apache。

(7)在Docker容器内安装Apache。

[root@d846367e8860 /]# uname -a
Linux d846367e8860 2.6.32-504.8.1.el6.x86_64 #1 SMP Wed Jan 28 21:11:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@d846367e8860 /]# rpm -qa | grep httpd
[root@d846367e8860 /]#

[root@d846367e8860 /]# yum -y install httpd
[root@d846367e8860 /]#

[root@d846367e8860 /]# rpm -qa | grep httpd
httpd-tools-2.2.15-39.el6.centos.x86_64
httpd-2.2.15-39.el6.centos.x86_64
[root@d846367e8860 /]#

(8) 在 Docker 容器中启动 Apache。

[root@d846367e8860 /]# cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.ORG
[root@d846367e8860 /]# diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.ORG
[root@d846367e8860 /]#
[root@d846367e8860 /]# sed -i -e 's/\#ServerName www.example.com:80/ServerName example-web-server.example.com/g' /etc/httpd/conf/httpd.conf
[root@d846367e8860 /]#
[root@d846367e8860 /]# diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.ORG
276c276
< ServerName example-web-server.example.com
---
> #ServerName www.example.com:80
[root@d846367e8860 /]#

[root@d846367e8860 /]# ps awux | grep -v grep | grep http
[root@d846367e8860 /]#
[root@d846367e8860 /]# /etc/init.d/httpd configtest
Syntax OK
[root@d846367e8860 /]# /etc/init.d/httpd start
Starting httpd:
[root@d846367e8860 /]#
[root@d846367e8860 /]# ps awux | grep -v grep | grep http
root        95  0.0  0.6 175276  3732 ?        Ss   17:34   0:00 /usr/sbin/httpd
apache      97  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache      98  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache      99  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache     100  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache     101  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache     102  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache     103  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
apache     104  0.0  0.4 175276  2420 ?        S    17:34   0:00 /usr/sbin/httpd
[root@d846367e8860 /]#
[root@d846367e8860 /]# ls -lrta /var/www/html/
total 8
drwxr-xr-x. 2 root root 4096 Oct 16 14:49 .
drwxr-xr-x. 6 root root 4096 Feb 27 17:34 ..
[root@d846367e8860 /]#

[root@d846367e8860 /]# echo "docker test server" >> /var/www/html/index.html
[root@d846367e8860 /]#

[root@d846367e8860 /]# curl http://127.0.0.1/
docker test server
[root@d846367e8860 /]#

[root@d846367e8860 /]# chkconfig httpd on
[root@d846367e8860 /]#

[root@d846367e8860 /]# exit
exit
[root@example-CentOS-6-6-Docker ~]#

从Docker宿主操作系统访问Docker容器内的Apache的步骤。

保存Docker容器中已安装的Apache为镜像。

[root@example-CentOS-6-6-Docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d846367e8860        centos:centos6      "/bin/bash"         18 minutes ago      Exited (0) 19 seconds ago                       backstabbing_colden
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# docker commit d846367e8860 centos:centos6/httpd
c9d152242caad4a7bf68f850d153a6150f9c65203ca8ec2d5477b5aee70bf0c8
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
d846367e8860        centos:centos6      "/bin/bash"         20 minutes ago      Exited (0) About a minute ago                       backstabbing_colden
[root@example-CentOS-6-6-Docker ~]#

[root@example-CentOS-6-6-Docker ~]# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos:centos6/httpd   latest              c9d152242caa        22 seconds ago      278 MB
centos                 centos6             a30bc9f3097e        3 weeks ago         215.8 MB
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# docker run -i -t centos:centos6/httpd /bin/bash
[root@4922d9e8597e /]#
[root@4922d9e8597e /]# ps awux | grep -v grep | grep http
[root@4922d9e8597e /]#
[root@4922d9e8597e /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@4922d9e8597e /]#
[root@4922d9e8597e /]# ps awux | grep -v grep | grep http
root        23  0.5  0.6 175276  3644 ?        Ss   17:41   0:00 /usr/sbin/httpd
apache      25  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      26  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      27  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      28  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      29  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      30  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      31  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
apache      32  0.0  0.3 175276  2416 ?        S    17:41   0:00 /usr/sbin/httpd
[root@4922d9e8597e /]#
[root@4922d9e8597e /]# curl http://127.0.0.1/
docker test server
[root@4922d9e8597e /]#

[root@4922d9e8597e /]# exit
exit
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# ps awux | grep -v grep | grep http
[root@example-CentOS-6-6-Docker ~]#

启动Docker容器,并通过-p选项指定端口转发,以便能够从主机操作系统访问Docker容器内的Apache。

[root@example-CentOS-6-6-Docker ~]# docker ps -a
CONTAINER ID        IMAGE                         COMMAND             CREATED              STATUS                      PORTS               NAMES
4922d9e8597e        centos:centos6/httpd:latest   "/bin/bash"         About a minute ago   Exited (0) 42 seconds ago                       drunk_thompson
d846367e8860        centos:centos6                "/bin/bash"         21 minutes ago       Exited (0) 3 minutes ago                        backstabbing_colden
[root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]# docker run -i -t -p 50050:80 centos:centos6/httpd /bin/bash
[root@38c4efed92a8 /]#
[root@38c4efed92a8 /]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@38c4efed92a8 /]#
[root@38c4efed92a8 /]# curl http://127.0.0.1/
docker test server
[root@38c4efed92a8 /]#

(11) 从Docker容器中注销。

在Docker容器内,按下Ctrl + p,然后再按下Ctrl + q键,从容器内登出。

[root@38c4efed92a8 /]# → ★Ctrl + pキーとCtrl + qキーの順に押下して、コンテナ内からログアウトします。

[root@38c4efed92a8 /]# [root@example-CentOS-6-6-Docker ~]#
[root@example-CentOS-6-6-Docker ~]#

(12)验证在Docker主机操作系统上能够访问Docker容器内的Apache。

[root@example-CentOS-6-6-Docker ~]# curl http://127.0.0.1:50050/
docker test server
[root@example-CentOS-6-6-Docker ~]#

使用Docker很方便,因为它可以轻松准备环境,非常方便。

以上是我的回答。

bannerAds