cakephp + Nginx + MySQLの開発環境をDockerで構築

4 min read読了の目安(約4200字

完成形のプロジェクト
https://github.com/asano-yuki/cakephp4-project

1. バージョン情報

  • php:7.4.13-fpm-buster
  • nginx:1.19.6-alpine
  • mysql:5.7
  • phpmyadmin/phpmyadmin:4.9

2. ファイル構成

.
├── app
├── db
├── nginx
    ├── default.conf
├── phpfpm
    ├── Dockerfile
    ├── php.ini
├── docker-compose.yml

3. docker-compose.yml

cakephpとDBのコネクションを常時確保するために、コンテナのIPアドレスを固定化する。

./docker-compose.yml
version: '3'
services:
  nginx:
    image: nginx:1.19.6-alpine
    ports:
      - 8080:80
    networks:
      app_net:
        ipv4_address: 172.30.0.2
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./app:/var/www/html
    depends_on: 
      - phpfpm
  phpfpm:
    build: ./phpfpm
    networks:
      app_net:
        ipv4_address: 172.30.0.3
    volumes:
      - ./app:/var/www/html
    depends_on: 
      - db
  db:
    image: mysql:5.7
    ports:
      - 13306:3306
    networks:
      app_net:
        ipv4_address: 172.30.0.4
    volumes:
      - ./db:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:4.9
    ports:
      - 8888:80
    networks:
      app_net:
        ipv4_address: 172.30.0.5
    depends_on:
      - db
networks:
  app_net:
    name: app_net
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.30.0.0/24

4. default.conf

./nginx/default.conf
server {
  listen 80;
  server_name _;

  root /var/www/html;
  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$is_args$args;
  }

  # アセットの読み込みを確保する
  location ~ ^/(img|css|js|files)/(.+)$ {
    root /var/www/html/webroot;
    try_files /$1/$2 =404;
  }

  location ~ \.php$ {
    fastcgi_pass phpfpm:9000;
    fastcgi_index index.php;    
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

5. Dockerfile

./phpfpm/Dockerfile
FROM php:7.4.13-fpm-buster
COPY php.ini /usr/local/etc/php/
# composerをインストール
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
  && php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { 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
# cakephpを実行するために必要なソフトウェアをインストール
RUN apt-get update \
  && apt-get install -y git libzip-dev zip unzip \
  && docker-php-ext-install zip \
  && docker-php-ext-install pdo_mysql \
  && apt-get install -y libicu-dev \
  && docker-php-ext-configure intl \
  && docker-php-ext-install intl \
  && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev \
  && docker-php-ext-configure gd --with-freetype --with-jpeg \
  && docker-php-ext-install gd
# cakephpをインストール
RUN cd /var/www/html \
  && composer create-project --prefer-dist cakephp/app:4.* app

6. php.ini

./phpfpm/php.ini
date.timezone = "Asia/Tokyo"

7. ビルド・コンテナーの起動

$ docker-compose build
$ docker-compose up -d

8. cakephp4をインストール

$ docker-compose exec phpfpm bash
[ユーザ名]@[ホスト名]:[ディレクトリ名] composer create-project --prefer-dist cakephp/app:4.* .

9. MySQLとコネクションの確立

./app/config/app_local.php
<?php
return [
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => '127.30.0.4',
            'username' => 'root',
            'password' => 'secret',
            'database' => '*****',
            'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
        ],
    ],
];