Introduction
Combining Laravel, a robust PHP framework, with Docker, a cutting-edge containerization technology, is a modern approach to software development. This tutorial guides you through setting up Laravel with Docker Compose in a step-by-step fashion. Whether you are a seasoned developer looking to dockerize your Laravel applications or a beginner curious about containerization, this guide will set you on the right path.
Prerequisites
- Docker and Docker Compose installed on your machine
- Basic understanding of Docker concepts
- Knowledge of Laravel’s requirements
Getting Started
First, ensure that Docker and Docker Compose are correctly installed on your machine. You can verify this by running docker --version
and docker-compose --version
commands in the terminal.
$ docker --version
Docker version xx.xx.x, build xxxxxxxx
$ docker-compose --version
Docker Compose version x.x.x, build xxxxxxx
If Docker is not installed, please follow the official Docker installation guide for your respective operating system at docker.com/get-started.
Setting up Docker Compose for Laravel
We’ll start by creating a new directory for our Laravel project:
mkdir laravel-docker && cd laravel-docker
Create the Dockerfile
Inside the laravel-docker
directory, create a Dockerfile
with the following content:
FROM php:8.0-fpm
RUN apt-get update && apt-get install -y \n libonig-dev \n libxml2-dev \n zip \n unzip \n git \n curl \n libpng-dev \n libjpeg62-turbo-dev \n libfreetype6-dev \n && docker-php-ext-configure gd --with-freetype --with-jpeg \n && docker-php-ext-install -j$(nproc) gd mbstring exif pcntl bcmath opcache \n && pecl install redis && docker-php-ext-enable redis
This will set up a PHP environment with all the necessary extensions required for a typical Laravel application.
Define Your Services in docker-compose.yml
Create a docker-compose.yml
file at the root of your project directory with the following services:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel-docker-app
container_name: laravel-docker-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
networks:
- laravel
webserver:
image: nginx:alpine
container_name: laravel-docker-webserver
restart: unless-stopped
tty: true
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./docker/nginx:/etc/nginx/conf.d/
networks:
- laravel
db:
image: mysql:5.7
container_name: laravel-docker-db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laraveluser
MYSQL_PASSWORD: laravelpassword
MYSQL_ROOT_PASSWORD: rootpassword
volumes:
- dbdata:/var/lib/mysql
networks:
- laravel
networks:
laravel:
driver: bridge
volumes:
dbdata:
driver: local
In this configuration, we define three services: app
for our Laravel application, a webserver
using Nginx, and a db
service running MySQL.
Under the volumes
section, we create a persistent volume for our database to ensure data is not lost when the container shuts down.
Running the Containers
To bring up the Docker environment, execute the following command:
docker-compose up -d
This will build and start all the services defined in your docker-compose.yml
file. You can check the status of your containers with:
docker-compose ps
Conclusion
Setting up Laravel with Docker Compose introduces consistency in development environments amongst team members and enhances production deployment strategies. This guide provided you with the essential steps to containerize your Laravel applications using Docker Compose.