Laravel Dockerization: The Ultimate Laravel Development Environment with Docker
Introduction
When it comes to creating platform-independent development environments for projects, Docker is the top pick, especially among web developers as it provides tools that automate most of the routine setup. However, even with a perfect Docker setup, there are still a lot of things that should be done by hand, such as setting up correct values for the environment variables, running project-specific commands, populating the database, and so forth. Therefore, I decided to create my own setup aiming to fully automate these processes in an example of a Laravel project, which you can find here.
Prerequisites
Before diving into the setup, make sure you have the following prerequisites:
Structure
The project setup incorporates four essential images: server
, which has apache2
installed and configured along with php8.2
; vite
, given that every Laravel 10 project features preinstalled vite
; mysql
, and redis
. I created separate Dockerfiles for server
and vite
and placed all the Docker-specific files into a new directory called docker
. There are subdirectories for server
and vite
, containing the mentioned Dockerfiles. On top of that, there are .env
and .env.example
files in the directory in order to configure the ports of each of the resulting containers.
Environment Variables
Those who are familiar with Laravel know that the first thing to do when setting up a project for the first time is to create/configure the .env
file in the root directory. Let’s create a bash script that does it for us and keeps both .env
s synchronized:
Artisan Commands
Another thing that requires automation is Artisan commands, such as migrate
, db:seed
, etc. All Artisan commands should be executed after the server
container is up and ready, therefore we can’t put all that code inside the Dockerfile
. Lucky for us, Docker provides the ability to run a bash script right after everything is ready, using the ENTRYPOINT
directive. All we need to do is to place a script containing all the Artisan commands in /usr/local/bin/docker-entrypoint
and instruct the Dockerfile
to execute it:
For my project, I also put some other commands so the resulting docker-entrypoint.sh
script looks like this:
Apart from running the Artisan commands and installing the composer dependencies, the script also makes sure that the server
container is able to access the MySQL database and Redis.
Configuring Vite
Whether you are using Webpack, Vue.js, or any other JavaScript framework within Laravel, you might need certain commands to be executed after the container is up, such as npm install
and npm run build
. We could create a bash script similar to docker-entrypoint.sh
for that purpose too, but since there are not many commands to run for the front-end part, I decided to use the command
directive in the docker-compose.yml
file:
The Dockerfile for vite
is pretty straightforward, we need to install node
and npm
via nvm
:
Introducing Makefile
In order to build the docker environment, we need to make the update.env.sh
script executable, execute it, and then run the actual command docker compose build
. I wanted to convert these commands into one short command, so I ended up using Makefile
. In short, Makefile
gives the ability to declare custom commands and list the actions that should be run subsequently. I created a Makefile
in the root directory with the following commands:
purge env
simply deletes both .env
files in case something went wrong during the run, server
and vite
connect to the appropriate containers.
Running The Docker Environment
It’s time to make use of the Makefile
and run commands. First, run the build command
You can check that the build command also created .env
files. Then run
You can read the output of docker-entrypoint.sh
and if it ran successfully, you should be able to see Laravel's welcome page at http://localhost:8082 and Vite’s main page at http://localhost:5173.
Conclusion
This powerful setup simplifies and automates many aspects of your development process, reducing manual effort and allowing you to focus on building features and writing code. It's worth noting that, while the configuration files presented here are designed to serve as a solid foundation, you are encouraged to modify them as needed to suit the specific needs of your own projects.
References