takaya030の備忘録

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

Docker で CentOS7 + Apache2.4 + PHP7 環境のイメージを作る

CentOS7 で動作する Apache + PHP の環境を作成したときの手順メモ

Docker 環境

Windows10 Home Edition
VirtualBox 5.1.6
docker 1.10.3
docker-machine 0.6.0

各種設定ファイル

Dockerfile

#
# Apache + PHP
#
# 2017-01-15
#   CentOS 7.3 + epel,remi
#   Apache 2.4.6
#   PHP 7.0.14

FROM centos:7
MAINTAINER takaya030

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

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

# httpd, sshd, scp, openssl, sudo, which
RUN yum install -y httpd httpd-tools openssh-server openssh-clients openssl sudo which && \
    yum clean all

# libmcrypt, supervisor
RUN yum install --enablerepo=epel -y libmcrypt supervisor && \
    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-php70 -y php-pecl-memcached && \
    yum clean all

# php
RUN yum install --enablerepo=remi-php70 -y php php-devel php-gd php-mbstring php-mcrypt php-mysqlnd php-pear php-xml php-opcache && \
    yum clean all && \
	sed -i -e "s/;date.timezone *=.*$/date.timezone = Asia\/Tokyo/" /etc/php.ini

# composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# phpunit
RUN curl -L https://phar.phpunit.de/phpunit.phar > /usr/local/bin/phpunit && \
	chmod +x /usr/local/bin/phpunit

# initialize for ssh
RUN sed -i '/pam_loginuid\.so/s/required/optional/' /etc/pam.d/sshd && \
	ssh-keygen -A

# create login user
RUN useradd -d /home/laravel -m -s /bin/bash laravel && \
	echo laravel:****laravel | chpasswd && \
	echo 'laravel ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# timezone
RUN cp -p /usr/share/zoneinfo/Japan /etc/localtime

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
ADD ./supervisord.conf /etc/supervisord.conf

EXPOSE 22 80

CMD ["/usr/bin/supervisord"]

index.html

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

phpinfo.php

<?php
    phpinfo();

supervisor.conf

長いので diff のみ

--- supervisord.conf.orig	Sun Jan 15 18:31:07 2017
+++ supervisord.conf	Sun Jan 15 19:08:05 2017
@@ -18,7 +18,7 @@
 logfile_backups=10          ; (num of main logfile rotation backups;default 10)
 loglevel=info               ; (log level;default info; others: debug,warn,trace)
 pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
-nodaemon=false              ; (start in foreground if true;default false)
+nodaemon=true              ; (start in foreground if true;default false)
 minfds=1024                 ; (min. avail startup file descriptors;default 1024)
 minprocs=200                ; (min. avail process descriptors;default 200)
 ;umask=022                  ; (process file creation umask;default 022)
@@ -127,3 +127,9 @@
 
 [include]
 files = supervisord.d/*.ini
+
+[program:sshd]
+command=/usr/sbin/sshd -D
+
+[program:httpd]
+command=/usr/sbin/httpd -DFOREGROUND

httpd.conf

長いので diff のみ

--- httpd.conf.orig	Sun Jan 15 18:31:49 2017
+++ httpd.conf	Sun Jan 15 19:38:42 2017
@@ -93,6 +93,7 @@
 # If your host doesn't have a registered DNS name, enter its IP address here.
 #
 #ServerName www.example.com:80
+ServerName webapp:80
 
 #
 # Deny access to the entirety of your server's filesystem. You must
@@ -116,7 +117,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"
 
 #
 # Relax access to content within /var/www.
@@ -128,7 +130,8 @@
 </Directory>
 
 # Further relax access to the default document root:
-<Directory "/var/www/html">
+#<Directory "/var/www/html">
+<Directory "${WEBAPP_ROOT}/public">
     #
     # Possible values for the Options directive are "None", "All",
     # or any combination of:
@@ -148,7 +151,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.
@@ -161,7 +164,7 @@
 # is requested.
 #
 <IfModule dir_module>
-    DirectoryIndex index.html
+    DirectoryIndex index.php index.html
 </IfModule>
 
 #

httpd.conf と supervisor.conf の完全版はこちらに置いてあります
centos7_httpd.conf GitHub

イメージのビルド

$ docker build -t takaya030/webapp

イメージの確認

$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
takaya030/webapp                   latest              46e548051792        3 hours ago         528 MB
centos                             7                   67591570dd29        4 weeks ago         191.8 MB

動作確認

$ docker run -d -p 80:80 -p 2022:22 --name webapp takaya030/webapp

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