takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Docker で Apache + PHP 環境のイメージを作る

Laravel や Google App Engine for PHP での開発を想定したコンテナイメージを作成しました。
MySQLmemcached は別コンテナにする予定なので、今回は Apache + PHP のみになります。

Dockerfile

#
# Apache + PHP
#
# 2015-06-22
#   CentOS 6.6 + epel,remi,rpmforge
#   Apache 2.2.15
#   PHP 5.5.26

FROM centos:6
MAINTAINER takaya030

# update yum
RUN yum update -y && \
    yum clean all

# epel repo
RUN yum install -y http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/epel.repo

# remi repo
RUN yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/remi.repo

# rpmforge repo
RUN yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/rpmforge.repo

# httpd
RUN yum install -y httpd httpd-tools && \
    yum clean all

# libmcrypt, s3cmd
RUN yum install --enablerepo=epel -y libmcrypt s3cmd && \
    yum clean all

# gd-last (for php-gd)
RUN yum install --enablerepo=remi -y gd-last && \
    yum clean all

# php-pecl-memcached
RUN yum install --enablerepo=remi,remi-php55 -y php-pecl-memcached && \
    yum clean all

# php
RUN yum install --enablerepo=remi-php55 -y php php-devel php-gd php-mbstring php-mcrypt php-mysqlnd php-pear php-xml php-opcache && \
    yum clean all

# modify /etc/php.ini
RUN sed -i -e "s/;date.timezone *=.*$/date.timezone = Asia\/Tokyo/" /etc/php.ini

ENV WEBAPP_ROOT /webapp

ADD ./httpd.conf /etc/httpd/conf/httpd.conf
ADD ./index.html /webapp/public/index.html
ADD ./phpinfo.php /webapp/public/phpinfo.php

EXPOSE 80

CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

index.html

<html>
	<body>
		<h1>It works!</h1>
		<p><a href="./phpinfo.php">phpinfo</a></p>
	</body>
</html>

phpinfo.php

<?php
    phpinfo();

httpd.conf

長いので diff のみ

--- httpd.conf.orig	Wed Jun 24 19:21:26 2015
+++ httpd.conf	Tue Sep  1 23:54:57 2015
@@ -274,6 +274,7 @@
 # redirections work in a sensible way.
 #
 #ServerName www.example.com:80
+ServerName webapp:80
 
 #
 # UseCanonicalName: Determines how Apache constructs self-referencing 
@@ -289,7 +290,8 @@
 # documents. By default, all requests are taken from this directory, but
 # symbolic links and aliases may be used to point to other locations.
 #
-DocumentRoot "/var/www/html"
+#DocumentRoot "/var/www/html"
+DocumentRoot "${WEBAPP_ROOT}/public"
 
 #
 # Each directory to which Apache has access can be configured with respect
@@ -314,7 +316,8 @@
 #
 # This should be changed to whatever you set DocumentRoot to.
 #
-<Directory "/var/www/html">
+#<Directory "/var/www/html">
+<Directory "${WEBAPP_ROOT}/public">
 
 #
 # Possible values for the Options directive are "None", "All",
@@ -335,7 +338,7 @@
 # It can be "All", "None", or any combination of the keywords:
 #   Options FileInfo AuthConfig Limit
 #
-    AllowOverride None
+    AllowOverride All
 
 #
 # Controls who can get stuff from this server.
@@ -399,7 +402,7 @@
 # negotiated documents.  The MultiViews Option can be used for the 
 # same purpose, but it is much slower.
 #
-DirectoryIndex index.html index.html.var
+DirectoryIndex index.php index.html index.html.var
 
 #
 # AccessFileName: The name of the file to look for in each directory

イメージのビルド

docker@dev:~$ docker build -t takaya030/webapp

イメージの確認

docker@dev:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
takaya030/webapp    latest              077220c8547a        4 minutes ago       458.9 MB
centos              6                   a005304e4e74        3 days ago          203.1 MB

動作確認

docker@dev:~$ docker run -d -p 80:80 --name webapp takaya030/webapp

WEBブラウザで "http://192.168.99.100" にアクセスして下の画像のように表示されれば動作しています。