Setting up a Laravel Local Environment with Docker

Hanie Asemi
4 min readApr 30, 2022

--

Image by author

Over the past years, Docker has become a popular and frequently used tool for deploying applications. Thank you Docker for simplifying running and deploying applications.

In this paper, we’re going to set up Laravel in the local environment with Docker using LEMP stack, for example, Nginx, MySQL, PHP, and phpMyAdmin. First of all, you need to download and install Docker Desktop in your system. Docker Desktop works with your choice of development tools and languages and gives you access to a vast library of certified images and templates in Docker Hub. We will use Docker Compose to run multi-container Docker applications. All things that we need is a singleYAML file to configure the application’s services and then just by using a single command we can create and start all the services in the configuration file. Here is the structure of the layout that we’ll have.

Image by author

Creating the Docker Compose File

To set up the Laravel project we need to create the docker-compose.yml file in the project’s directory that will define our web server (Nginx), database (MySQL), application (PHP) services, and phpMyAdmin service.

Adding Nginx

In this section, we’re trying to create a container with nginx name under the laravelnetwork that we’ve initialized in the first section.
This container is built from nginx:alpine image and exposed on port :8080of our local machine. Under the depends_onitem, we’re adding php and db containers which mean before running nginx, Docker should initialize the phpand dbcontainers.

what about volumes in nginx service?

  • The local /src folder is bound to /var/wwwin the container.
  • The /nginx/conf.d/default.conffile we created is linked to the /etc/nginx/conf.d/default.conf container file and enables us to modify the nginx web server locally.

Adding MySQL

In this section, we’re creating a mysqlcontainer that is built from themysql image. The default port that MySQL is using is the:3306port on our local machine. In this configuration, we defined one data volume named as ./mysql/data, which is attached to themysqlcontainer mounted on the /var/lib/mysqldirectory. This is the default directory used by MySQL to store all data files.

Note: If you’re using Mac with an ARM architecture processor don’t forget to add platform:linux/x86_64 in your configuration.

Adding phpMyAdmin

Adding PHP

Because of a dependency that Laravel requires we’re going to actually be building our own image off of a local Dockerfile. So we define the context as a phpfolder and define the dockerfile in this directory that Docker will use in the building step. And also we’re defining a volume that the local /srcfolder is bound to /var/www in the container.

Creating the Dockerfile

now it’s time to create the Dockerfile to specify the environment inside individual containers. In this section, we just need to create a Dockerfile for php container. So we should create a Dockerfile in the php directory and add the following code to it. In the Dockerfile we’re specifying that we want our php container to be built from the php:8.0.3-fpmPHP image and installing pdo and pdo_mysql .

php/Dockerfile

Nginx Configuration

Now we should configure Nginx based on the following code that doesn’t need any extra explanation and is used to Nginx configuration with most basic Laravel apps.

nginx/conf.d/default.conf

Create Laravel Project

After configuring our stack it’s time to create and add the Laravel project to this directory. So try to make a new Laravel project with srcname using Composer:composer create-project laravel/laravel src

Starting Docker

Now it’s finally time to start the Docker network. First, navigate to the main project repository and then run docker-compose build to generate defined image data. It can take a few minutes to complete. After finishing the build process it’s time to run the docker-compose with this command docker-compose up -d . If you want to know more about docker-compose CLI check out this link.

Running Your App

After finishing all these steps successfully it’s time to see your Laravel project’s landing page on the browser on the 8080 port that you’ve defined under the nginx service in the Docker compose configuration file.

Using phpMyAdmin User Interface

We use phpMyAdmin to handle the administration of MySQL over the web. so navigate to the http://127.0.0.1:3400 address in your browser and enter the name and the password of the user that you defined in the docker-compose.yml. Here you are, our database is here you can manage tables and their relations via the phpMyAdmin’s user interface.

Image by author

Congratulations! You’ve Done it.

If you don’t want to do all things by yourself you can just clone the below repository and start docker running.

YouTube Video:

--

--

Hanie Asemi
Hanie Asemi

Written by Hanie Asemi

A forward-looking programmer who loves writing and learning

Responses (2)