Skip to main content
CakeDC Blog

A CakePHP Docker Development Environment

We sponsor a monthly CakePHP training session (register here https://training.cakephp.org ) where we cover different topics about the framework. One of our sessions, the "Getting Started with CakePHP 4" is aimed to help developers starting a new project quickly and following the best practices.

 

Our previous "recommended" quick setting for a CakePHP development environment was using a vagrant box. See details here:  https://www.cakedc.com/jorge_gonzalez/2018/01/17/using-a-vagrant-box-as-quick-environment-for-the-getting-started-with-cakephp-training-session. However, we've switched internally to use docker as our primary development environment and also we started using docker in our training sessions.

 

Here's a quick overview of a simple docker based development environment for CakePHP.

 

1. Create a new CakePHP project skeleton using 

composer create-project cakephp/app myproject

 

A new folder "myproject" will be created with a CakePHP project skeleton inside. Go to this new directory and proceed.

 

2. Create a new "docker-compose.yaml" file with the following contents

version: '3'

services:

  mysql8:

    image: mysql:8

    restart: always

    container_name: mysql

    environment:

        MYSQL_ROOT_PASSWORD: root

        MYSQL_DATABASE: my_app

        MYSQL_USER: my_app

        MYSQL_PASSWORD: secret

    volumes:

      - ./:/application

    ports:

      - '9306:3306'

 

  cakephp:

    image: webdevops/php-apache:8.0

    container_name: cakephp

    working_dir: /application/webroot

    volumes:

      - ./:/application

    environment:

      - WEB_DOCUMENT_ROOT=/application/webroot

      - DATABASE_URL=mysql://my_app:secret@mysql/my_app

    ports:

      - "8099:80"


 

3. Run "docker-compose up"

You'll create 2 containers named mysql and cakephp -  check the docker-compose configuration to see default database and users created in the mysql container, and the same environment params passed to the cakephp container via DATABASE_URL to allow the cakephp container to connect with the mysql database.

 

NOTE: the ports exposed are 9306 for mysql and 8099 for cakephp webserver. You can list them using docker-compose ps.

 

4. Access your database and cakephp shell

  • To access the database you can use the command:

mysql --port 9306 -umy_app -psecret my_app

 

To restore a database dump for example, you can use the command:

curl -L https://raw.githubusercontent.com/CakeDC/cakephp4-getting-started-session/master/my_app.sql |mysql --port 9306 -umy_app -psecret my_app

 

You can also configure any database tool to access the database in: localhost:9306

 

  • To access the cakephp environment and shell you can use the command:

docker exec -it --user application cakephp bash

 

You'll go to the webroot folder, so in order to run the cake shell you'll need to:

cd ..

bin/cake 


 

Now you have a working environment to play with the training session contents.

 

In this previous article, we covered another approach to setting up a local docker environment: https://www.cakedc.com/rochamarcelo/2020/07/20/a-quick-cakephp-local-environment-with-docker 

 

We hope to see you in our next training session!

https://training.cakephp.org 

 

Back to all articles
We Bake with CakePHP