PHP with extensions required for CakePHP 4, including Composer, the PHP dependency manager.
This image may be used directly for development or as a base for deploying an existing application. In both cases it is assumed a MySQL server is needed by the application.
This section describes how to maunally create a development container. For a simpler docker-compose deployment for an existing CakePHP project, see later in this guide.
In order for the CakePHP container to be able to resolve the MySQL container by hostname, both containers need to be on the same Docker network.
docker network create my-app
Create a MySQL container with a random root password and a database/username/password with the name my-app-mysql
for the application:
docker run -d \
--network my-app \
--name my-app-mysql \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes \
-e MYSQL_DATABASE=cake \
-e MYSQL_USER=cake \
-e MYSQL_PASSWORD=cake \
mysql:5.7.31
The -d
flag will start the container in detached mode and logs will not be displayed in the terminal, though are still available with:
docker logs my-app-mysql
On the host machine, create a project directory, for example project
.
mkdir project
Mount the new project directory on the host machine as the /var/www/html
directory in a new container from this image (attached to the network created previously), publishing port 80
in the container to the arbitrary port 2531
on the host:
docker run -it \
--network my-app \
--name my-app-cakephp \
--mount type=bind,src=`pwd`/project,dst=/var/www/html \
-p 2531:80 \
-e DEBUG=true \
-e SECURITY_SALT=abc123 \
-e DATABASE_URL="mysql://cake:cake@my-app-mysql/cake?encoding=utf8mb4&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false" \
voquis/cakephp:8.1.20-apache-buster \
bash
Note that environment variables are used to reference the MySQL database. For a list of all CakePHP environment variables, see the configuration section of the docs.
Create a new cakephp project with:
composer create-project --prefer-dist cakephp/app .
Note the .
at the end, if ommitted, the new CakePHP app will be created in a new sub-directory title app
and apache paths will not work. If asked to Set Folder Permissions?
, reply Y
to allow apache write access to the tmp
and log
directories.
Start the apache service with:
apachectl start
This step is necessary because apache is configured with a webroot of /var/www/html/webroot
which does not exist when the container starts (causing the apache process to exit) but is created after the CakePHP project is.
The application will be available at http://127.0.0.1:2531 on the host machine.
Create a Dockerfile
in the root of your existing CakePHP directory with the following content:
FROM voquis/cakephp:8.1.20-apache-buster
# Copy application and config files
COPY . .
# Install production dependencies only
RUN composer install --no-dev
Build a new image using this dockerfile with:
docker build -t my/company/name:tag .
Run the newly built container with:
docker run -it \
--network my-app \
--name my-company-app-cakephp \
-p 2532:80 \
-e DEBUG=false \
-e SECURITY_SALT=abc123 \
-e DATABASE_URL="mysql://cake:cake@my-app-mysql/cake?encoding=utf8mb4&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false" \
my/company/name:tag \
bash
Docker compose is a convenient way of starting multiple related containers.
It will create a new network for the related containers.
Create a docker-compose.yaml
file in the root of your project with the following content.
Note the similarity in arguments between starting the containers manually and the codified version in the docker-compose.yaml
file.
Some additional options for the mysql database are added to assist with debugging.
Note also that the database port (3306
) is published as 2533
for connecting from the host machine with a MySQL client for database administration.
version: "3.7"
services:
# MySQL database container
my-app-mysql:
image: "mysql:5.7.31"
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_DATABASE: "cake"
MYSQL_USER: "cake"
MYSQL_PASSWORD: "cake"
ports:
- "2533:3306"
command: [
"--general_log=1",
"--general_log_file=/var/log/mysql/general.log",
"--slow_query_log=1"
]
# CakePHP development container
my-app-cakephp:
image: "voquis/cakephp:8.1.20-apache-buster"
environment:
DATABASE_URL: "mysql://cake:cake@my-app-mysql/cake?encoding=utf8mb4&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false"
DEBUG: "false"
SECURITY_SALT: "abc123"
volumes:
- type: "bind"
source: "./"
target: "/var/www/html"
ports:
- "2532:80"
Start the containers with:
docker-compose up
The application will be available at http://127.0.0.1:2532 on the host machine.
To quite the stack but leave the containers, use Ctrl+c
. The next time you need the containers, use docker-compose up
again.
To destroy the containers, use docker-compose down
.