Introduction
Over the past few years, Docker has become a frequently used solution for deploying applications thanks to how it simplifies running and deploying applications in ephemeral containers. When using a LEMP application stack, for example, with PHP, Nginx, MySQL and the Laravel framework, Docker can significantly streamline the setup process.
Docker Compose has further simplified the development process by allowing developers to define their infrastructure, including application services, networks, and volumes, in a single file. Docker Compose offers an efficient alternative to running multiple docker container create and docker container run commands.
In this tutorial, you will build a web application using the Laravel
framework, with Nginx as the web server and MySQL as the database, all
inside Docker containers. You will define the entire stack configuration
in a docker-compose file, along with configuration files for PHP, MySQL, and Nginx.
Prerequisites
Step 1 — Downloading Laravel and Installing Dependencies
As a first step, we will get the latest version of Laravel and install the dependencies for the project, including Composer, the application-level package manager for PHP. We will install these dependencies with Docker to avoid having to install Composer globally.
First, check that you are in your home directory and clone the latest Laravel release to a directory called laravel-app:
cd ~
git clone https://github.com/laravel/laravel.git laravel-app
Move into the laravel-app directory:
cd ~/laravel-app
Next, use Docker’s composer image to mount the directories that you will need for your Laravel project and avoid the overhead of installing Composer globally:
docker run --rm -v $(pwd):/app composer install
Using the -v and --rm flags with docker run
creates an ephemeral container that will be bind-mounted to your
current directory before being removed. This will copy the contents of
your ~/laravel-app directory to the container and also ensure that the vendor folder Composer creates inside the container is copied to your current directory.
As a final step, set permissions on the project directory so that it is owned by your non-root user:
sudo chown -R $USER:$USER ~/laravel-app
This will be important when you write the Dockerfile for your application image in Step 4, as it will allow you to work with your application code and run processes in your container as a non-root user.
With your application code in place, you can move on to defining your services with Docker Compose.
Step 2 — Creating the Docker Compose File
Building your applications with Docker Compose simplifies the process
of setting up and versioning your infrastructure. To set up our Laravel
application, we will write a docker-compose file that defines our web server, database, and application services.
Open the file:
nano ~/laravel-app/docker-compose.yml
In the docker-compose file, you will define three services: app, webserver, and db. Add the following code to the file, being sure to replace the root password for MYSQL_ROOT_PASSWORD, defined as an environment variable under the db service, with a strong password of your choice: