A set of lightweight Docker images created with Laravel in mind, but work just as fine for other applications. At the basis lies Alpine Linux and the amazing Docker-Webstack which keeps the images that you pull in very small but usable.
The image sets you up with Alpine Linux, PHP-FPM, Nginx. Currently there is an image for different PHP versions available.
In the folder of your application create two files: docker-compose.yml
and Dockerfile
(note that the latter has no extension). On the command line, run docker compose up -d --build
and view your containerized application at localhost:80
.
The docker image contains a healthcheck. You can run the command docker-compose ps
to see if the build was succesful, the state should mention "Up (healthy)" for the app container.
services:
app:
build: .
ports:
- 80:80
volumes:
- .:/www:delegated
FROM jeroeng/alpine-artisan:web8.3
In the end, the best Docker setup is one you create yourself. That being said, this is how you could quickly build upon the Docker image from this repository.
Percona is an (optimized) version of MySQL and very useful as your database server. Add the following to your docker-compose.yml file:
services:
# app container definition
db:
image: percona:5.7
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-secret}
MYSQL_DATABASE: my_database
MYSQL_USER: jeroen
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-secret}
volumes:
- mysql-data:/var/lib/mysql:rw
ports:
- 3306:3306
volumes:
mysql-data: # nothing necessary here, let Docker manage the storage
Your application's .env should contain the following environment variables, after that run docker compose up -d --build
again to have a database container running next to your app.
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=jeroen
DB_PASSWORD=secret
A typical use of redis is for queues (possibly in combination with Laravel Horizon). To have a redis container, add to the docker-compose.yml file:
services:
# app container definition
queue:
image: redis:5-alpine
ports:
- 6379:6379
Adapt your .env file as well, and after another docker compose up -d --build
you will have a redis container running for your application (yes, there is no password in this case). If you want to use Horizon, you would still need to install it in your application.
QUEUE_CONNECTION=redis
REDIS_HOST=queue
REDIS_PASSWORD=
REDIS_PORT=6379
Laravel Horizon requires a daemon in order to work. If you want to have Laravel Horizon running continuously you may start a new container based on the app one. The image name, test_app
is unique for your application, test
is the name of the folder, app
is the name of the app container. After a docker-compose build
this image name should be listed when you run docker images
.
services:
horizon:
image: test_app
command: php artisan horizon
Note: this only runs horizon locally using docker-compose. As soon as you want to host horizon in a container in the cloud, e.g. using kubernetes, you will have to build a separate image.
Clone this repository and run make
to see which commands are there to help you. every command requires a TAG=
parameter, which is the Docker image you want to build, for example: make build TAG=web8.0
.