Contents
  1. 1. Docker compose template
    1. 1.1. PHP Stack
    2. 1.2. NGINX
    3. 1.3. MYSQL
    4. 1.4. Laravel environment file

This is a docker-compose temaplte and file structures for Laravel development.

Docker compose template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# docker-compose.yml
version: '3'
services:

#PHP Service
app:
build: ./php
image: homestead/php:7.2
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- LARAVEL_APP_ROOT_PATH:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network

#Redis service
redis:
image: redis
container_name: redis
restart: unless-stopped
tty: true
ports:
- "6379:6379"
networks:
- app-network

#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8888:80"
- "443:443"
volumes:
- LARAVEL_APP_ROOT_PATH:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network

#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_ROOT_PASSWORD: secret
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network

#phpmyadmin
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
tty: true
environment:
- PMA_ARBITRARY=1
- PMA_HOST=db
- PMA_PORT=3306
- PMA_USER=homestead
- PMA_PASSWORD=secret
ports:
- 8000:80
volumes:
- /sessions
networks:
- app-network

#socket.io
socket:
image: node:10.16.0
container_name: app-socket
restart: unless-stopped
tty: true
ports:
- 6001:6001
volumes:
- LARAVEL_APP_ROOT_PATH:/var/www
working_dir: /var/www
command: bash -c "npm install && node /var/www/socket.dev.js"
networks:
- app-network

#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local

PHP Stack

In folder ./php
– ./php/api-queue.conf // API queue worker
– ./php/crontab // Cron job
– ./php/Dockerfile // Docker image build
– ./php/local.ini // The PHP config
– ./php/start.sh // Start up services

  • ./php/api-queue.conf
1
2
3
4
5
6
7
8
9
[program:api-queue]
command=php /var/www/artisan queue:work --tries=1 --memory=384 --timeout=600 --env=local
user=www
numprocs=2
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
startretries=99999
stderr_logfile=/var/log/api-queue.log
  • ./php/crontab
1
2
* * * * * root /usr/local/bin/php  /var/www/artisan schedule:run --env=local

  • ./php/Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
FROM php:7.2-fpm-stretch

# Set working directory
WORKDIR /var/www

# Install dependencies and extensions
RUN buildDeps=" \
default-libmysqlclient-dev \
libbz2-dev \
libmemcached-dev \
libsasl2-dev \
" \
runtimeDeps=" \
build-essential \
mysql-client \
supervisor \
cron \
libpng-dev \
libjpeg62-turbo-dev \
locales \
zip \
jpegoptim \
optipng \
pngquant \
gifsicle \
vim \
unzip \
curl \
git \
libgmp-dev \
re2c \
libmhash-dev \
file \
libfreetype6-dev \
libicu-dev \
libjpeg-dev \
libldap2-dev \
libmcrypt-dev \
libmemcachedutil2 \
libpng-dev \
libxml2-dev \
" \
&& apt-get update && apt-get -y upgrade && apt-get dist-upgrade && DEBIAN_FRONTEND=noninteractive apt-get install -y $buildDeps $runtimeDeps \
&& apt-get install -y libpq-dev \
&& apt-get clean \
&& ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/ \
&& docker-php-ext-configure gmp \
&& docker-php-ext-install zip exif pcntl bcmath bz2 calendar iconv intl mbstring mysqli opcache pdo_mysql gmp pdo_pgsql pgsql soap zip \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \
&& docker-php-ext-install gd \
&& docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
&& docker-php-ext-install ldap \
&& docker-php-ext-install exif \
&& pecl install memcached redis \
&& docker-php-ext-enable memcached.so redis.so \
&& apt-get purge -y --auto-remove $buildDeps \
&& rm -r /var/lib/apt/lists/*

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www \
&& useradd -u 1000 -ms /bin/bash -g www www

# Set the cron job
COPY ./crontab /etc/cron.d/cool-task
RUN chmod 0644 /etc/cron.d/cool-task && crontab /etc/cron.d/cool-task && touch /var/log/cron.log

# Set the supervisord
COPY ./api-queue.conf /etc/supervisor/conf.d/api-queue.conf

# Boost compose install speed
RUN composer global require hirak/prestissimo

COPY ./start.sh /usr/local/bin/start.sh
RUN cat /usr/local/bin/start.sh && chmod +x /usr/local/bin/start.sh

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD service supervisor start && service cron start && php-fpm && cron

  • ./php/local.ini
1
2
3
upload_max_filesize=40M
post_max_size=40M

  • ./php/start.sh
1
2
3
4
5
6
7
8
9
10
#!/bin/bash

chmod 0644 /etc/cron.d/cool-task
crontab /etc/cron.d/cool-task
touch /var/log/cron.log

service supervisor start
service cron start
php-fpm
cron

NGINX

./nginx/conf.d/app.conf

  • ./nginx/conf.d/app.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}

MYSQL

  • ./mysql/my.cnf
1
2
3
4
[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log

Laravel environment file

  • ./api_development_env_files/.env
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
APP_ENV=local
APP_KEY=abcdf
APP_URL=http://laravel.api
DB_HOST=db
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
PUSH_ENV=development
CACHE_DRIVER=redis
SESSION_DRIVER=file
BROADCAST_DRIVER=redis
REDIS_HOST=redis
JWT_SECRET=JWT_SECRET
JWT_TTL=262800
JWT_TTL_REFRESH=262800
ALLOW_ORIGIN=*
QUEUE_DRIVER=sync #sqs in production, but sync for staging
Contents
  1. 1. Docker compose template
    1. 1.1. PHP Stack
    2. 1.2. NGINX
    3. 1.3. MYSQL
    4. 1.4. Laravel environment file