【Laravel + MySQL】Dockerで開発環境を作る
Docker、docker-compose.yml を利用して次の開発環境を作成する手順をまとめました。
- WEBサーバー nginx:1.15.6
- データベース mysql 5.7
- アプリケーション php 7.2
- フレームワーク laravel 6.2
構成について
まずプロジェクト全体の構成は以下のようになっています。
.
├── docker
│ ├── mysql
│ │ └── my.cnf
│ ├── php
│ │ └── Dockerfile
│ └── web
│ └── default.conf
├── docker-compose.yml
└── laravel-app
基本的には docker-compose.yml を用いてビルドしますが、PHPのみDockerfileを使います。
※後に記載しますが、pdo_mysqlをコンテナにセットアップしたいためです。(これが分からなくて少し嵌りました。)
各ファイルについての詳細
Docker Compose
docker-compose.yml
docker-compose.yml の内容は以下です。
version: '3'
services:
web:
image: nginx:1.15.6
ports:
- '8000:80'
depends_on:
- app
volumes:
- ./docker/web/default.conf:/etc/nginx/conf.d/default.conf
- .:/var/www/html
app:
build: ./docker/php
volumes:
- .:/var/www/html
depends_on:
- mysql
mysql:
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
environment:
MYSQL_DATABASE: test
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
- ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
volumes:
mysql-data:
- web
nginxの設定、設定ファイルは予め用意したものに置き換えます。 - app
PHPの設定、./docker/php/Dockerfile によりビルドします。 - mysql
データベースの設定、DB名と接続情報を定義します。
マルチバイトを保存できるようにcommandオプションにより文字コードの設定を行います。
その他の各ファイルは以下の通りです。
nginx設定ファイル
./docker/web/default.conf
server {
listen 80;
root /var/www/html/laravel-app/public;
index index.php index.html index.htm;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
PHP
./docker/php/Dockerfile
FROM php:7.2-fpm
# install composer
RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer
RUN apt-get update \
&& apt-get install -y \
git \
zip \
unzip \
vim
RUN apt-get update \
&& apt-get install -y libpq-dev \
&& docker-php-ext-install pdo_mysql pdo_pgsql
WORKDIR /var/www/html
MySQL設定ファイル
./docker/mysql/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
ビルド(コンテナ起動)
ビルド
以下コマンドで初回のみビルド処理が行われ、コンテナが起動します。
$ docker-compose up -d
Starting laravel-project_mysql_1 ... done
Starting laravel-project_app_1 ... done
Starting laravel-project_web_1 ... done
確認
docker ps コマンドにより各コンテナのステータスを確認します。formatオプションにより出力する項目を指定することができます。
$ docker ps --format "table {{.ID}} {{.Image}} {{.Status}} {{.Names}}"
CONTAINER ID IMAGE STATUS NAMES
29a71082b88d mysql:5.7 Up 35 minutes laravel-project_mysql_1
71042526d8fd nginx:1.15.6 Up 35 minutes laravel-project_web_1
f50d6321fa87 laravel-project_app Up 35 minutes laravel-project_app_1
データベース確認
データベースを確認するには一度コンテナに入ってMySQLコマンドで確認します。
コンテナに入るには以下コマンドです。
docker exec -it {コンテナ名} bash
$ docker exec -it laravel-project_mysql_1 bash
root@29a71082b88d:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------+
| Tables_in_test |
+-----------------+
| failed_jobs |
| migrations |
| password_resets |
| users |
+-----------------+
4 rows in set (0.00 sec)
mysql> select id, name from users;
+----+-----------+
| id | name |
+----+-----------+
| 1 | テスト |
+----+-----------+
1 row in set (0.00 sec)
※今回は事前に migration を実行しておいたのですでにテーブルが存在していますが、初期状態では何もありません。
migration に関しては別の機会に触れます。
docker-compose.yml に記載したコマンドにより文字化けが発生していないことが分かります。
動作確認
ブラウザで http://localhost:8000/ を開き、Laravelのウェルカムページが表示されればOKです。
次回は簡単な認証処理を作ってみます。
コメント
コメントを投稿