See posts by tags

Setting up a development environment for the latest version of Laravel using Docker

  • 3 min read
  • 23 Jun, 2024

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 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.

First, let’s create a folder for our project and configure the structure for the project:

/docker
   /nginx # folder for nginx settings
   /php   # folder for php settings
   nginx.Dockerfile    # dockerfile for nginx 
   php.Dockerfile      # dockerfile for php 
/mysql # folder for mysql volums, empty folder
/src   # folder where the code of your site will be
docker-compose.yml  # docker conf file with services
   
   

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:

# docker/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

# docker/php/www.conf

[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: Setup Nginx Docker Environment

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

# docker/nginx/default.conf

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;
    }
}

 

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

# docker/nginx.Dockerfile

FROM nginx:stable-alpine

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

RUN mkdir -p /var/www/html

 

Step 3: Create a Docker Compose File

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

# docker-compose.yml

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 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

You can verify if the project is working by opening the browser. For example, if port set to 80 in docker-compose.yml:

http://localhost

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

If you need to run composer or artisan, we have made separate services for this case that can be started with commands:

# for composer 

docker compose run --rm composer

# for artisan

docker compose run --rm artisan

Access the Full Project

For the full project, feel free to visit GitHub repository