Skip to content

How to Setup Laravel with Dockerfile and Docker-Compose

Advertisement

Setting up Laravel with Docker and Docker Compose is an excellent way to create a consistent development environment. Docker simplifies app deployment by packaging everything needed, from code to libraries, in isolated containers, so your Laravel app can run smoothly on any system. With Docker, developers can avoid the common “it works on my machine” problem by ensuring that everyone’s development setup is the same. Laravel, one of the most popular PHP frameworks, is great for building robust web applications, and pairing it with Docker makes it even easier to manage and deploy.

In this guide, we’ll walk you through setting up Laravel using a Dockerfile and Docker Compose. The Dockerfile helps define the app’s environment, while Docker Compose makes it easy to manage multiple containers, like the web server, PHP, and database.

Prerequisites

  • Docker installed on your system.
  • Docker-compose installed on your system.
  • Basic understanding of Docker concepts such as images, containers, and Dockerfile.

Step 1: Setting up Laravel

Start by creating a new Laravel project or navigating to your existing project.

composer create-project --prefer-dist laravel/laravel my-laravel-app 
cd my-laravel-app 

Step 2: Create a Dockerfile

The Dockerfile contains instructions to Docker for building your app’s environment.

Let’s create a file named Dockerfile in your project root directory and add the following:


FROM php:8.3-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev zip git curl unzip

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd pdo pdo_mysql

# Get Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Copy existing application directory
COPY . .

# Install Laravel dependencies
RUN composer install

CMD ["php-fpm"]

EXPOSE 9000

Step 3: Docker-compose Configuration

Docker-compose helps in defining and running multi-container Docker applications. Create a docker-compose.yml file in the project root:


services:

  # PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: my-laravel-app
    container_name: app
    restart: unless-stopped
    tty: true
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./storage:/var/www/storage
    networks:
      - app-network

  # MySQL Service
  database:
    image: mysql:5.8
    container_name: database
    restart: unless-stopped
    tty: true
    ports:
      - "4306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_PASSWORD: password
      MYSQL_USER: user
    networks:
      - app-network

  # Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./nginx:/etc/nginx/conf.d
    networks:
      - app-network

# Networks Configuration
networks:
  app-network:
    driver: bridge
 

Step 4: Nginx Configuration

The Docker-compose uses Nginx web server for running the application.

Create a new folder nginx in your project root. Inside, create a file named default.conf with the following content:


server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
 

Step 5: Start the Docker Containers

With everything set up, start your containers:

docker-compose up -d 

Once the containers are up, you can visit http://localhost:8000 in your browser to view your Laravel application.

Step 6: Running Artisan Commands

To run Laravel’s Artisan commands (like migrations), you need to execute them inside the container.

Use the following command to access the Laravel container:

docker-compose exec app bash

Now you can run any Artisan command, such as:

php artisan migrate

This will run the migrations and set up the tables in your MySQL database.

Step 7: Cleanup

To stop the services, you can use:

docker-compose down 

Conclusion

By integrating Laravel with Docker and Docker-compose, developers can ensure a consistent environment throughout development, testing, and production stages. This not only eases the deployment process but also eradicates the common “it works on my machine” problem. As you become more accustomed to Docker, you can further enhance and customize this setup to suit your specific needs.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top