This repository is no longer maintained/updated, see new repository https://github.com/allysonsilva/laravel-docker
This is a personal collection of Docker tools and images(Nginx, PHP-FPM, MySQL, Redis, MongoDB, Queue, Scheduler, and RabbitMQ) for applications in Laravel.
tree -a -I '.git|.DS_Store' --dirsfirst.
βββ mongodb
β βββ ssl
β β βββ .gitignore
β β βββ ca.pem
β β βββ client.pem
β β βββ server.pem
β βββ mongod.conf
βββ mysql
β βββ ssl
β β βββ .gitignore
β β βββ ca.pem
β β βββ client-cert.pem
β β βββ client-key.pem
β β βββ server-cert.pem
β β βββ server-key.pem
β βββ my.cnf
β βββ mysql.env
βββ nginx
β βββ certs
β β βββ .gitignore
β β βββ fullchain.pem
β β βββ privkey.pem
β βββ configs
β β βββ addon.d
β β β βββ 10-realip.conf
β β βββ nginx.d
β β β βββ 10-headers.conf
β β β βββ 20-gzip-compression.conf
β β β βββ 20-open-file-descriptors.conf
β β β βββ 30-buffers.conf
β β β βββ 40-logs.conf
β β β βββ 50-timeouts.conf
β β β βββ 60-misc.conf
β β β βββ 70-proxy.conf
β β βββ servers
β β β βββ app.conf
β β βββ snippets
β β β βββ cache-static.conf
β β β βββ deny.conf
β β β βββ php-fpm.conf
β β β βββ ssl-certificates.conf
β β β βββ ssl.conf
β β βββ fastcgi.conf
β β βββ mime.types
β β βββ nginx.conf
β βββ Dockerfile
β βββ docker-entrypoint.sh
βββ php
β βββ configs
β β βββ conf.d
β β β βββ opcache.ini
β β β βββ xdebug.ini
β β βββ fpm
β β β βββ php-fpm.conf
β β β βββ www.conf
β β βββ php-local.ini
β β βββ php-production.ini
β βββ queue
β β βββ templates
β β β βββ laravel-horizon.conf.tpl
β β β βββ laravel-worker.conf.tpl
β β βββ supervisord.conf
β βββ scheduler
β β βββ artisan-schedule-run
β βββ vscode
β β βββ launch.json
β βββ Dockerfile
β βββ app.env.example
β βββ docker-entrypoint.sh
β βββ queue.env.example
β βββ scheduler.env.example
βββ redis
β βββ redis.conf
βββ .dockerignore
βββ .editorconfig
βββ .env.example
βββ .gitignore
βββ Makefile
βββ README.md
βββ docker-compose.override.yml
βββ docker-compose.yml
PHP7.3.x- PHP-FPM
Nginx1.16.xMySQL5.7MongoDB4.2-
Redis5.x -
RabbitMQ3.8
dockerfolder must match current repository folder.- The folder name is configurable via the
LOCAL_DOCKER_PATHvariable of the.envenvironment file.
- The folder name is configurable via the
srcfolder should contain Laravel application.- The folder name is configurable via the
LOCAL_APP_PATHvariable of the.envenvironment file.
- The folder name is configurable via the
.
βββ docker
βββ src
$ git clone https://github.com/AllysonSilva/laravel-docker-lite docker && cd docker- Copy the
.env.examplefile in the root of the project to.envand customize. - Uncomment the
pwdvariable and fill it with resultecho $PWD. - The
LOCAL_DOCKER_PATHvariable in the.envfile must be the folder name of the docker project. - The
LOCAL_APP_PATHvariable that is in the.envfile must be the folder name of the Laravel application project.
Customize Running in Development
docker-compose -f docker-compose.yml -f docker-compose.override.yml up app webserver database redis queue schedulerTo build the images it is necessary to configure the
.envfile with the information that will be used when thedocker-compose upcommand is executed.
make config-docker-env \
local_docker_path=./docker \
local_app_path=./src \
app_domain=domain.xyz \
remote_src=/var/www/domain.xyz \
app_image_name=app:1.0 \
webserver_image_name=webserver:1.0-
app_domainmust match the domain that will be used in the application. Used exclusively in the webserver container inserver_nameNginx settings. -
remote_srcmust match the same path in PHP (app) and Nginx (webserver) containers. Because thewebservercontainer sends theindex.phpfile path to PHP-FPM, so if the paths are different in both containers, a project execution error will occur.
- The construction/configuration of this image is used for applications in Laravel.
- Used multi-stage builds in your
Dockerfile.
- In the first stage use the image of Composer to manage the system dependencies (To speed up the download process hirak/prestissimo is used).
- In the second stage use the Node.js image to build the dependencies of the Front-end using the yarn manager.
- In the third and final stage, the results of the previous stages are used to copy the necessary files to the project image.
$APP_ENV: Set the environment where the Laravel application will be configured. This variable can be defined at the moment of image build through arguments(--build-arg APP_ENV=production||local), or if the image is already created, then it can be replaced by the parameter--env "APP_ENV=production||local"when running the container.WORKDIRcorresponds to the variable$REMOTE_SRC.php.ini: Configured at the time of image build and is set according to the value passed to$APP_ENV. If the value of$APP_ENVislocalthenphp.iniwill match the filesphp-local.ini, if the value of$APP_ENVisproductionthen the contents ofphp.iniwill correspond to the filephp-production.ini.- Can be overridden by volume pointing to the
php.inifile path:/usr/local/etc/php/php.ini.
- Can be overridden by volume pointing to the
php-fpm.conf: GLOBAL PHP-FPM configuration found/usr/local/etc/php-fpm.conf.www.conf: Specific configuration for pool[www]found/usr/local/etc/php-fpm.d/www.conf.
Use the following command to build the image:
make build-app \
app_env=production||local \
remote_src=/var/www/domain.xyz \
local_app_path=./src \
local_docker_path=./docker \
app_image_name=app:1.0If you want to customize the image construction according to your arguments, use the docker build command directly:
docker build -t app:1.0 -f ./php/Dockerfile \
--build-arg USER_NAME=app \
--build-arg USER_UID=$UID \
--build-arg USER_GID=$(id -g) \
--build-arg APP_ENV=production||local \
--build-arg REMOTE_SRC=/var/www/domain.xyz \
--build-arg LOCAL_APP_PATH=./app \
--build-arg LOCAL_DOCKER_PATH=./docker \
./../Image will be used as REVERSE PROXY.
Use the following command to build the image:
make build-webserver \
app_domain=domain.xyz \
remote_src=/var/www/domain.xyz \
webserver_image_name=webserver:1.0-
app_domainis used for settingserver_nameinapp.conffile. -
remote_srcmust have the same value used in themake build-appcommand. So there is no conflict in the communication of the services of the reverse proxy (NGINX) with the service CGI (PHP-FPM). -
Use the Let's Encrypt to generate SSL certificates and perform the NGINX SSL configuration to use the HTTPS protocol.
- Folder
nginx/certs/should contain the files created by Let's Encrypt. It should contain the files:cert.pem,chain.pem,dhparam4096.pem,fullchain.pem,privkey.pem.
- Folder
Copy the files from the
phpfolder to the docker root folder(./docker/php/app.env.examplemove to./docker/app.env):app.env.example,queue.env.example,scheduler.env.example, and set to the values that will be used in the environment. Remove the suffix.env.example, leaving only.env.
APP_KEY: If the application key is not set, your user sessions and other encrypted data will not be secure!
Runs the container with the PHP-FPM processes running.
Set the CONTAINER_ROLE environment variable when running the PHP container so that its value is app.
exec /usr/local/sbin/php-fpm -y /usr/local/etc/php-fpm.conf --nodaemonize --force-stderrContainer/service responsible for managing Queue in the application.
Set the CONTAINER_ROLE environment variable when running the PHP container so that its value is queue.
Container with PID 1 executed by supervisor to manage processes. Can have two configurations:
- Many processes running in the Laravel
artisan queue:workfor queue management. - Used for debugging and development, the Horizon is a robust and simplistic queue management panel. A single process in a supervisor configuration by running the
artisan horizoncommand.
exec /usr/bin/supervisord --nodaemon --configuration /etc/supervisor/supervisord.confMandatory environment variables
LARAVEL_QUEUE_MANAGER: This environment variable defines the container context, and can have the following values:- worker: Configure the supervisor to run many processes in the Laravel command
artisan queue:work. - horizon: Configure the supervisor to run a single Horizon process
artisan horizon.
- worker: Configure the supervisor to run many processes in the Laravel command
Container/service responsible for managing Scheduler in the application.
Set the CONTAINER_ROLE environment variable when running the PHP container so that its value is scheduler.
- Container with PID 1 executed by cron.
- Environment variables
APP_KEYandAPP_ENVare required when executing the container. - Environment variables available for PHP processes thanks to the
printenv > /etc/environmentscript in container entrypoint. - Container run as
rootas a cron service request.
Running a single scheduling command
* * * * * {{USER}} /usr/local/bin/php {{REMOTE_SRC}}artisan schedule:run --no-ansi >> /usr/local/var/log/php/cron-laravel-scheduler.log 2>&1exec /usr/sbin/crond -l 2 -f -L /var/log/cron.logSome points to consider:
- MongoDB Container Uses SSL/TLS configuration for database connections. Before running the container you need to create these SSL files, or simply remove this setting.
- MySQL is also configured to accept only SSL connections. You can generate SSL keys through this link https://dev.mysql.com/doc/refman/5.7/en/creating-ssl-files-using-openssl.html.
- The Redis container uses a password(
requirepass) to perform a database connection. - The Nginx container uses SSL certificates to make HTTPS connections only.
Only port 80 and 443 are tight to the internet for security reasons.
Change the passwords present in the files: mysql.env, redis.conf and the docker-compose.yml variables which are: RABBITMQ_USERNAME, RABBITMQ_PASSWORD, MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD.
This project use the following ports:
| Server | Port |
|---|---|
| Nginx | 80 |
| Nginx | 443 |
Set the WEBSERVER_PORT_HTTP environment variable for HTTP connections and WEBSERVER_PORT_HTTPS for HTTPS connections.
mysql -h 127.0.0.1 -P 30062 -uapp -p'ExBkhs^NGuA6r_Fu' \
--ssl-ca=mysql/ssl/ca.pem \
--ssl-cert=mysql/ssl/client-cert.pem \
--ssl-key=mysql/ssl/client-key.pemdocker exec -it redis redis-cli -n 0 -p 6379 -a '.7HVhf(Yh+9CF-58' --no-auth-warningmongo --ssl \
--sslCAFile ./mongodb/ssl/ca.pem --sslPEMKeyFile ./mongodb/ssl/client.pem \
--host 127.0.0.1 --port 29019 -u 'root' -p 'jyA7LF_7dX7.pmH' --authenticationDatabase admindocker exec -it app /bin/bash
# After
$ /usr/local/bin/docker-php-ext-configure xdebug
$ /usr/local/bin/docker-php-ext-install xdebugdocker run --rm -v $(pwd):/app composer require laravel/horizondocker run --rm -v $(pwd):/app composer updatedocker-compose exec -T app ./vendor/bin/phpunit --colors=always --configuration ./appFixing standard code with PSR2
docker-compose exec -T app ./vendor/bin/phpcbf -v --standard=PSR2 ./appAnalyzing source code with PHP Mess Detector
docker-compose exec -T app ./vendor/bin/phpmd ./app text cleancode,codesize,controversial,design,naming,unusedcodeIf you find an issue, or have a special wish not yet fulfilled, please open an issue on GitHub providing as many details as you can (the more you are specific about your problem, the easier it is for us to fix it).
Pull requests are welcome, too π! Also, it would be nice if you could stick to the best practices for writing Dockerfiles.