Skip to content

Commit d89e61b

Browse files
committed
(feat) initial checking from local
1 parent e5cf825 commit d89e61b

File tree

5 files changed

+250
-1
lines changed

5 files changed

+250
-1
lines changed

README.md

+158-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,159 @@
11
# 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+

docker-compose.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
###############################################################################
2+
# Generated on phpdocker.io #
3+
###############################################################################
4+
5+
myapp-mailhog:
6+
image: phpdockerio/mailhog:latest
7+
container_name: myapp-mailhog
8+
ports:
9+
- "8125:8025"
10+
11+
myapp-mysql:
12+
image: mysql:5.7
13+
container_name: myapp-mysql
14+
volumes:
15+
- ../mysql:/var/lib/mysql
16+
ports:
17+
- "8106:3306"
18+
environment:
19+
- MYSQL_ROOT_PASSWORD=root
20+
- MYSQL_DATABASE=myapp
21+
- MYSQL_USER=myapp
22+
- MYSQL_PASSWORD=myapp
23+
24+
myapp-webserver:
25+
image: phpdockerio/nginx:latest
26+
container_name: myapp-webserver
27+
volumes:
28+
- ../cakephp:/var/www/myapp
29+
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
30+
ports:
31+
- "8180:80"
32+
links:
33+
- myapp-php-fpm
34+
35+
myapp-php-fpm:
36+
build: .
37+
dockerfile: php-fpm/Dockerfile
38+
container_name: myapp-php-fpm
39+
volumes:
40+
- ../cakephp:/var/www/myapp
41+
- ./php-fpm/php-ini-overrides.ini:/etc/php/7.1/fpm/conf.d/99-overrides.ini
42+
links:
43+
- myapp-mailhog
44+
- myapp-mysql

nginx/nginx.conf

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# server {
2+
# listen 80;
3+
# server_name www.myapp.com;
4+
# rewrite ^(.*) http://myapp.com$1 permanent;
5+
# }
6+
7+
server {
8+
listen 80;
9+
server_name myapp.com;
10+
11+
client_max_body_size 108M;
12+
13+
access_log /var/www/myapp/logs/access.log;
14+
error_log /var/www/myapp/logs/error.log;
15+
16+
root /var/www/myapp/webroot/;
17+
index index.php;
18+
19+
# if (!-e $request_filename) {
20+
# rewrite ^.*$ /index.php last;
21+
# }
22+
23+
location / {
24+
try_files $uri /index.php?$args;
25+
}
26+
27+
location ~ \.php$ {
28+
fastcgi_pass myapp-php-fpm:9000;
29+
fastcgi_index index.php;
30+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
31+
# fastcgi_param PHP_VALUE "error_log=/var/www/myapp/logs/php_errors.log";
32+
fastcgi_buffers 16 16k;
33+
fastcgi_buffer_size 32k;
34+
include fastcgi_params;
35+
}
36+
37+
}

php-fpm/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM phpdockerio/php71-fpm:latest
2+
3+
# Install selected extensions and other stuff
4+
RUN apt-get update \
5+
&& apt-get -y --no-install-recommends install php7.1-mysql php7.1-intl php7.1-mbstring php7.1-sqlite3\
6+
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
7+
8+
9+
WORKDIR "/var/www/myapp"

php-fpm/php-ini-overrides.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
upload_max_filesize = 100M
2+
post_max_size = 108M

0 commit comments

Comments
 (0)