CakePHPの開発環境をDockerで構築する
はじめまして。 今年4月に入社したkyamashitaです。 主にバックエンド担当になります。 初執筆ということで緊張しておりますが、どうぞよろしくお願いします。
Webフレームワークはいくつか触ったことありますが、 CakePHPは今回が初だったため、とりあえず試せるような環境が欲しいと思い、Dockerで構築しました。
構成
[root@localhost:/docker/cake]
# tree
.
├── build
│ └── cake_php
│ └── Dockerfile
├── config
│ ├── mysql
│ │ └── my.cnf
│ └── nginx
│ └── default.conf
├── data ※ここにCakeのリソースとMySQLのデータを保持しています。
└── docker-compose.yml
各ファイルの内容
/docker/cake/docker-compose.yml
version: '3'
services:
nginx:
image: nginx:1.13.5-alpine
container_name: cake_nginx
ports:
- '8080:80'
volumes:
- ./config/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./data/htdocs:/var/www/html
networks:
vpcbr:
ipv4_address: 172.26.0.2
php:
container_name: cake_php
build: build/cake_php
volumes:
- ./data/htdocs:/var/www/html
networks:
vpcbr:
ipv4_address: 172.26.0.3
mariadb:
image: mariadb:10.2
container_name: cake_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: cake
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
- ./data/mysql:/var/lib/mysql
networks:
vpcbr:
ipv4_address: 172.26.0.4
networks:
vpcbr:
ipam:
driver: default
config:
- subnet: 172.26.0.0/16
/docker/cake/build/cake_php/Dockerfile
FROM php:7-fpm
RUN apt-get update \
&& apt-get install -y git libcurl4-gnutls-dev zlib1g-dev libicu-dev g++ libxml2-dev libpq-dev unzip vim \
&& docker-php-ext-install pdo pdo_mysql intl curl json opcache xml \
&& apt-get autoremove && apt-get autoclean \
&& rm -rf /var/lib/apt/lists/*
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_NO_INTERACTION 1
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& mv composer.phar /usr/local/bin/composer
CMD php-fpm
/docker/cake/config/mysql/my.cnf
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc_messages_dir = /usr/share/mysql
lc_messages = en_US
skip-external-locking
max_connections = 100
connect_timeout = 5
wait_timeout = 600
max_allowed_packet = 16M
thread_cache_size = 128
sort_buffer_size = 4M
bulk_insert_buffer_size = 16M
tmp_table_size = 32M
max_heap_table_size = 32M
myisam_recover_options = BACKUP
key_buffer_size = 128M
table_open_cache = 400
myisam_sort_buffer_size = 512M
concurrent_insert = 2
read_buffer_size = 2M
read_rnd_buffer_size = 1M
query_cache_limit = 128K
query_cache_size = 64M
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 10
expire_logs_days = 10
max_binlog_size = 100M
default_storage_engine = InnoDB
innodb_buffer_pool_size = 256M
innodb_log_buffer_size = 8M
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 400
innodb_flush_method = O_DIRECT
[galera]
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
[isamchk]
key_buffer = 16M
!include /etc/mysql/mariadb.cnf
!includedir /etc/mysql/conf.d/
[client-mariadb]
default-character-set = utf8mb4
[mariadb]
character-set-server = utf8mb4
/docker/cake/config/nginx/default.conf
server {
listen 80;
server_name _;
root /var/www/html/webroot;
index index.php index.html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ ^/.+\.php(/|$) {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
起動
コンテナを起動します。
cd /docker/cake
docker-compose up
ホストから各コンテナへは下記でアクセスします。
docker exec -it cake_php bash
docker exec -it cake_db bash
docker exec -it cake_nginx sh
CakePHPアプリ作成
[host]
docker exec -it cake_php bash
[container]
cd /var/www/html
composer create-project --prefer-dist cakephp/app .
データベースの接続情報を変更
vi /var/www/html/config/app.php
Datasources を変更します。
変更箇所抜粋
'Datasources' => [
'host' => '172.26.0.4', ※docker-compose.ymlで指定したMySQLのアドレス
'username' => 'admin',
'password' => 'admin',
'database' => 'cake',
]
ブラウザに http://ホストのIP:8080 でアクセスすると、 welcomeページが表示され、データベース接続もされていることが確認できます。
やはりDockerは楽で良いですね。