Laravel Installation and Development Environment Preparation with Docker Compose ğŸ³
In this guide, you will learn how to quickly deploy your Laravel application in three separate containers (PHP-FPM, MySQL, Nginx) on Ubuntu 22.04 using Docker Compose. Your development environment will be completely isolated and you will get a portable structure. This method is especially useful in teamwork and to ensure consistency across environments.
What Will You Learn in This Guide?
You will learn how to containerize your Laravel application with Docker Compose. You will run PHP-FPM, MySQL and Nginx services in three separate containers, thus creating an isolated and portable development environment. Additionally, you can easily replicate this environment and test your entire application before going into production.
Prerequisites
Note that you must have the following tools installed before you start:
- Ubuntu 22.04 operating system (sudo is an authorized user) – Docker
- Docker Compose
1. Downloading Demo Application Code
To get started, let's pull the demo version of your Laravel app from GitHub.
Downloading and Extracting Codes
First, download the application codes to your home directory (~):
cd ~
curl -L https://github.com/do-community/travellist-laravel-demo/archive/tutorial-1.0.1.zip -o travellist.zip
Next, extract the file and rename the directory:
sudo apt update
sudo apt install unzip
unzip travellist.zip
mv travellist-laravel-demo-tutorial-1.0.1 travellist-demo
cd travellist-demo
2. Setting the Environment Variables File (.env)
Laravel saves environment-dependent settings in the .env file. We need to update this file according to the Docker service names.
Creating and Editing the .env File First, create a new .env file by copying the .env.example file:
cp .env.example .env
nano .env
Since the database service will be defined as db, change DB_HOST to db:
DB_CONNECTION=mysql
DB_HOST=db # Burayı db olarak değiştirin
DB_PORT=3306
DB_DATABASE=genixnode_list
DB_USERNAME=genix_user
DB_PASSWORD=cokgizlisifre
With this change, connection to the database service will be provided in the Docker environment.
3. Preparing Dockerfile for Application Container
We will create a custom Dockerfile for our application container running on PHP-FPM. This will include Composer and the necessary PHP modules.
Creating Dockerfile
nano Dockerfile
Add the following content to the Dockerfile:
FROM php:7.4-fpm
# Docker Compose'de tanımlanan argümanlar
ARG user
ARG uid
# Sistem bağımlılıklarını kurma
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# PHP eklentilerini kurma
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Composer'ı ekleme
COPY /usr/bin/composer /usr/bin/composer
# Kullanıcı oluşturma
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
WORKDIR /var/www
USER $user
Why Use This Dockerfile? It installs the necessary dependencies for PHP-FPM, includes Composer, and sets the working directory and user permissions. This structure is necessary for your Laravel application to function properly.
4. Preparing Nginx and MySQL Configuration Files
Now, we need to prepare the configuration files for Nginx and MySQL.
Nginx Configuration File
To specify how Nginx will serve Laravel, create docker-compose/nginx/genixnode_app.conf:
mkdir -p docker-compose/nginx
nano docker-compose/nginx/genixnode_app.conf
Set the content as follows:
server {
listen 80;
index index.php index.html;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
MySQL Initial Installation File
Create the database initial load file as docker-compose/mysql/init_db.sql:
mkdir docker-compose/mysql
nano docker-compose/mysql/init_db.sql
Add the following SQL commands:
DROP TABLE IF EXISTS `yerler`;
CREATE TABLE `yerler` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`isim` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`ziyaret_edildi` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `yerler` (isim, ziyaret_edildi) VALUES ('Istanbul',0),('Ankara',0),('Izmir',1);
5. Defining the Docker Compose Configuration File (docker-compose.yml)
Now, let's create the docker-compose.yml file where we will define the three containers for our Laravel application.
nano docker-compose.yml
Add the following content:
version: "3.7"
services:
app:
build:
args:
user: genixnode_dev
uid: 1000
context: ./
dockerfile: Dockerfile
image: genixnode_laravel
container_name: genixnode-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- genixnode_net
db:
image: mysql:8.0
container_name: genixnode-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- genixnode_net
nginx:
image: nginx:alpine
container_name: genixnode-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- genixnode_net
networks:
genixnode_net:
driver: bridge
6. Running the Environment and Configuring Laravel
After completing all the configurations, we will run the environment using Docker Compose.
Building the Application Image
docker-compose build app
Starting Containers
docker-compose up -d
Checking Service Status
docker-compose ps
Installing Laravel Dependencies
docker-compose exec app composer install
Generating Laravel Key
docker-compose exec app php artisan key:generate
Once the application runs successfully, you can test it by going to http://localhost:8000 in your browser.
Frequently Asked Questions (FAQ)
- What is a container and why should I use it?
A container is a lightweight virtualization technology that runs your application and all its dependencies in an isolated environment. It ensures consistent and portable operation in different environments.
- What do the volumes in the docker-compose.yml file do?
Volumes enable data sharing between your local file system and the container. This way, changes you make during development are immediately reflected within the container.
- Why did we use Dockerfile?
While ready-made images are sufficient for MySQL and Nginx, for the Laravel application it is necessary to use PHP modules, Composer and a special Dockerfile to set user permissions.
Result
You can easily run your Laravel application in an isolated and portable environment with Docker Compose. This guide gives you a strong foundation for rapid development and porting of your app.

