Setup CakePHP 2.x Application with Docker from Scratch

This is a basic setup of CakePHP application (specifically for version 2.x) with Docker. There are a lot more things you can do with Docker. This article is to help you get started.

You may download the final result of this setup in GitHub.

Before we start, let us go through some of the important prerequisites for this setup.


  • Docker (with Docker Compose)
  • Terminal of your choice
  • Text editor of your choice

Getting Started

Let us now grab a fresh copy of CakePHP 2.x from their official Git Repository (https://github.com/cakephp/cakephp/tags).

Note: Make sure to download version 2.x.

You may rename the folder to any name you want. I renamed mine to dockerized-cakephp-app.

Now cd into the directory and move on to creating a Dockerfile.


Let’s first create a docker directory where we’re going to place all Docker container related files.

Inside the docker directory, create a file named Dockerfile. For this application, we’re going to use php:7.2-apache as our base image. It’s an official Docker image maintained by the Docker community. For more details, you may check it here: https://hub.docker.com/r/library/php/

Below will be the content of your Dockerfile: (Note: Make sure to read the comments to understands what’s going on.)

FROM php:7.2-apache RUN apt-get update -yqq \ && apt-get install -yqq --no-install-recommends \ git \ zip \ unzip \ && rm -rf /var/lib/apt/lists # Enable PHP extensions RUN docker-php-ext-install pdo_mysql mysqli # Install composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer # Add cake and composer command to system path ENV PATH="${PATH}:/var/www/html/lib/Cake/Console" ENV PATH="${PATH}:/var/www/html/app/Vendor/bin" # COPY apache site.conf file COPY ./docker/apache/site.conf /etc/apache2/sites-available/000-default.conf # Copy the source code into /var/www/html/ inside the image COPY . . # Set default working directory WORKDIR ./app # Create tmp directory and make it writable by the web server RUN mkdir -p \ tmp/cache/models \ tmp/cache/persistent \ && chown -R :www-data \ tmp \ && chmod -R 770 \ tmp # Enable Apache modules and restart RUN a2enmod rewrite \ && service apache2 restart EXPOSE 80

Apache Site Conf

As you can see in line 21 of your Dockerfile, we would need to create a site.conf file. Create an apache directory inside the docker directory, then create the site.conf file inside it.

<VirtualHost *:80> DocumentRoot /var/www/html/app/webroot/ <Directory /var/www/html/app/webroot/> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>

Let’s move on now to creating the docker-compose.yml file.

Docker Compose

This will be the content of your docker-compose.yml file for now.

version: "3" services: app: build: context: . dockerfile: docker/Dockerfile volumes: - .:/var/www/html ports: - 8000:80


  • We have added a service named app. This will later create a PHP-Apache container where our appliction will be served.
  • Under app, we have the build option. When the build command is executed, this will point the Dockerfile inside the docker directory to be built with the context from the root directory.
  • Volumes is for mounting our application to the container. As a result, you can make changes to your code while developing.
  • We have added port to access our application through port 8000.

Before we can start up the application container, we have to build the image first.

docker-compose build

When the build is done, we can now start up the container with the command below:

docker-compose up -d // you may also add the --build option to build it before starting up the containers docker-compose up -d --build

Now try to open your browser and visit localhost:8000.


As you can see from the result above, there are some notice on CakePHP configuration that we need to set.

For Security.salt and Security.cipherSeed, you just have to open up app/Config/core.php and find both keys and change the value to random value.

Now we need the database configuration. We haven’t created a container for our database yet. So, let’s start from creating the container before configuring our application. Let’s destroy first the container by running the command below:

docker-compose down

Let’s go back to our docker-compose.yml file and make some changes:

version: "3" services: app: build: context: . dockerfile: docker/Dockerfile volumes: - .:/var/www/html ports: - 8000:80 depends_on: - db db: image: mysql:5 volumes: - db-data:/var/lib/mysql environment: MYSQL_DATABASE: cakephp_db MYSQL_ROOT_PASSWORD: root_password volumes: db-data: external: false


  • As you can see, we’ve added a new service called db. We used the official image of MySQL version 5.
  • Volume to keep our data persistent.
  • Environment variables to add to the container.
  • Note that we also added depends_on: db on our app service. The app service will wait for the db service to start first.

Finally, before we restart the containers, let’s configure the database connection in CakePHP application.

Duplicate the file app/Config/database.php.default and remove the extension .default. As a result, you should now have: app/Config/database.php.

Now open the file app/Config/database.php and edit the default database configuration based on the environment variables the we have set in docker-compose.yml.

... public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'db', 'login' => 'root', 'password' => 'root_password', 'database' => 'cakephp_db', 'prefix' => '', //'encoding' => 'utf8', ); ...

After saving the database config, we may now restart the containers.

docker-compose up -d

And that’s it. You now have a running CakePHP 2.x application on Docker with MySQL database.