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
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
Now cd into the directory and move on to creating a
Let’s first create a
docker directory where we’re going to place all Docker container related files.
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.)
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.
Let’s move on now to creating the
This will be the content of your
docker-compose.yml file for now.
- 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.
When the build is done, we can now start up the container with the command below:
Now try to open your browser and visit
As you can see from the result above, there are some notice on CakePHP configuration that we need to set.
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:
Let’s go back to our
docker-compose.yml file and make some changes:
- 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: dbon 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:
Now open the file
app/Config/database.php and edit the default database configuration based on the environment variables the we have set in
After saving the database config, we may now restart the containers.
And that’s it. You now have a running CakePHP 2.x application on Docker with MySQL database.