Setting Up a Laravel 11 Development Environment with Docker

Vitalii Shloda
4 min readJul 7, 2023

--

Updated June 2024.

Access the Full Project

For the full project, feel free to visit GitHub repository.

With the growing trend of containerization in web development, Docker has become an indispensable tool for developers. Docker allows for seamless management of dependencies and environments, enabling developers to focus on coding. In this article, we’ll guide you through setting up a Laravel 10 development environment using Docker.

Prerequisites

Before starting, make sure the following are installed on your system:

  • Docker: Visit Docker’s official website to download and install Docker Desktop.
  • Docker Compose: Usually included with Docker Desktop, install separately if it’s not.

Project Structure

Step 1: Setup PHP Docker Environment

In the docker folder of your project, create a file named php.Dockerfile. This file defines the configuration for our Docker container. Add the following content to your php.Dockerfile:

FROM php:8.2-fpm-alpine

ADD ./docker/php/www.conf /usr/local/etc/php-fpm.d/www.conf

RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel

RUN mkdir -p /var/www/html

ADD ./src/ /var/www/html

RUN docker-php-ext-install pdo pdo_mysql

RUN chown -R laravel:laravel /var/www/html

This Dockerfile creates a Docker container with PHP 8.2 and all the necessary extensions to run a Laravel project.

Create file www.conf based in php folder and add following content

[www]

user = laravel
group = laravel

listen = 127.0.0.1:9000

pm = dynamic

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Step 2: Create a Docker Compose File

In the root directory of your Laravel project, create a docker-compose.yml file. This file allows us to define and manage multi-container Docker applications. Insert the following content:

services:

nginx:
build:
context: .
dockerfile: ./docker/nginx.Dockerfile
depends_on:
- php
- mysql
container_name: laravel_nginx
ports:
- 80:80
- 443:443
volumes:
- ./src:/var/www/html

php:
build:
context: .
dockerfile: ./docker/php.Dockerfile
container_name: laravel_php
volumes:
- ./src:/var/www/html

mysql:
image: mysql:8.0.27
platform: linux/amd64
container_name: laravel_mysql
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: laraveldb
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret

composer:
image: composer:latest
container_name: laravel_composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html

artisan:
build:
context: .
dockerfile: ./docker/php.Dockerfile
container_name: laravel_artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['php', 'artisan']

npm:
image: node:current-alpine
container_name: laravel_npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']

This docker-compose.yml file creates two services: one for PHP and one for Nginx. It also defines a Docker network that the two services use to communicate.

Step 3: Configure Nginx

In the docker folder of your project, create a file named nginx.Dockerfile.

FROM nginx:stable-alpine

ADD ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf

RUN mkdir -p /var/www/html

Create an nginx directory in a docker folder and within it, create a default.conf file and insert the following Nginx server configuration:

server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/html/public;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

Step 4: Build the Docker Containers

Now, we’re ready to build our Docker containers. From the root directory of your Laravel project, run the following command:

docker compose build

Step 5: Create a New Laravel Project

We need to create a new Laravel project. Navigate to the directory where you wish to create your project and run the following command:

docker compose run --rm composer create-project laravel/laravel .

After running this command, the project code should appear in the src folder.

Start docker containers

docker compose up -d

Your Laravel application is now accessible at http://localhost:80

Step 6. Configure Laravel project

Configure Mysql in /src/.env . Uncomment and change:

DB_CONNECTION=mysql       # connection name, we use mysql
DB_HOST=mysql # name of mysql service in docker-compose.yml
DB_PORT=3306 # mysql standart port
DB_DATABASE=laraveldb # database name from MYSQL_DATABASE in docker-compose.yml
DB_USERNAME=laravel # username from MYSQL_USER in docker-compose.yml
DB_PASSWORD=secret # user password from MYSQL_PASSWORD in docker-compose.yml

Restart all services

docker compose down
docker compose up -d

Step 7. Run migrations

docker compose run --rm artisan migrate

Congratulations! You’ve now successfully set up a latest version of Laravel development environment with Docker and PHP 8.2.

If you’re interested in similar articles, you might want to explore more on this site.

--

--

Vitalii Shloda
Vitalii Shloda

Written by Vitalii Shloda

Software Engineer. I write about backend, data and other amazing stuff

Responses (7)