A base image for running Laravel apps on Docker.
This image allows to run Laravel applications using:
- Nginx
- php-fpm
- supervisord (as parent process for both Nginx and php-fpm)
It offers a sensible configuration for running Laravel in both development and production environments.
In your Laravel project create a Dockerfile
with the following content:
FROM loige/laravel-nginx
# copy your sources in /app within the container
COPY . .
# Install PHP dependencies
RUN composer install --optimize-autoloader --no-dev
RUN chown -R www-data:www-data /app
# Install JavaScript/Node.js dependencies with NPM, compile assets with Mix
RUN yarn && yarn run production
The image can be configured at build time using build args and at runtime using environment variables
platform
(defaultlinux/amd64
): can be used to build the image for different platforms.
RUN_MIGRATIONS
: if set it will run the migrations at runtime.INSTALL_DEV_DEPS_AT_RUNTIME
: if set set then allows dev dependencies (e.g. debug bar) and runyarn run dev
If more advanced configuration is needed, all configuration files can be overridden with explicit COPY
commands. You could copy the original files from this repo into your own project, change them and then override the original file by adding a COPY
command to override the file in your container.
This is where the original configuration files are copied:
File location in repo | File location in container | Notes |
---|---|---|
./config/fastcgi-php.conf |
/etc/nginx/fastcgi-php.conf |
|
./config/nginx.conf |
/etc/nginx/nginx.conf |
|
./config/php-fpm.conf |
/usr/local/etc/php/php-fpm.conf |
|
./config/php-fpm.conf |
/usr/local/etc/php/php.ini |
|
./config/start.sh |
/usr/bin/start.sh |
Needs to be executable |
./config/nginx-healthcheck.sh |
/usr/bin/nginx-healthcheck.sh |
Needs to be executable |
./config/supervisord.ini |
/etc/supervisor.d/supervisord.ini |
A sample index.php
file is automatically created in /app/public/index.php
.
This allows to easily test the container locally without having to setup a full Laravel application.
One approach you could be using for testing configuration changes is the following:
docker build . -t laravel-nginx-local
docker run -it laravel-nginx-local bash
Inside the bash prompt within the container:
supervisord -c /etc/supervisor.d/supervisord.ini -l /var/log/supervisord.log -j /var/run/supervisord.pid;
This should already show if you are able to start php-fpm and nginx correctly.
In another terminal you can get the container id with:
docker ps
Then you can run another bash terminal inside the same container with:
docker exec -it <container_id> bash
In this new shell you could for instance run:
curl -vvv localhost
To see if nginx is able to connect to php-fpm and render the sample index.php
page.
Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.
Licensed under MIT License. © Luciano Mammino