Help us understand the problem. What is going on with this article?

Docker ComposeでCakePHP環境構築

書いてあること

  • Docker ComposeによるCakePHP環境構築手順の理解・検証を行った際のメモ
  • 【爆速】15分でCakePHP3 実行環境を簡単に構築する by Docker を参考に理解・検証をさせて頂きました。ありがとうございます!
  • 既に稼働中のCakePHPアプリケーションの機能修正等を行うにあたり、自分用(社内向け)に多少アレンジしたものになります

環境

  • macOS Catalina 10.15.3
  • Docker 19.03.5
  • Docker Compose 1.25.4
  • Nginx 1.17.8
  • PHP 7.4.3
  • MySQL 8.0.19
  • phpMyAdmin 5.0.1
  • CakePHP 3.8.10

作成したファイル

下記リポジトリのcakephpディレクトリ配下に保管しています

Docker_public

利用方法

URL

【sample page】http://localhost
【php info】http://localhost/phpinfo.php
【phpMyAdmin】http://localhost:8080
【CakePHP】http://localhost:8765

CakePHPプロジェクトを新規インストール

bash
# Build
$ docker-compose build

# Create and launch container
$ docker-compose up -d

# List containers
$ docker-compose ps

# Connect to mysql container
$ docker-compose exec mysql /bin/bash

# MySQL login
$ mysql -u root -p

# Create a new database
$ create database [db name] character set utf8mb4 collate utf8mb4_ja_0900_as_cs_ks;

# Grant permissions
$ grant all on [db name].* to cakephp@'%';

# MySQL logout
$ exit

# Disconnect from container
$ exit

# Login to phpfpm container
$ docker-compose exec phpfpm /bin/ash

# Get composer
$ curl -s https://getcomposer.org/installer | php

# Create a CakePHP project(Latest version)
php composer.phar create-project --prefer-dist cakephp/app [project name]

# Create a CakePHP project(Specify minor version)
php composer.phar create-project --prefer-dist cakephp/app:3.8.* [project name]

# Create a CakePHP project(Specify maintenance version)
php composer.phar create-project --prefer-dist cakephp/app:3.8.10 [project name]

# Disconnect from container
$ exit

# Stop container
$ docker-compose down

# Modify settings in app.config and docker-compose.yml

# Create and launch container
$ docker-compose up -d

# List containers
$ docker-compose ps

# Open CakePHP in browser

# Stop container
$ docker-compose down

設定ファイルの修正箇所

app.php

item value
App => defaultTimezone Asia/Tokyo
Datasources => default => host mysql
Datasources => default => username cakephp
Datasources => default => password password
Datasources => default => database sample
Datasources => default => encoding utf8mb4
Datasources => default => timezone Asia/Tokyo

docker-compose.yml

item value
services => host => environment => PRJ_NAME [project name]

各種ファイル備忘メモ

docker-compose.yml

利用する際に下記設定を確認・適宜変更

  • 共通
    • Dockerイメージ名、コンテナ名
  • mysql
    • パスワードやDB名など
  • host
    • CakePHPプロジェクト名(環境変数PRJ_NAMEで指定)
docker-compose.yml
version: '3'

# Service definition
services:

  mysql:
    # Dockerfile
    build: ./mysql
    # Image Name
    image: yoshi0518/cakephp-mysql:8.0.19
    # Container Name
    container_name: cakephp-mysql
    # Environment
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: sample
      MYSQL_USER: cakephp
      MYSQL_PASSWORD: password
      TZ: 'Asia/Tokyo'
    # Mount host directory(host:container)
    volumes:
      - ./data/share/mysql:/share
      - ./data/config/mysql/my.cnf:/etc/my.cnf
      - ./data/db:/var/lib/mysql
      - ./data/sql:/docker-entrypoint-initdb.d
    # Port forwarding(host:container)
    ports:
      - 3306:3306
    # Restart
    restart: always

  phpfpm:
    # Dockerfile
    build: ./phpfpm
    # Image Name
    image: yoshi0518/cakephp-phpfpm:7.4.3
    # Container Name
    container_name: cakephp-phpfpm
    # Environment
    environment:
      TZ: 'Asia/Tokyo'
    # Mount host directory(host:container)
    volumes:
      - ./data/share/phpfpm:/share
      - ./data/config/php/php.ini:/usr/local/etc/php/php.ini
      - ./data/httpdocs:/var/www/html
    # Container dependencies
    depends_on:
      - mysql
    # Connect a container to another container
    links:
      - mysql
    # Restart
    restart: always

  nginx:
    # Dockerfile
    build: ./nginx
    # Image Name
    image: yoshi0518/cakephp-nginx:1.17.8
    # Container Name
    container_name: cakephp-nginx
    # Environment
    environment:
      TZ: 'Asia/Tokyo'
    # Mount host directory(host:container)
    volumes:
      - ./data/share/nginx:/share
      - ./data/config/nginx:/etc/nginx
      - ./data/httpdocs:/var/www/html
    # Port forwarding(host:container)
    ports:
      - 80:80
      - 443:443
    # Container dependencies
    depends_on:
      - phpfpm
    # Connect a container to another container
    links:
      - phpfpm
    # Restart
    restart: always

  host:
    # Dockerfile
    build: ./host
    # Image Name
    image: yoshi0518/cakephp-host:7.4.3
    # Container Name
    container_name: cakephp-host
    # Environment
    environment:
      TZ: "Asia/Tokyo"
      PRJ_NAME: "sample"
    # Mount host directory(host:container)
    volumes:
      - ./data/share/host:/share
      - ./data/config/php/php.ini:/usr/local/etc/php/php.ini
      - ./data/httpdocs:/projects
    # Port forwarding(host:container)
    ports:
      - "8765:80"
    # Container dependencies
    depends_on:
      - mysql
      - phpfpm
    # Connect a container to another container
    links:
      - mysql
      - phpfpm
    # Restart
    restart: always

  phpmyadmin:
    # Dockerfile
    build: ./phpmyadmin
    # Image Name
    image: yoshi0518/cakephp-phpmyadmin:5.0.1
    # Container Name
    container_name: cakephp-phpmyadmin
    # Environment
    environment:
      PMA_ARBITRARY: 1
      PMA_HOSTS: cakephp-mysql
      PMA_USER: root
      PMA_PASSWORD: password
    # Mount host directory(host:container)
    volumes:
      - ./data/share/phpmyadmin:/share
    # Port forwarding(host:container)
    ports:
      - 8080:80
    # Container dependencies
    depends_on:
      - mysql
      - phpfpm
      - nginx
    # Connect a container to another container
    links:
      - mysql
      - phpfpm
      - nginx
    # Restart
    restart: always

Dockerfile

  • コンテナ毎にディレクトリを作成し、その配下で保管
  • MySQL、Nginxなどの各アプリケーションのバージョンを変えたい場合はDockerfileで指定している公式イメージを変える

アプリケーションの設定

  • ./data/config配下の各ディレクトリで保管
  • こちらのファイルをマウントして各コンテナに設定を反映

MySQLデータ

  • ./data/dbをマウントしてデータを永続保管
  • ./data/sqlはMySQL起動時に実行されるスクリプト(確認用データのためCakePHPとは直接関係なし)

CakePHPプロジェクト

  • ./data/httpdocsをマウントしてデータを保管
  • phpfpmコンテナでCakePHPプロジェクトをインストールすると、こちらのディレクトリに作成される
yoshi0518
社内SEによる個人メモ。ほぼ自己学習なので間違っていたらすいません。。。
https://www.n-asset.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした