PHP development with docker
This is a docker-compose temaplte and file structures for Laravel development.
Docker compose template
1 | # docker-compose.yml |
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
82FROM 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
3upload_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
21server {
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
17APP_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