|
1 | 1 | # cakephp-docker
|
2 |
| -A Docker Compose setup for CakePHP Applications |
| 2 | +A Docker Compose setup for containerized CakePHP Applications |
| 3 | + |
| 4 | +This setup spools up the following containers |
| 5 | + |
| 6 | +* **mysql** (5.7) |
| 7 | +* **nginx** |
| 8 | +* **php-fpm** (php 7.1) |
| 9 | +* **mailhog** (smtp server for testing) |
| 10 | + |
| 11 | +The guide will walk you thru the following things |
| 12 | + |
| 13 | +* [Installation](#installation) |
| 14 | +* [Now, how to use `bin/cake` or `mysql`](#now-how-to-use-bin--cake-or-mysql) |
| 15 | +* [OK, so what did the defaults set up?](#ok-so-what-did-the-defaults-set-up) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Clone this repo (or just download the zip) and put it in a `docker` folder inside your root app folder |
| 20 | + |
| 21 | +Here is an example of what my typical setup looks like |
| 22 | + |
| 23 | +``` |
| 24 | + myapp-folder |
| 25 | + cakephp |
| 26 | + src |
| 27 | + config |
| 28 | + .. |
| 29 | + docker |
| 30 | + docker-compose.yml |
| 31 | + nginx |
| 32 | + nginx.conf |
| 33 | + php-fpm |
| 34 | + Dockerfile |
| 35 | + php-ini-overrides.ini |
| 36 | +``` |
| 37 | + |
| 38 | +Then, **Find/Replace** `myapp` with the name of your app. |
| 39 | + |
| 40 | +> **WHY?** by default the files are set to name the containers based on your app prefix. By default this is `myapp`. |
| 41 | +> A find/replace on `myapp` is safe and will allow you to customize the names of the containers |
| 42 | +> |
| 43 | +> e.g. myapp-mysql, myapp-php-fpm, myapp-mysql, myapp-mailhog |
| 44 | +
|
| 45 | +**Build and Run your Containers** |
| 46 | + |
| 47 | +```bash |
| 48 | +cd /path/to/your/app/docker |
| 49 | +docker-compose -up |
| 50 | +``` |
| 51 | + |
| 52 | +That's it. You can now access your CakePHP app at |
| 53 | + |
| 54 | +`localhost:8180` |
| 55 | + |
| 56 | +**Connecting to your database** |
| 57 | + |
| 58 | +Also by default the first time you run the app it will create a `MySQL` database with the following credentials |
| 59 | + |
| 60 | +``` yaml |
| 61 | +host : myapp-mysql |
| 62 | +username : myapp |
| 63 | +password : myapp |
| 64 | +database : myapp |
| 65 | +``` |
| 66 | +You can access your MySQL database (with your favorite GUI app) on |
| 67 | +
|
| 68 | +`localhost:8106` |
| 69 | + |
| 70 | +Your `app/config.php` file should be set to the following (it connects through the docker link) |
| 71 | + |
| 72 | +```php |
| 73 | + 'Datasources' => [ |
| 74 | + 'default' => [ |
| 75 | + 'host' => 'myapp-mysql', |
| 76 | + 'port' => '3306', |
| 77 | + 'username' => 'myapp', |
| 78 | + 'password' => 'myapp', |
| 79 | + 'database' => 'myapp', |
| 80 | + ], |
| 81 | +``` |
| 82 | + |
| 83 | +## Now, how to run `bin/cake` and `mysql` |
| 84 | + |
| 85 | +Now that you're running stuff in containers you need to access the code a little differently |
| 86 | + |
| 87 | +You can run things like `composer` on your host, but if you want to run `bin/cake` or use MySQL from commandline you just need to connect into the container first |
| 88 | + |
| 89 | +**access your php server** |
| 90 | + |
| 91 | +```bash |
| 92 | +docker exec -it myapp-php-fpm /bin/bash |
| 93 | +``` |
| 94 | +> remember to replace `myapp` with whatever you really named the container |
| 95 | + |
| 96 | +**access mysql cli** |
| 97 | + |
| 98 | +```bash |
| 99 | +docker exec -it myapp-mysql /usr/bin/mysql -u root -p myapp |
| 100 | +``` |
| 101 | +> remember to replace `myapp` with whatever you really named the container and with your actual database name and user login |
| 102 | + |
| 103 | + |
| 104 | +## OK, so what did the defaults set up? |
| 105 | + |
| 106 | +There are 4 containers that I use all the time that will be spooled up automatically |
| 107 | + |
| 108 | +### `myapp-nginx` - the web server |
| 109 | + |
| 110 | +First we're creating an nginx server. The configuration is set based on the CakePHP suggestions for nginx and `myapp-nginx` will handle all the incoming requests from the client and forward them to the `myapp-php-fpm` server which is what actually runs your PHP code. |
| 111 | + |
| 112 | + |
| 113 | +### `myapp-php-fpm` - the PHP processor |
| 114 | + |
| 115 | +This container runs `php` (and it's extensions) needed for your CakePHP app |
| 116 | + |
| 117 | +It automatically includes the following extensions |
| 118 | + |
| 119 | +* `php7.1-intl` (required for CakePHP 3.x +) |
| 120 | +* `php7.1-mbstring` |
| 121 | +* `php7.1-sqlite3` (required for DebugKit) |
| 122 | +* `php7.1-mysql` |
| 123 | + |
| 124 | +It also includes some php ini overrides (see `php-fpm\php-ini-overrides.ini`) |
| 125 | + |
| 126 | +This container will (by default) look for your web app code in `../cakephp/` (relative to the `docker-compose` file). |
| 127 | + |
| 128 | +### `myapp-mysql` - the database server |
| 129 | + |
| 130 | +The first time you run the docker containers it will create a folder in your root structure called `mysql` and this is where it will store all your database data. |
| 131 | + |
| 132 | +Since the data is stored on your host device you can bring the mysql container up and down or completely destroy and rebuild it without ever actually touching your data - it is "safely" stored on the host |
| 133 | + |
| 134 | +### `myapp-mailhog` - the smtp server |
| 135 | + |
| 136 | +This is just a built-in mail server you can use to 'send' and intercept mail coming from your application. |
| 137 | + |
| 138 | +Set up your `app/config.php` with the following |
| 139 | + |
| 140 | +```php |
| 141 | + 'EmailTransport' => [ |
| 142 | + ... |
| 143 | + 'mailhog' => [ |
| 144 | + # These are default settings for the MailHog container - make sure it's running first |
| 145 | + 'className' => 'Smtp', |
| 146 | + 'host' => 'myapp-mailhog', |
| 147 | + 'port' => 1025, |
| 148 | + 'timeout' => 30, |
| 149 | + ], |
| 150 | + ... |
| 151 | +``` |
| 152 | + |
| 153 | +You can access the **Web GUI** (using the defaults) for mailhog at |
| 154 | + |
| 155 | +`localhost:8125` |
| 156 | + |
| 157 | +To send mail over the transport layer just set your `Email::transport('mailhog')` |
| 158 | + |
| 159 | + |
0 commit comments