Richard Mendes
May 18, 2025 •
3 mins
How to Set Up Laravel 12 with Docker (Step-by-Step Guide)
Want a clean, professional Laravel 12 development setup using Docker? This guide walks you through the exact configuration, including Docker files, terminal commands, and permissions—plus a link to the full code on GitHub.
Prerequisites
- Docker Desktop installed on your system
- Basic knowledge of Laravel
- Git installed (optional but recommended)
Step 1: Project Structure
Ensure you have these main files inside your project directory:
- Dockerfile
- docker-compose.yml
- .env file (configure your database credentials)
- Laravel project folder (inside the Docker volume)
docker-compose.yml
This file defines the services:
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:
Dockerfile
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
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"
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
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
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
.env
COMPOSE_PROJECT_NAME="ahura"
CONTAINER_NAME_PREFIX=ahura
PATH_TO_LARAVEL_PROJECT='./ahura'
#DATABASE
ROOT_USER=richard
DB_USER=richardm
DB_PASSWORD=ChangeMe
DB_NAME=laravel_db
ROOT_PASSWORD=ChangeMe
Step 2: Build and Start Docker
Run this command to spin up your containers:
Now your containers will be running, go to Docker Desktop to check your containers, click on the container and open the apache container.
Step 3: Build and Start Docker
In your Docker command line use the below command to create project in root directory
composer create-project laravel/laravel .
Step 4: Set Correct Permissions
Ensure the Laravel application has the correct permissions:
chmod -R 775 .
chown -R www-data:www-data .
Get the important config files here:
https://github.com/richard9004/docker-for-laravel-news-app