DockerでCakePHP環境構築(Mac環境)

各端末にDockerソフトがインストールされている前提で進めます。

まだの方は公式ページからどうぞ

また、CakePHPのプロジェクト自体は別途作成されている既存のものを使用します。
手順としては以下の通りです。

  • CakePHPの既存ソースダウンロード
  • Dockerの設定ファイルを追加
  • Dockerのビルド&起動
  • Webブラウザで表示

PHP環境構築

まずは試しに以下のDockerfileを作成してPHP環境を構築

# ベースイメージ
FROM centos:7
# CentOSのキー(各インストール時に必要)
RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

# パッケージ管理コマンドのアップデート
RUN yum -y update

# apacheのインストール
RUN yum -y install httpd

# epelリポジトリをインストール
RUN yum -y install epel-release
# remiリポジトリのインストール
RUN yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# PHPのインストール
RUN yum install --enablerepo=remi,remi-php70 -y php php-common php-mysqlnd php-mbstring php-intl php-mcrypt php-opcache php-xml
# ポート808080でアクセス可能な状態に設定
EXPOSE 8080
# apache起動
RUN systemctl enable http

以下のコマンドを実行します。

docker build -f ./Dockerfile -t centos-test .

以下のようにエラーになりました。

[+] Building 2.8s (4/4) FINISHED                                                                                              docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                          0.0s
 => => transferring dockerfile: 786B                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                   2.6s
 => [auth] library/centos:pull token for registry-1.docker.io                                                                                 0.0s
 => [internal] load .dockerignore                                                                                                             0.0s
 => => transferring context: 2B                                                                                                               0.0s
Dockerfile:19
--------------------
  17 |     RUN yum install --enablerepo=remi,remi-php70 -y php php-common php-mysqlnd php-mbstring php-intl php-mcrypt php-opcache php-xml
  18 |     # ポート808080でアクセス可能な状態に設定
  19 | >>> EXPOSE 808080
  20 |     # apache起動
  21 |     RUN systemctl enable http
--------------------
ERROR: failed to solve: invalid containerPort: 808080
tsunenori.henna@tsunenorihennas-MacBook-Pro docker % vim Dockerfile                               
tsunenori.henna@tsunenorihennas-MacBook-Pro docker % docker build -f ./Dockerfile -t centos-test .
[+] Building 20.1s (7/12)                                                                                                     docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                          0.0s
 => => transferring dockerfile: 784B                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                   2.2s
 => [auth] library/centos:pull token for registry-1.docker.io                                                                                 0.0s
 => [internal] load .dockerignore                                                                                                             0.0s
 => => transferring context: 2B                                                                                                               0.0s
 => [1/8] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4                            15.7s
 => => resolve docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4                             0.0s
 => => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB                                              7.9s
 => => sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 1.20kB / 1.20kB                                                0.0s
 => => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B                                                    0.0s
 => => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB                                                0.0s
 => => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc                                                     7.0s
 => [2/8] RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7                                                                              1.2s
 => ERROR [3/8] RUN yum -y update                                                                                                             0.8s
------                                                                                                                                             
 > [3/8] RUN yum -y update:                                                                                                                        
0.311 Loaded plugins: fastestmirror, ovl                                                                                                           
0.480 Determining fastest mirrors                                                                                                                  
0.740 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
0.740 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
0.748 
0.748 
0.748  One of the configured repositories failed (Unknown),
0.748  and yum doesn't have enough cached data to continue. At this point the only
0.748  safe thing yum can do is fail. There are a few ways to work "fix" this:
0.748 
0.748      1. Contact the upstream for the repository and get them to fix the problem.
0.748 
0.748      2. Reconfigure the baseurl/etc. for the repository, to point to a working
0.748         upstream. This is most often useful if you are using a newer
0.748         distribution release than is supported by the repository (and the
0.748         packages for the previous distribution release still work).
0.748 
0.748      3. Run the command with the repository temporarily disabled
0.748             yum --disablerepo=<repoid> ...
0.748 
0.748      4. Disable the repository permanently, so yum won't use it by default. Yum
0.748         will then just ignore the repository until you permanently enable it
0.748         again or use --enablerepo for temporary usage:
0.748 
0.748             yum-config-manager --disable <repoid>
0.748         or
0.748             subscription-manager repos --disable=<repoid>
0.748 
0.748      5. Configure the failing repository to be skipped, if it is unavailable.
0.748         Note that yum will try to contact the repo. when it runs most commands,
0.748         so will have to try and fail each time (and thus. yum will be be much
0.748         slower). If it is a very temporary problem though, this is often a nice
0.748         compromise:
0.748 
0.748             yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
0.748 
0.748 Cannot find a valid baseurl for repo: base/7/x86_64
------
Dockerfile:7
--------------------
   5 |     
   6 |     # パッケージ管理コマンドのアップデート
   7 | >>> RUN yum -y update
   8 |     
   9 |     # apacheのインストール
