CakePHP and Docker
We all know that while developing a CakePHP software, we need to have a local environment with PHP, HTTP Server (nginx, apache) and a database (MySql, Postgres, Mongodb, etc). Installing those tools directly to your system is the basic way, but it can become a bit tricky when we have multiple projects using different versions of those tools... that’s where Docker will help us. In this article, we will show a quick docker setup to improve our CakePHP local environment.
If you don’t have docker installed go to: https://docs.docker.com/get-docker/. It is available for Linux, MacOS and Windows.
For our setup we are going to use PHP, Nginx, and Mysql. All of the information required will be added to a new file named docker-compose.yml. In our environment we will need two docker images [https://docs.docker.com/engine/reference/commandline/images/], one image for php + nginx and one for mysql.
Setup Nginx + PHP service
Create the file docker-compose.yml inside your application with this:
version: "3.1"
services:
php-fpm:
image: webdevops/php-nginx:7.4
container_name: myapp-webserver
working_dir: /app
volumes:
- ./:/app
environment:
- WEB_DOCUMENT_ROOT=/app/webroot
ports:
- "80:80"
Now,we have a service named php-fpm, which is able to run php 7.4 and nginx at port 80 pointing to our webroot dir. Important note: the container_name must be unique in your system.
Setup MySql service
Our MySql service requires a username, password and database name. For this, we are going to create the file mysql.env (don’t use a weak password in production, you could share a mysql.env.default file with your team) with this content:
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=my_app
MYSQL_USER=my_user
MYSQL_PASSWORD=password
Now, at the end of docker-compose.yml , add this:
mysql:
image: mysql:5.6
container_name: myapp-mysql
working_dir: /app
volumes:
- .:/app
- ./tmp/data/mysql_db:/var/lib/mysql
env_file:
- mysql.env
command: mysqld --character-set-server=utf8 --init-connect='SET NAMES UTF8;'
ports:
- "3306:3306"
Before we start this service, lets add the service for our database, include this at the end of the file: docker-compose.yml .
You’ll see that we have - ./tmp/data/mysql_db:/var/lib/mysql, this allows us to persist mysql data. Now we also have a service named mysql with one empty database named my_app and a user name my_user.
Starting the services and app configuration
Before we continue, make sure that you don’t have any other http server or mysql server running.
Now that we have finished our docker-compose.yml we can execute docker-compose up to start the services and access the app at http://localhost. The next thing you need to do is update your database configuration with the correct credentials - the host is the service name, in our case it is “mysql”:
'host' => ‘mysql’,
'username' => 'my_user',
'password' => ‘password’,
'database' => 'my_app',
That’s it! Now we have a working local environment for our CakePHP app. We can now access the services using docker-compose exec php-fpm bash and docker-compose exec mysql bash.
The files mentioned here (docker-compose.yml and mysql.env) can be found at https://gist.github.com/CakeDCTeam/263a65336a85baab2667e08c907bfff6.
The icing on the cake
Going one step further, we could add some alias (with linux) to make it even easier. Let’s add these lines at the end of your ~/.bashrc file:
alias cake="docker-compose exec -u $(id -u ${USER}):$(id -g ${USER}) php-fpm bin/cake"
alias fpm="docker-compose exec -u $(id -u ${USER}):$(id -g ${USER}) php-fpm"
alias composer="docker-compose exec -u $(id -u ${USER}):$(id -g ${USER}) php-fpm composer"
With those entries, instead of typing docker-compose exec php-fpm bin/cake, we can just type cake. The other two aliases are for composer and bash. Notice that we have ${USER}? This will ensure that we are using the same user inside the services.
Additional information
Normally docker images allow us to customize the service, for webdevops/php-nginx:7.4 - you can check more information at: https://dockerfile.readthedocs.io/en/latest/content/DockerImages/dockerfiles/php-nginx.html and for mysql check: https://hub.docker.com/_/mysql . You can find more images at: https://hub.docker.com/.
If you are not familiar with docker, take a look at: https://docs.docker.com/get-started/overview/, as this documentation provides good information.
Hope you have enjoyed this article and will take advantage of docker while working in your CakePHP application.
BUT...
It is not like we want to detroy what you have created but...
And we have to report it, it is our job...
It is not like we think
I mean
I know you think
But remmember we are here to help xD
Happy Holidays to ya'll folks!