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

Update your system packages:

sudo apt-get update

In…


This content originally appeared on DEV Community and was authored by Anas Hussain

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  
  1. Install Docker:
   sudo apt-get install -y docker.io  
  1. Start and enable Docker:
   sudo systemctl start docker  
   sudo systemctl enable docker  

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>  

Install Dependencies

composer install  

Prepare the Environment

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

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  

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  

Build and Start Docker Containers

docker-compose build  
docker-compose up -d  

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  

Migrate the Database

php artisan migrate  

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!


This content originally appeared on DEV Community and was authored by Anas Hussain


Print Share Comment Cite Upload Translate Updates
APA

Anas Hussain | Sciencx (2025-01-14T22:02:25+00:00) Laravel Application Setup with Docker: A Step-by-Step Guide. Retrieved from https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/

MLA
" » Laravel Application Setup with Docker: A Step-by-Step Guide." Anas Hussain | Sciencx - Tuesday January 14, 2025, https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/
HARVARD
Anas Hussain | Sciencx Tuesday January 14, 2025 » Laravel Application Setup with Docker: A Step-by-Step Guide., viewed ,<https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/>
VANCOUVER
Anas Hussain | Sciencx - » Laravel Application Setup with Docker: A Step-by-Step Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/
CHICAGO
" » Laravel Application Setup with Docker: A Step-by-Step Guide." Anas Hussain | Sciencx - Accessed . https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/
IEEE
" » Laravel Application Setup with Docker: A Step-by-Step Guide." Anas Hussain | Sciencx [Online]. Available: https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/. [Accessed: ]
rf:citation
» Laravel Application Setup with Docker: A Step-by-Step Guide | Anas Hussain | Sciencx | https://www.scien.cx/2025/01/14/laravel-application-setup-with-docker-a-step-by-step-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.