amazonlinux - php7.3のDockerファイル作成(Laravel5.8-Project)
Laravel5.8をインストール
下記のコマンドでec2-sampleのlaravel 5.8プロジェクト作成
# composer create-project "laravel/laravel=5.8.*" ec2-sample
ec2-sampleでコマンドでimageファイル作成
# docker pull amazonlinux:2017.03
# docker pull busybox
# docker pull mysql:5.6
プロジェクト(ec2-sample/)直下にdocker関連ファイル作成
1。 ec2-sample/docker-compose.ymlファイル作成
docker-compose.yml
version: '2'
services:
data:
image: busybox
container_name: docker_data
restart: always
volumes:
- ./docker/mysql/storage:/var/lib/mysql
- ./docker/php/default.conf:/etc/httpd/conf.d/default.conf
- ./docker/php/logs:/home/ec2-user/logs
tty: true
mysql:
build: "./docker/mysql"
container_name: docker_mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: db_sample
ports:
- "4306:3306"
networks:
app_net:
ipv4_address: 172.20.0.2
php:
build: "./docker/php"
volumes_from:
- data
container_name: docker_sample
depends_on:
- mysql
restart: always
ports:
- "8888:80"
volumes:
- "./:/home/ec2-user/public_html"
links:
- mysql
tty: true
networks:
app_net:
ipv4_address: 172.20.0.3
networks:
app_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.20.0.0/16
gateway: 172.20.0.1
2。ec2-sample/dockerディレクトリに下記のmysql関連とphp関連ディレクトリとファイル作成
(1) mysql関連:
mysql/Dockerfile
FROM mysql:5.6.38
(2) php関連:
php/Dockerfile
FROM amazonlinux:2017.03
TimeZoneの設定
RUN cp /usr/share/zoneinfo/Japan /etc/localtime
## yumアップデート
RUN yum -y update
RUN yum remove -y httpd
## httpd-toolsアンインストール
RUN yum remove -y httpd-tools
## apacheのインストール:v2.4.6
RUN yum install -y httpd24
## wgetのインストール
RUN yum install -y wget
## SSHDのインストール
RUN yum -y install openssh-server
## SSHDの設定
RUN sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config && \
sed -ri 's/^UsePAM yes/UsePAM no/' /etc/ssh/sshd_config
## rootユーザーのパスワード変更
RUN echo 'root:12345678' | chpasswd
## ec2-userユーザーの作成
RUN useradd ec2-user
## sudoのインストール
RUN yum -y install sudo
## 外部に公開するポートを指定
EXPOSE 22 80 443
## remiリポジトリの登録
RUN yum install -y epel-release
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
## mysql-clientインストール
RUN yum -y install mysql-community-client
## php7.3インストール
RUN yum remove php-*
RUN yum -y install php73 php73-cli php73-devel php73-common php73-mbstring php73-mysql php73-fpm php73-pdo php73-gd php73-xml php73-mcrypt php73-mysqlnd
## php-xdebugインストール
RUN yum -y install --enablerepo=remi,remi-php73 --disablerepo=amzn-main php-xdebug
## php-xdebugモジュールのphp/7.3/への移動
RUN mv /usr/lib64/php/modules/xdebug.so /usr/lib64/php/7.3/modules
## php.iniファイルのコピー
COPY php.ini /etc/php-7.3.ini
## composerのインストール
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/tmp
RUN mv /tmp/composer.phar /usr/local/bin/composer
## apacheの設定
RUN rm -rf /etc/httpd/conf.d/welcome.conf
RUN cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
RUN sed -ri '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/ AllowOverride None/ AllowOverride All/' /etc/httpd/conf/httpd.conf && \
sed -ri '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/ Options Indexes FollowSymLinks/ Options Indexes FollowSymLinks Includes/' /etc/httpd/conf/httpd.conf && \
sed -ri 's/DirectoryIndex index.html index.html.var/DirectoryIndex index.html index.shtml index.html.var/' /etc/httpd/conf/httpd.conf && \
sed -ri '/DirectoryIndex index.html/s/$/ index.php/g' /etc/httpd/conf/httpd.conf
## make logs dir
RUN mkdir /home/ec2-user/logs
## 起動スクリプトの指定
COPY startup.sh /etc/startup.sh
RUN chmod +x /etc/startup.sh
ENTRYPOINT /etc/startup.sh
php/default.conf
<VirtualHost _default_:80>
DocumentRoot /home/ec2-user/public_html/public
ServerName any
ServerSignature Off
RewriteEngine On
ErrorLog /home/ec2-user/logs/test_error.log
CustomLog /home/ec2-user/logs/test_access.log combined env=!nolog
LogLevel warn
RewriteEngine on
<Directory /home/ec2-user/public_html>
AllowOverride All
Options All -Indexes
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
php/startup.sh
#!/bin/bash
chown ec2-user:ec2-user /home/ec2-user/public_html
chmod 705 /home/ec2-user
/sbin/service crond start
/sbin/service httpd start
/etc/rc.d/init.d/sshd start
while true
do
sleep 10
done
php/php.ini
[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"
[XDebug]
zend_extension="/usr/lib64/php/7.3/modules/xdebug.so"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9000
xdebug.idekey = PHPSTORM
xdebug.remote_log=/tmp/xdebug.log
docker実行
■ Docker Desktop for Mac Edgeをインストール
https://docs.docker.com/docker-for-mac/edge-release-notes/
■ プロジェクトルートに移動
$ cd ec2-sample
■ dockerインストール
$ docker-compose build
■ docker立ち上げ
$ docker-compose up -d
■ dockerに入る
$ docker exec -it docker_sample bash
dockerから DBサーバーに入る
$ mysql -u root -h 172.20.0.2 -p
● DBサーバーに入る
$ docker exec -it docker_mysql bash
サイト表示
macの場合、hostsファイルを編集
$ sudo vi /etc/hosts
ファイルに下記を追記&保存
127.0.0.1 smple.local
アクセスしてみる
サイト:http://sample.local:8888/
DBの中身をみる
■ Sequel Proをインストール
■ 接続設定
ホスト:127.0.0.1
ユーザ名:root
パスワード:password
データベース:db_sample
ポート:4306
ハマった点
php7.3インストールで、 php-pecl-xdebugが入らない。
調べたらどうやらamzn-mainリポジトリにはphp73-pecl-xdebug.x86_64パッケージは存在しないらしい。(2019年08月01日時点で)
php7.3インストールとは別で、php/Dockerfileに下記内容の追加で解決
RUN yum -y install --enablerepo=remi,remi-php73 --disablerepo=amzn-main php-xdebug
RUN mv /usr/lib64/php/modules/xdebug.so /usr/lib64/php/7.3/modules
参考サイト
https://qiita.com/srai0628/items/6b7c6792a3d97adeb35b
https://rythgs.co/archives/2015/02/15/fix-php-xdebug-error/
https://qiita.com/Ikumi/items/8f76d074134b7080d102
https://qiita.com/kazukiii/items/297bd1baa9ea579b7659