概要
Dockerを使ってApache+PHP+MySQLの環境を構築してみたので手順を記載して行く。
ディレクトリ構成
ディレクトリ
|_docker-compose.yaml
|_Dockerfile
|_config
| |_mysql
| | |_my.cnf
| | |_my.conf
| |_php
| |_php.ini
|_html
|_index.php
|_connect.php
手順
各種ファイルの作成をする。
docker-compose.yamlの作成
version: '3.7'
services:
web:
build: .
container_name: "php74-1"
volumes:
- "./html/:/var/www/html"
privileged: true
ports:
- "80:80"
depends_on:
- mysql
mysql:
image: mysql:8
volumes:
- ./mysql:/var/lib/mysql
- ./config/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
- MYSQL_USER=test_user
- MYSQL_PASSWORD=test_password
ports:
- "4306:3306"
container_name: mysql8-1
Dockerfileの作成
FROM php:7.4.8-apache
RUN apt-get update && apt-get install -y \
libonig-dev \
&& docker-php-ext-install pdo_mysql mysqli
COPY ./config/php/php.ini /usr/local/etc/php/
configファイルの作成
my.confの作成
config/mysql/my.cnfフォルダ配下に作成
[mysqld]
default_autentication_plugin=mysql_native_password
php.iniの作成
[Date]
date.timezone = "Asia/Tokyo"
phpファイルの作成
index.phpの作成
phpinfoを表示するように記述する。
<?php
phpinfo();
connect.phpの作成
DBとの接続確認用のコードを記述する。
<?php
try {
$dsn = 'mysql:host=mysql;dbname=test_db;charset=utf8';
$db = new PDO($dsn, 'test_user', 'test_password');
$sql = 'SELECT version();';
$contact = $db->prepare($sql);
$contact->execute();
$result = $contact->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
} catch (PDOException $e) {
echo $e->getMessage();
exit;
}
実行
docker-compose up -d
コマンドで起動する。
- 実行時の出力例
$ docker-compose up -d
[+] Running 13/13
⠿ mysql Pulled 17.0s
⠿ 72a69066d2fe Already exists 0.0s
⠿ 93619dbc5b36 Already exists 0.0s
⠿ 99da31dd6142 Already exists 0.0s
⠿ 626033c43d70 Already exists 0.0s
⠿ 37d5d7efb64e Already exists 0.0s
⠿ ac563158d721 Already exists 0.0s
⠿ d2ba16033dad Already exists 0.0s
⠿ 688ba7d5c01a Pull complete 1.0s
⠿ 00e060b6d11d Pull complete 12.8s
⠿ 1c04857f594f Pull complete 12.9s
⠿ 4d7cfa90e6ea Pull complete 12.9s
⠿ e0431212d27d Pull complete 13.0s
[+] Building 2.7s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/php:7.4.8-apache 1.8s
=> [1/3] FROM docker.io/library/php:7.4.8-apache@sha256:70b441b3613f49c26eafe231b678ffbd376161b06c7904de8273b8189a039f89 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 225B 0.0s
=> CACHED [2/3] RUN apt-get update && apt-get install -y libonig-dev && docker-php-ext-install pdo_mysql mysqli 0.0s
=> [3/3] COPY ./config/php/php.ini /usr/local/etc/php/ 0.6s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:0f7f0ac23605b5e69ab4e67fdd6e91069899cba69eb985f258649183a60086c6 0.0s
=> => naming to docker.io/library/apache_php74_mysql8_web 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/3
⠿ Network apache_php74_mysql8_default Created 0.1s
⠿ Container mysql8-1 Started 0.8s
⠿ Container php74-1 Started
- 実行後のコンテナ状態
$ docker-compose ps
NAME COMMAND SERVICE STATUS PORTS
mysql8-1 "docker-entrypoint.s…" mysql running 0.0.0.0:4306->3306/tcp
php74-1 "docker-php-entrypoi…" web running 0.0.0.0:80->80/tcp
確認
- phpinfoの確認
http://localhost
を実行
- DB接続確認
http://localhost/connect.php
を実行
環境の構築ができたので完了とする。