DEV Community

Cover image for Laravel Application Setup with Docker: A Step-by-Step Guide
Anas Hussain
Anas Hussain

Posted on

Laravel Application Setup with Docker: A Step-by-Step Guide

Prerequisites

  • A system with Docker support (Linux, macOS, or Windows).
  • Basic knowledge of Laravel and Docker.

Step 1: Install Docker

For Linux

  1. Update your system packages:
   sudo apt-get update  
Enter fullscreen mode Exit fullscreen mode
  1. Install Docker:
   sudo apt-get install -y docker.io  
Enter fullscreen mode Exit fullscreen mode
  1. Start and enable Docker:
   sudo systemctl start docker  
   sudo systemctl enable docker  
Enter fullscreen mode Exit fullscreen mode

For macOS and Windows

Download and install Docker Desktop from Docker's official site.


Step 2: Set Up the Laravel Project

Clone the Project Repository

git clone <your-repo-url>  
cd <project-name>  
Enter fullscreen mode Exit fullscreen mode

Install Dependencies

composer install  
Enter fullscreen mode Exit fullscreen mode

Prepare the Environment

  1. Create a .env file:
   cp .env.example .env  
Enter fullscreen mode Exit fullscreen mode
  1. Generate the application key:
   php artisan key:generate  
Enter fullscreen mode Exit fullscreen mode

Update the Dockerfile and docker-compose.yml

Example Dockerfile

FROM php:8.2-apache  
WORKDIR /var/www/html  
COPY . .  
RUN docker-php-ext-install pdo pdo_mysql  
Enter fullscreen mode Exit fullscreen mode

Example docker-compose.yml

version: '3.8'  
services:  
  app:  
    build: .  
    ports:  
      - "8080:80"  
    volumes:  
      - .:/var/www/html  
    environment:  
      - APACHE_LOG_DIR=/var/log/apache2  
    depends_on:  
      - db  

  db:  
    image: mysql:8.0  
    environment:  
      MYSQL_ROOT_PASSWORD: root  
      MYSQL_DATABASE: laravel  
      MYSQL_USER: laravel  
      MYSQL_PASSWORD: secret  
Enter fullscreen mode Exit fullscreen mode

Build and Start Docker Containers

docker-compose build  
docker-compose up -d  
Enter fullscreen mode Exit fullscreen mode

Set Permissions

chmod -R 775 bootstrap/cache storage  
chown -R www-data:www-data bootstrap/cache storage  
chmod 775 database  
chown -R www-data:www-data database  
chmod 664 database/database.sqlite  
chown www-data:www-data database/database.sqlite  
Enter fullscreen mode Exit fullscreen mode

Migrate the Database

php artisan migrate  
Enter fullscreen mode Exit fullscreen mode

Step 3: Access the Application

Once the containers are running, access your Laravel application at:

http://localhost:8080


Step 4: Reference

Find the complete project code on GitHub:

GitHub Repository Link


Step 5: Most Used Docker Commands

Basic Commands

  • docker --version: Check Docker version.
  • docker ps: List running containers.
  • docker ps -a: List all containers.
  • docker images: List all Docker images.
  • docker pull <image>: Pull an image from Docker Hub.
  • docker build -t <name> .: Build an image from a Dockerfile.
  • docker run -d -p <host-port>:<container-port> <image>: Run a container.
  • docker exec -it <container-id> bash: Access a container's shell.
  • docker stop <container-id>: Stop a running container.
  • docker rm <container-id>: Remove a stopped container.

Advanced Commands

  • docker logs <container-id>: View container logs.
  • docker-compose up: Start services defined in docker-compose.yml.
  • docker-compose down: Stop and remove services.
  • docker volume ls: List volumes.
  • docker network ls: List networks.
  • docker inspect <container-id>: Inspect container details.
  • docker system prune: Clean up unused containers, images, and volumes.
  • docker cp <src> <container>:<dest>: Copy files to a container.
  • docker stats: View real-time resource usage.
  • docker tag <image-id> <new-name>: Tag an image.

Refer to the Docker Documentation for more commands and usage examples.


By following these steps, you’ll have a Laravel application running in Docker in no time!

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs