Support Online
Skip to main content

Laravel Docker Installation: Laravel Environment with Docker Compose on Ubuntu 22.04

What will you learn in this guide?

This guide teaches you how to run Laravel application with Docker Compose.
The goal is to manage PHP, Nginx and MySQL services in isolated containers.
You learn to make development and production environments consistent.

🧠 Technical Summary

This guide explains how to install Laravel with Docker Compose on Ubuntu 22.04.
The goal is to eliminate the “it works on my machine” problem.
Steps; project download, .env setting, Dockerfile and service definitions.


Why Laravel with Docker?

Docker runs the application along with its dependencies.
Every developer uses the same environment.
Maintenance, update and migration processes become easier.

Key Achievements

  • Service separation: PHP, MySQL and Nginx run in separate containers.
  • Flexible configuration: Environment settings are managed with .env.
  • Repeatable installation: The same structure works in any environment.
  • Development compatibility: Files remain synchronized with the host.

Prerequisites

  • Local or remote server with Ubuntu 22.04 installed
  • User with sudo privilege
  • Docker installed
  • Docker Compose installed

1️⃣ Getting the Demo Laravel Project

cd ~
curl -L https://github.com/do-community/travellist-laravel-demo/archive/tutorial-1.0.1.zip -o travellist.zip
  • This command downloads the sample Laravel project.


sudo apt update && sudo apt install unzip
unzip travellist.zip
mv travellist-laravel-demo-tutorial-1.0.1 travellist-demo
cd travellist-demo
  • These steps make the project ready.

2️⃣ Configuring the .env File


cp .env.example .env
nano .env
  • This command creates the environment configuration file.

Edit the following fields:


APP_ENV=dev
APP_DEBUG=true
APP_URL=http://localhost:8000

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=travellist
DB_USERNAME=travellist_user
DB_PASSWORD=password
  • These settings enable the database to be accessed from within Docker.

3️⃣ Creating Dockerfile


nano Dockerfile
  • This file creates PHP-FPM based custom image.


FROM php:7.4-fpm

ARG user
ARG uid

RUN apt-get update && apt-get install -y \
git curl zip unzip libpng-dev libonig-dev libxml2-dev \
&& docker-php-ext-install pdo_mysql mbstring bcmath gd

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN useradd -G www-data,root -u $uid -d /home/$user $user
WORKDIR /var/www
USER $user
  • This structure prepares the PHP environment required for Laravel.

4️⃣ Nginx and MySQL Configurations


mkdir -p docker-compose/nginx docker-compose/mysql
  • These directories contain service settings.

Nginx Configuration


nano docker-compose/nginx/travellist.conf

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

location ~ \.php$ {
fastcgi_pass app:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
  • This structure directs PHP requests to the app service.

5️⃣ Creating docker-compose.yml


nano docker-compose.yml

version: "3.9"

services:
app:
build:
args:
user: devuser
uid: 1000
context: .
volumes:
- ./:/var/www
networks:
- travellist

db:
image: mysql:8.0
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- travellist

nginx:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d
networks:
- travellist

networks:
travellist:
driver: bridge
  • This file defines the three-service Laravel environment.

6️⃣ Running the Environment


docker compose build app
docker compose up -d
  • These commands create the image and start the services.


docker compose ps
  • This command lists running services.

7️⃣ Completing the Laravel Installation


docker compose exec app composer install
  • This command installs PHP dependencies.


docker compose exec app php artisan key:generate
  • This command generates the Laravel application key.

Access from browser:


http://localhost:8000

Troubleshooting File Permissions


docker compose exec app chown -R devuser:devuser storage bootstrap/cache
docker compose exec app chmod -R 775 storage bootstrap/cache
  • These commands fix Laravel writable directories.

What Does docker-compose.override.yml Do?

  • Override file reserves environment-specific settings.
  • Keeps development and production structures clean.
  • It is applied automatically in the local environment.

Frequently Asked Questions (FAQ)

1. Is Composer required on the host machine? No. Composer runs inside a container.

2. How to run migration? docker compose exec app php artisan migrate

3. Can new PHP extensions be added? Yes. Dockerfile is updated and the image is rebuilt.

4. How to add HTTPS? Nginx configuration or Traefik can be used.

5. Can the same structure be used in the prod environment? Yes. It is run on an image basis instead of bind mount.


Result

With Docker Compose, Laravel runs portable and consistently. Local installation confusion is eliminated. The same structure can be used in development and production. If you wish, you can try this structure on the GenixNode platform and fly your projects.