Help us understand the problem. What is going on with this article?

Dockerコンテナを作成してApache2.2を動かす

More than 3 years have passed since last update.

はじめに

新しく構築したCentOS 6.6サーバ(CentOSをminimalインストールしたサーバ)に対して、Dockerのインストールを行い、Dockerコンテナ作成 及び Dockerコンテナ内でApache 2.2を起動する手順です。

対象環境

Dockerをインストールする環境は以下になります。

[Dockerホスト側環境]
・CentOS release 6.6 (2.6.32-504.8.1.el6.x86_64)
・docker-io 1.4.1-3

[Dockerコンテナ側環境]
・CentOS release 6.6 (2.6.32-504.8.1.el6.x86_64)
・Apache 2.2.15

参考サイト

Dockerインストールやコンテナ作成にあたり、以下のサイトやページを参考にさせて頂きました。

http://docs.docker.com/installation/centos/#installing-docker---centos-6.5
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ホストOS側のDockerインストール手順

(1) DockerホストOS化したいCentOS 6.6サーバにrootユーザでログインします。

(2) DockerホストOS化したい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 ~]#

(3) DockerホストOS化したいCentOS 6.6サーバに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ホストOS上でDockerコンテナ作成

(5) ホストOS上で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ホストOSからDockerコンテナ内のApacheへアクセスする手順

(9) ApacheをインストールしたDockerコンテナのイメージを保存します。

[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 ~]#

(10) Dockerコンテナをポートフォワード(-pオプションでポート番号を指定)するように起動し、ホストOS側から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ホストOS上で、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を利用すると、手軽に環境を用意出来るので便利ですね。

以上になります。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away