Laravel 12 in a Docker Environment: Step-by-Step Setup
Laravel 12: Tutorials, Guides & Best Practices

Laravel 12 in a Docker Environment: Step-by-Step Setup

Laravel 12 Installation with Docker (Complete Guide)

Author
Richard Mendes
March 17, 2025 • 5 mins

How to Set Up Laravel 12 with Docker - Installation & Setup

In this guide, we’ll walk you through how to set up a Laravel 12 application using Docker. We will use Docker Compose to set up containers for PHP, MySQL, and Adminer. Follow these steps to get your Laravel application up and running quickly.

Step1: Set Up Docker Compose

The first step is to create a docker-compose.yml file in your project directory. This file will define all the services needed to run your Laravel application, such as PHP, MySQL, and Adminer for database management.

Create docker-compose.yml file


version: "3"
services:
php-apache:
container_name: "${CONTAINER_NAME_PREFIX}-apache-php"
build: .
volumes:
- ${PATH_TO_LARAVEL_PROJECT}:/var/www/html
ports:
- "80:80"

db:
container_name: "${CONTAINER_NAME_PREFIX}-mysql"
image: mysql:8.0
restart: always
environment:
- MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PASSWORD}
volumes:
- db:/var/lib/mysql
ports:
- "3306:3306"

adminer:
container_name: "${CONTAINER_NAME_PREFIX}-adminer"
image: adminer
ports:
- "8080:8080"
volumes:
db:

Step 2: Define Environment Variables

You need to create a .env file to define the environment variables required by the docker-compose.yml file. Here's a sample:

COMPOSE_PROJECT_NAME="larapp"
CONTAINER_NAME_PREFIX=laravel-app-12

PATH_TO_LARAVEL_PROJECT='./larapp'

#DATABASE
ROOT_USER=richard
DB_USER=richardm
DB_PASSWORD=ChangeMe
DB_NAME=laravel_db
ROOT_PASSWORD=ChangeMe

Step 3: Create Dockerfile for Laravel 12 with Docker

Now that you have your docker-compose.yml set up, the next step is to create a Dockerfile to build the PHP-Apache environment required to run your Laravel application. Below is a refined version of your Dockerfile for setting up Laravel 12 with Docker.

FROM php:8.2-apache

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# INI-Files
COPY ./opcache.ini "$PHP_INI_DIR/conf.d/docker-php-ext-opcache.ini"
COPY ./xdebug.ini "$PHP_INI_DIR/conf.d/99-xdebug.ini"
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

# Install Packages
RUN apt-get -y update && apt-get install -y libicu-dev libzip-dev zip libjpeg-dev libpng-dev libfreetype6-dev git
RUN docker-php-ext-configure intl
RUN docker-php-ext-configure gd '--with-jpeg' '--with-freetype'
RUN docker-php-ext-install intl opcache pdo_mysql zip gd
RUN pecl install xdebug
RUN a2enmod rewrite

# INSTALL APCU
RUN pecl install apcu-5.1.24 && docker-php-ext-enable apcu
RUN echo "extension=apcu.so" > /usr/local/etc/php/php.ini
RUN echo "apc.enable_cli=1" > /usr/local/etc/php/php.ini
RUN echo "apc.enable=1" > /usr/local/etc/php/php.ini
# APCU

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install NVM
RUN curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Step 4: Run Docker Compose to Build and Start Containers

Once you have your docker-compose.yml and Dockerfile set up, you can use Docker Compose to build and start all the necessary containers in one command. This command will:

  1. Build the Docker images if they haven't been built already.
  2. Start the containers as defined in the docker-compose.yml file.

Run this command in your terminal:

docker-compose up --build

Step 5: Access Your Application

Once you do this you will see a directory created, create public directory and in public directory create index.php file. Go to http://localhost to view your Laravel application.

Step 6: Install Laravel

Now delete the public directory and run the the command in you container to install

composer create-project --prefer-dist laravel/laravel .

I am using dot so it creates laravel in our larapp directory


Latest Articles