--------------------
ERROR: failed to solve: process "/bin/sh -c yum -y update" did not complete successfully: exit code: 1

CentOS 7が2024年6月でサポート終了となってしまったことが原因みたい。(詳細はこちら

rockylinuxってのが代替OSの1つとしてあるらしいのでPHP、apacheをインストール

PHP:https://docs.rockylinux.org/guides/web/php/
apache:https://docs.rockylinux.org/guides/web/apache-sites-enabled/

# ベースイメージ
FROM rockylinux:8.9
# Remiリポジトリのインストール
RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# Remiリポジトリの有効化
RUN dnf config-manager --set-enabled remi
# PHP7.0有効化
RUN dnf -y module enable php:remi-7.4
# PHPの必要なモジュール追加
RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml
# ポート808080でアクセス可能な状態に設定
EXPOSE 8080
# apacheインストール
RUN dnf -y install httpd
# apache起動
RUN systemctl enable --now httpd

こんな感じに書き直して再度buildコマンド実行

% docker build -f ./docker/Dockerfile -t cakephp-test .
[+] Building 2.7s (11/11) FINISHED                                                                                                             docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                           0.0s
 => => transferring dockerfile: 625B                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/rockylinux:8.9                                                                                              2.2s
 => [auth] library/rockylinux:pull token for registry-1.docker.io                                                                                              0.0s
 => [internal] load .dockerignore                                                                                                                              0.0s
 => => transferring context: 2B                                                                                                                                0.0s
 => [1/7] FROM docker.io/library/rockylinux:8.9@sha256:9794037624aaa6212aeada1d28861ef5e0a935adaf93e4ef79837119f2a2d04c                                        0.0s
 => CACHED [2/7] RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm                                                                    0.0s
 => CACHED [3/7] RUN dnf config-manager --set-enabled remi                                                                                                     0.0s
 => CACHED [4/7] RUN dnf -y module enable php:remi-7.4                                                                                                         0.0s
 => CACHED [5/7] RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml                                                           0.0s
 => CACHED [6/7] RUN dnf -y install httpd                                                                                                                      0.0s
 => ERROR [7/7] RUN systemctl enable --now httpd                                                                                                               0.4s
------                                                                                                                                                              
 > [7/7] RUN systemctl enable --now httpd:                                                                                                                          
0.369 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.                                            
0.369 System has not been booted with systemd as init system (PID 1). Can't operate.
0.369 Failed to connect to bus: Host is down
------
Dockerfile:16
--------------------
  14 |     RUN dnf -y install httpd
  15 |     # apache起動
  16 | >>> RUN systemctl enable --now httpd
  17 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c systemctl enable --now httpd" did not complete successfully: exit code: 1

エラーになりました。
調べてみるとdockerではsystemctlコマンドを使わないパターンも多いみたい。

docker起動時に「docker run –privileged」みたいなオプションをつけたらできるみたいな話もあるけど、
ホストマシンの全デバイスとかにアクセスできるようになっちゃってセキュリティ的にまずいらしい。

ってことで、以下のようなDockerfileでとりあえずビルド&起動

# ベースイメージ
FROM rockylinux:8.9

# 初期プロセス設定
# RUN ["/sbin/init"]
# Remiリポジトリのインストール
RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# Remiリポジトリの有効化
RUN dnf config-manager --set-enabled remi
# PHP7.0有効化
RUN dnf -y module enable php:remi-7.4
# PHPの拡張モジュール追加
RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml php-intl
# systemd
RUN dnf install -y systemd
# ポート8080でアクセス可能な状態に設定
EXPOSE 8080
# apacheインストール
RUN dnf -y install httpd
# apache設定ファイルをコンテナ上に格納
COPY ./docker/virtualhost.conf /etc/httpd/conf.d/
# apache起動
RUN ["/sbin/httpd", "-k", "start"]

ビルド

docker build -f docker/Dockerfile -t docker-test .

起動

docker run -it -p 8080:80 docker-test 

ホストからDocker内部にコピーしている「./docker/virtualhost.conf」の中身はこんな感じです。

ServerName localhost
<VirtualHost *:80>
  UseCanonicalName Off
  ServerName          any
  VirtualDocumentRoot /var/www/html/webroot
  DirectoryIndex      index.php index.html
</VirtualHost>

これでとりあえず起動したDockerの中に入り、以下の操作をするとブラウザにテキストが表示できるところまではいけた。

apache起動(Dockerfileに同じ命令を入れてるけど、なぜか動作してないっぽいので中で直接実行)

httpd -k start

「/var/www/html/webroot/index.html」ファイルを以下の内容で作成

test test

ブラウザで表示(http://localhost:8080)

ここまでくれば、あとは「httpd -k start」が動かないところだけの修正とCakePHPの導入です。

CakePHP導入

今回は、既存のCakePHPソースをgitからダウンロードして、
そのファイルをDocker内にマウントして動かそうと思ってます。

ファイルのマウントはdocker-compose.ymlを使用します。

とりあえず以下のような内容でdocker-compose.ymlファイルを作成

version: '3.8'
webserver:
  build: .

以下のコマンドを実行(同じディレクトリ内にDockerfileが存在すること)

docker compose up --build

はい、エラー出ました。

validating /Users/tsunenori.henna/Documents/cdp/00_source/03_cakephp/crmsystem/docker/docker-compose.yml: (root) Additional property webserver is not allowed

docker-compose.ymlの書き方間違ってたみたいです。

version: '3.8'
services:
  webserver:
    build: .

servicesを追加して再度実行

またまたエラー

[+] Running 0/0
[+] Running 0/1erver  Building                                                                                                0.1s 
[+] Building 2.0s (13/15)                                                                                     docker:desktop-linux 
 => [webserver internal] load build definition from Dockerfile                                                                0.0s
 => => transferring dockerfile: 1.39kB                                                                                        0.0s
 => [webserver internal] load metadata for docker.io/library/rockylinux:8.9                                                   1.9s 
 => [webserver auth] library/rockylinux:pull token for registry-1.docker.io                                                   0.0s 
 => [webserver internal] load .dockerignore                                                                                   0.0s
 => => transferring context: 2B                                                                                               0.0s
 => [webserver  1/10] FROM docker.io/library/rockylinux:8.9@sha256:9794037624aaa6212aeada1d28861ef5e0a935adaf93e4ef79837119f  0.0s
 => [webserver internal] load build context                                                                                   0.0s
 => => transferring context: 2B                                                                                               0.0s
 => CACHED [webserver  2/10] RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm                       0.0s
 => CACHED [webserver  3/10] RUN dnf config-manager --set-enabled remi                                                        0.0s
 => CACHED [webserver  4/10] RUN dnf -y module enable php:remi-7.4                                                            0.0s
 => CACHED [webserver  5/10] RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml php-intl     0.0s
 => CACHED [webserver  6/10] RUN dnf install -y systemd                                                                       0.0s
 => CACHED [webserver  7/10] RUN dnf -y install httpd                                                                         0.0s
 => ERROR [webserver  8/10] COPY ./docker/virtualhost.conf /etc/httpd/conf.d/                                                 0.0s
------
[+] Running 0/18/10] COPY ./docker/virtualhost.conf /etc/httpd/conf.d/:
 ⠹ Service webserver  Building                                                                                                2.2s 
failed to solve: failed to compute cache key: failed to calculate checksum of ref af08e3c8-6c64-4285-acdf-8991b0c76b20::w174bl4n1hx6dw99bmq5xc58u: "/docker/virtualhost.conf": not found

Dockerfile内のパスの書き方がまずそうですね
以下のように修正します。

# ベースイメージ
FROM rockylinux:8.9

# 初期プロセス設定
# RUN ["/sbin/init"]
# Remiリポジトリのインストール
RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# Remiリポジトリの有効化
RUN dnf config-manager --set-enabled remi
# PHP7.0有効化
RUN dnf -y module enable php:remi-7.4
# PHPの拡張モジュール追加
RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml php-intl
# systemd
RUN dnf install -y systemd
# ポート8080でアクセス可能な状態に設定
EXPOSE 8080
# apacheインストール
RUN dnf -y install httpd
# apache設定ファイルをコンテナ上に格納
COPY ./virtualhost.conf /etc/httpd/conf.d/
# apache起動
RUN /sbin/httpd -k start

再度実行して成功しました。

[+] Running 0/0
[+] Running 0/1erver  Building                                                                                                0.1s 
[+] Building 0.8s (16/16) FINISHED                                                                            docker:desktop-linux 
 => [webserver internal] load build definition from Dockerfile                                                                0.0s 
 => => transferring dockerfile: 1.38kB                                                                                        0.0s
 => [webserver internal] load metadata for docker.io/library/rockylinux:8.9                                                   0.7s 
 => [webserver internal] load .dockerignore                                                                                   0.0s
 => => transferring context: 2B                                                                                               0.0s
 => [webserver  1/10] FROM docker.io/library/rockylinux:8.9@sha256:9794037624aaa6212aeada1d28861ef5e0a935adaf93e4ef79837119f  0.0s
 => [webserver internal] load build context                                                                                   0.0s
 => => transferring context: 235B                                                                                             0.0s
 => CACHED [webserver  2/10] RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm                       0.0s
 => CACHED [webserver  3/10] RUN dnf config-manager --set-enabled remi                                                        0.0s
 => CACHED [webserver  4/10] RUN dnf -y module enable php:remi-7.4                                                            0.0s
 => CACHED [webserver  5/10] RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml php-intl     0.0s
 => CACHED [webserver  6/10] RUN dnf install -y systemd                                                                       0.0s
 => CACHED [webserver  7/10] RUN dnf -y install httpd                                                                         0.0s
 => CACHED [webserver  8/10] COPY ./virtualhost.conf /etc/httpd/conf.d/                                                       0.0s
 => CACHED [webserver  9/10] RUN /sbin/httpd -k start                                                                         0.0s
 => CACHED [webserver 10/10] WORKDIR /var/www/html                                                                            0.0s
 => [webserver] exporting to image                                                                                            0.0s
 => => exporting layers                                                                                                       0.0s
 => => writing image sha256:d2e5a895973af4c163cfdf91aedae0676fc890323d454e7d15d64e73def41a49                                  0.0s
[+] Running 3/3o docker.io/library/docker-webserver                                                                           0.0s
 ✔ Service webserver             Built                                                                                        1.0s 
 ✔ Network docker_default        Created                                                                                      0.1s 
 ✔ Container docker-webserver-1  Created                                                                                      0.1s 
Attaching to webserver-1
webserver-1 exited with code 0

ただ、動いてるけど処理が終わります。

stdin_open、ttyをtrueにすることでこれは回避できるみたいです。
docker-compose側に記載します。

version: '3.8'
services:
  webserver:
    build: .
    volumes:
      - ../:/var/www/html
    ports:
      - "8080:80"
    tty: true

コマンドを維持し続けるみたいなオプションみたいです。

こちらで再ビルド

docker compose up --build

ビルド自体は成功したのですが、apache起動が相変わらずできてないので以下のように変更

サーバ起動後に実行するシェルスクリプト作成(init.shというファイル名で作成)

#!/bin/sh

# apache起動
httpd -k start 
# composerインストール
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

./composer.phar install

apache起動コマンドと、ついでにCakePHPを動かすためのcomposerもインストールいちゃいます。

Dockerfileは以下のように変更

# ベースイメージ
FROM rockylinux:8.9

# 初期プロセス設定
# RUN ["/sbin/init"]
# Remiリポジトリのインストール
RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# Remiリポジトリの有効化
RUN dnf config-manager --set-enabled remi
# PHP7.0有効化
RUN dnf -y module enable php:remi-7.4
# PHPの拡張モジュール追加
RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml php-intl
# systemd
RUN dnf install -y systemd
# ポート8080でアクセス可能な状態に設定
EXPOSE 8080
# apacheインストール
RUN dnf -y install httpd
# apache設定ファイルをコンテナ上に格納
COPY ./virtualhost.conf /etc/httpd/conf.d/
COPY ../* /var/www/html

# cakephpインストール
WORKDIR /var/www/html
# 初期化スクリプト実行
CMD [ "sh","docker/init.sh"]

再度に「CMD〜」で作成したシェルを実行するコマンドを追加しました。

こちらでビルドして「http://localhost:8080」でアクセスしてみます。

503 「Service Unavaliable」の表示。

各ログを確認してみると、apacheのログに以下のようなエラーが出ていました。
(/var/log/httpd/error_logの内容)

[Tue Jan 14 06:18:16.112198 2025] [proxy:error] [pid 11:tid 140717544441600] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /run/php-fpm/www.sock (*) failed
[Tue Jan 14 06:18:16.112268 2025] [proxy_fcgi:error] [pid 11:tid 140717544441600] [client 172.21.0.1:58496] AH01079: failed to make connection to backend: httpd-UDS

調べたところ、php-fpmというのが原因っぽいです。
詳細は別途調べるとして、「/etc/httpd/conf.modules.d/00-mpm.conf」の設定値を
「mod_mpm_event.so」ではなく「mod_mpm_prefork.so」すると良いらしい。

ってことで、ローカルに上記ファイルをコピー・編集して、
Dockerビルド時にファイルを置き換えるようにします。

ローカルに準備する00-mpm.confは以下の通り

# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines.  See the httpd.conf(5) man
# page for more information on changing the MPM.

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
#
# NOTE: If enabling prefork, the httpd_graceful_shutdown SELinux
# boolean should be enabled, to allow graceful stop/shutdown.
#
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#
#LoadModule mpm_worker_module modules/mod_mpm_worker.so

# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
#LoadModule mpm_event_module modules/mod_mpm_event.so

元々、一番下行の「mod_mpm_event.so」が有効で「mod_mpm_prefork.so」がコメントアウトされてましたが、↑のように変更します。

これで再ビルドするとエラーCakePHPが動作してそうなログが出てきました。

画面は多少変わって500エラー

元々、CakePHPのエラーは出ておりませんでしたが、
CakePHPプロジェクトの<ルート>/logs/配下にエラーログが出てきました。

2025-01-14 17:24:32 Warning: Warning (4096): ini_set(): Cannot set 'user' save handler by ini_set() or session_module_name() in [/var/www/html/vendor/cakephp/cakephp/src/Network/Session.php, line 291]
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
ini_set - [internal], line ??
Cake\Network\Session::options() - CORE/src/Network/Session.php, line 291
Cake\Network\Session::__construct() - CORE/src/Network/Session.php, line 212
Cake\Network\Session::create() - CORE/src/Network/Session.php, line 119
Cake\Http\ServerRequestFactory::fromGlobals() - CORE/src/Http/ServerRequestFactory.php, line 48
Cake\Http\Server::run() - CORE/src/Http/Server.php, line 75
[main] - ROOT/webroot/index.php, line 37



2025-01-14 17:24:32 Warning: Warning (4096): ini_set() [<a href='http://php.net/function.ini-set'>function.ini-set</a>]: Cannot set 'user' save handler by ini_set() or session_module_name() in [/var/www/html/vendor/cakephp/cakephp/src/Network/Session.php, line 291]
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
ini_set - [internal], line ??
Cake\Network\Session::options() - CORE/src/Network/Session.php, line 291
Cake\Network\Session::__construct() - CORE/src/Network/Session.php, line 212
Cake\Network\Session::create() - CORE/src/Network/Session.php, line 119
Cake\Network\Request::createFromGlobals() - CORE/src/Network/Request.php, line 184
Cake\Error\ExceptionRenderer::_getController() - CORE/src/Error/ExceptionRenderer.php, line 118
Cake\Error\ExceptionRenderer::__construct() - CORE/src/Error/ExceptionRenderer.php, line 91
Cake\Error\ErrorHandler::_displayException() - CORE/src/Error/ErrorHandler.php, line 143
Cake\Error\BaseErrorHandler::handleException() - CORE/src/Error/BaseErrorHandler.php, line 180
Cake\Error\BaseErrorHandler::wrapAndHandleException() - CORE/src/Error/BaseErrorHandler.php, line 164
[main] - [internal], line ??



2025-01-14 17:24:32 Error: Fatal Error (256): [RuntimeException] Unable to configure the session, setting session.save_handler failed.
#0 /var/www/html/vendor/cakephp/cakephp/src/Network/Session.php(212): Cake\Network\Session->options()
#1 /var/www/html/vendor/cakephp/cakephp/src/Network/Session.php(119): Cake\Network\Session->__construct()
#2 /var/www/html/vendor/cakephp/cakephp/src/Network/Request.php(184): Cake\Network\Session::create()
#3 /var/www/html/vendor/cakephp/cakephp/src/Error/ExceptionRenderer.php(118): Cake\Network\Request::createFromGlobals()
#4 /var/www/html/vendor/cakephp/cakephp/src/Error/ExceptionRenderer.php(91): Cake\Error\ExceptionRenderer->_getController()
#5 /var/www/html/vendor/cakephp/cakephp/src/Error/ErrorHandler.php(143): Cake\Error\ExceptionRenderer->__construct()
#6 /var/www/html/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php(180): Cake\Error\ErrorHandler->_displayException()
#7 /var/www/html/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php(164): Cake\Error\BaseErrorHandler->handleException()
#8 [internal function]: Cake\Error\BaseErrorHandler->wrapAndHandleException()
#9 {main} in [/var/www/html/vendor/cakephp/cakephp/src/Error/ErrorHandler.php, line 156]

これは、PHPのバグみたいです。バージョンによっては出力されるエラーみたいです。

こちらを参考に、「<プロジェクトルート>/vendor/cakephp/cakephp/src/Network/Session.php」を修正します。

すると、またエラーが変わります

SSLのエラー…ローカルなのでできれば無視してしまいたいが…

簡単に証明書発行できそうなので、証明書をMacに入れてファイルをDockerにコピーで対応してみます。

brew install mkcert
mkcert -install

↑の2つのコマンドを実行

最後に↓

mkcert localhost 127.0.0.1

↓のようなメッセージが表示されればOK

Note: the local CA is not installed in the Firefox trust store.
Run "mkcert -install" for certificates to be trusted automatically ⚠️

Created a new certificate valid for the following names 📜
 - "localhost"
 - "127.0.0.1"

The certificate is at "./localhost+1.pem" and the key at "./localhost+1-key.pem" ✅

It will expire on 14 April 2027 🗓

そうするとカレントディレクトリに「localhost+1-key.pem」「localhost+1.pem」というファイルが生成されています。

これをDocker内部にコピーする必要があるので、
Dockerfileに追加します。

# ベースイメージ
FROM rockylinux:8.9

# 初期プロセス設定
# RUN ["/sbin/init"]
# Remiリポジトリのインストール
RUN dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# Remiリポジトリの有効化
RUN dnf config-manager --set-enabled remi
# PHP7.0有効化
RUN dnf -y module enable php:remi-7.4
# PHPの拡張モジュール追加
RUN dnf -y install php php-curl php-zip php-mbstring php-mysqlnd php-mcrypt php-xml php-intl
# systemd
# RUN dnf install -y systemd
# ポート8080でアクセス可能な状態に設定
EXPOSE 8080
# apacheインストール
RUN dnf -y install httpd
# apache設定ファイルをコンテナ上に格納
COPY ./virtualhost.conf /etc/httpd/conf.d/
COPY ./00-mpm.conf /etc/httpd/conf.modules.d/
COPY ./localhost+1-key.pem /etc/ssl/private/
COPY ./localhost+1.pem /etc/ssl/private/
COPY ../* /var/www/html

# cakephpインストール
WORKDIR /var/www/html
# 初期化スクリプト実行
CMD [ "sh","docker/init.sh"]

https用のポートも空ける必要あるので、docker-compose.ymlも変更します。

version: '3.8'
services:
  webserver:
    build: .
    volumes:
      - ../:/var/www/html
    ports:
      - "8080:80"
      - "8443:443"
    tty: true

「./docker/virtualhost.conf」にも443用の設定追加です。

ServerName localhost
<VirtualHost *:80>
  UseCanonicalName Off
  ServerName          any
  VirtualDocumentRoot /var/www/html/webroot
  DirectoryIndex      index.php index.html
</VirtualHost>

<VirtualHost *:443>
  UseCanonicalName Off
  ServerName          any
  VirtualDocumentRoot /var/www/html/webroot
  DirectoryIndex      index.php index.html
</VirtualHost>

「DockerでCakePHP環境構築(Mac環境)」への1件のフィードバック

コメントする