Skeleton for creating a multi-container Docker application based on Laravel framework.
For use in development environment only. Not suitable for use in production environment!.
The stack is created using Docker Compose and consist of the following services:
- Nginx HTTP server.
- PHP-FPM FastCGI process manager.
- PostgreSQL relational database.
- Redis in-memory cache and message broker.
- Containers are based on Alpine Linux which makes them slim and fast.
- Source code and server configuration files are mounted via Docker volumes so changes are reflected immediately, without having to rebuild the containers.
- Database persisted data is also mounted via Docker volume so changes are not lost after rebuilding the containers.
- Dependencies are managed using PHP Composer official Docker image to keep all the tools inside Docker land.
- All services are run by the same UID:GID (
1000:1000
) to prevent permission problems between containers. - Communication between Nginx and PHP-FPM using UNIX sockets instead of TCP for better performance.
- Communication between Redis and PHP using PhpRedis PHP extension for better performance.
- Supervisor is used to automatically run Laravel queue workers.
- Git.
- Docker and Docker Compose.
To prevent permission problems ensure all files and directories are owned by user:group 1000:1000
.
Get the application skeleton
git clone [email protected]:Stolz/laravel-docker-stack.git my-laravel-project && cd my-laravel-project
Install your Laravel flavor
git clone [email protected]:laravel/laravel
docker run --rm -it --user 1000:1000 -v $(pwd)/laravel:/app -v $(pwd)/composer:/tmp composer install
Start the services
docker-compose up
Configure environment following standard Laravel instructions:
cp laravel/.env.example laravel/.env
$EDITOR laravel/.env
To use Postgres database with the default user set these values ...
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=postgres
DB_USERNAME=postgres
DB_PASSWORD=secret
... but since postgres
is a privileged account you should consider creating a dedicated user instead. If you do so, don't forget to also update the service configuration in docker-compose.yml
file.
To use Redis, first you need to edit config/database.php
file to use PhpRedis client ...
'client' => env('REDIS_CLIENT', 'phpredis'),
... and then you can use these values in your .env
file
BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_CLIENT=phpredis
REDIS_HOST=redis
SESSION_DRIVER=redis
You can run Artisan directly from the host. For instance
docker exec -it php ./artisan key:generate
docker exec -it php ./artisan migrate
If you prefer, you can connect to the container and run Artisan from inside it
docker exec -it php ash
./artisan --help
Supervisor is used to automatically run Laravel queue workers. You can customize the configuration by modifying php/supervisord.conf
file.
- Add Nodejs to the stack to compile Laravel assets.