Setting Up a Laravel Project with Docker Compose Across Multiple Environment  

Last updated: June 21, 2025

This guide provides instructions for setting up the project in different environments (local, stage, prod) using Docker Compose and Laravel. The codebase remains the same across environments, with only the database and configuration files changing.

Local Screenshot GitHub Repo: https://github.com/Gurinder-Batth/laravel-12-dockerize
Environments

Last updated: June 21, 2025

The project supports three environments:

  • Local: Uses docker-compose.yml
  • Stage: Uses docker-compose.stage.yml
  • Prod: Uses docker-compose.prod.yml

Each environment has its own .env file:

  • src/.env.local for local
  • src/.env.stage for stage
  • src/.env for prod
Setup Instructions

Last updated: June 21, 2025

Local Environment Setup

Clone the repository: git clone https://github.com/Gurinder-Batth/laravel-12-dockerize

  1. Run the following commands to set up the local environment:
    docker compose -f docker-compose.yml up -d --build
    docker compose -f docker-compose.yml exec app composer install
    docker compose -f docker-compose.yml exec app php artisan key:generate
    docker compose -f docker-compose.yml exec app php artisan migrate
  2. Open http://localhost:8003/ in a browser.
Stage Environment Setup
  1. Run the following commands to set up the stage environment:
    docker compose -f docker-compose.stage.yml up -d --build
    docker compose -f docker-compose.stage.yml exec app composer install
    docker compose -f docker-compose.stage.yml exec app php artisan key:generate
    docker compose -f docker-compose.stage.yml exec app php artisan migrate
  2. Open http://localhost:8002/ in a browser.
Production Environment Setup
  1. Run the following commands to set up the prod environment:
    docker compose -f docker-compose.prod.yml up -d --build
    docker compose -f docker-compose.prod.yml exec app composer install
    docker compose -f docker-compose.prod.yml exec app php artisan key:generate
    docker compose -f docker-compose.prod.yml exec app php artisan migrate
  2. Open http://localhost:8001/ in a browser.
Important Notes

Last updated: June 21, 2025

  • Codebase: The codebase remains the same across all environments. Only the database and .env files differ.
  • Git Branches: It’s best practice to switch to the appropriate Git branch for each environment.
  • Resource Customization: Each environment uses a different docker-compose file, allowing resource customization (e.g., including Adminer in local).
  • Credentials: All credentials for resources are defined in the respective .env files in the src/ directory.
Environment File Handling

Last updated: June 21, 2025

The Laravel application dynamically loads the appropriate .env file based on the APP_ENV variable. This is configured in src/bootstrap/app.php:

// Get environment name from APP_ENV
$env = env('APP_ENV_FILE', 'local');

// Determine the environment-specific .env file
$envFile = ".env.$env";

// Load the environment file if it exists
$basePath = dirname(__DIR__);
if (file_exists("$basePath/$envFile")) {
    $dotenv = Dotenv\Dotenv::createImmutable($basePath, $envFile);
    $dotenv->safeLoad();
}
Setting Up a New Environment

Last updated: June 21, 2025

To create a new environment (e.g., newenv):

  1. Create a new docker-compose.newenv.yml file by copying docker-compose.stage.yml.
  2. Replace all instances of stage with newenv in the new file.
  3. Create a new .env.newenv file in the src/ directory and register all necessary variables.
  4. Run the following commands:
    docker compose -f docker-compose.newenv.yml up -d --build
    docker compose -f docker-compose.newenv.yml exec app composer install
    docker compose -f docker-compose.newenv.yml exec app php artisan key:generate
    docker compose -f docker-compose.newenv.yml exec app php artisan migrate
  5. Open the application in a browser at http://localhost:{new_port}/, where {new_port} is the port defined in docker-compose.newenv.yml.
Author Contact

To create a new environment (e.g., newenv):

  1. Github: https://github.com/Gurinder-Batth
  2. Linkedin: https://www.linkedin.com/in/gurinderpal-batth/
  3. Medium: https://medium.com/@gurinderpal
  4. Website: https://gurinder.mondaygeek.live