Bird
Raised Fist0
Laravelframework~30 mins

Docker deployment in Laravel - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Docker Deployment for Laravel Application
📖 Scenario: You have built a Laravel web application and want to deploy it using Docker. Docker helps package your app with all its dependencies so it runs the same everywhere.Imagine you want to share your Laravel app with a friend who doesn't want to install PHP or Composer. Docker makes this easy by creating a container that has everything your app needs.
🎯 Goal: Build a Docker setup for a Laravel app that includes a Dockerfile and a docker-compose.yml file. This setup will run the Laravel app with PHP and a MySQL database inside containers.
📋 What You'll Learn
Create a Dockerfile for the Laravel app with PHP and Composer
Create a docker-compose.yml file to run Laravel and MySQL containers
Configure environment variables for database connection
Set up volume mounting for live code updates
💡 Why This Matters
🌍 Real World
Docker deployment is widely used to package and run web applications consistently across different environments, making it easier to share and deploy apps.
💼 Career
Many companies use Docker to deploy Laravel and other web apps. Knowing how to containerize apps and configure multi-service setups is a valuable skill for developers and DevOps roles.
Progress0 / 4 steps
1
Create Dockerfile for Laravel App
Create a file named Dockerfile in your Laravel project root. Write these exact lines to set up PHP 8.1 with Composer and copy your app code: FROM php:8.1-fpm RUN docker-php-ext-install pdo pdo_mysql COPY . /var/www/html WORKDIR /var/www/html RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install
Laravel
Hint

Use the official PHP 8.1 FPM image. Install the pdo_mysql extension for database support. Copy your Laravel app code into the container. Set the working directory. Install Composer and run composer install to get dependencies.

2
Create docker-compose.yml with Laravel and MySQL
Create a file named docker-compose.yml in your project root. Add a version key set to '3.8'. Define two services: app and db. For app, use the Dockerfile you created, map port 8000 to container port 9000, and mount the current directory to /var/www/html. For db, use the mysql:8.0 image, set environment variables MYSQL_ROOT_PASSWORD to secret and MYSQL_DATABASE to laravel, and expose port 3306. Your docker-compose.yml should look like this structure:
Laravel
Hint

Use build: . to build the app service from your Dockerfile. Map ports so you can access Laravel on localhost:8000. Mount your code so changes update live. Use the official MySQL 8 image for the database with environment variables for root password and database name.

3
Configure Laravel .env for Docker MySQL
Open your Laravel .env file. Change the database connection settings to match the Docker MySQL service. Set DB_HOST to db, DB_PORT to 3306, DB_DATABASE to laravel, DB_USERNAME to root, and DB_PASSWORD to secret. Your changes should look like this: DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=secret
Laravel
Hint

Set DB_HOST to the service name db so Laravel connects to the MySQL container. Use the same database name, username, and password as in docker-compose.yml.

4
Run Laravel with Docker Compose
Add a command to the app service in docker-compose.yml to start the PHP built-in server on port 9000 serving the Laravel public folder. Add this line under app:: command: php artisan serve --host=0.0.0.0 --port=9000 This will let you access your Laravel app at http://localhost:8000.
Laravel
Hint

Use the command key in docker-compose.yml under the app service to start Laravel's built-in server listening on all interfaces at port 9000.

Practice

(1/5)
1. What is the main purpose of using Docker for deploying a Laravel application?
easy
A. To package the Laravel app with all its dependencies for consistent environments
B. To write Laravel code faster using special Docker commands
C. To replace Laravel's routing system with Docker's networking
D. To automatically generate Laravel migrations

Solution

  1. Step 1: Understand Docker's role in deployment

    Docker packages the app and its environment so it runs the same everywhere.
  2. Step 2: Relate this to Laravel deployment

    Laravel apps need PHP, web server, database, and dependencies; Docker bundles these.
  3. Final Answer:

    To package the Laravel app with all its dependencies for consistent environments -> Option A
  4. Quick Check:

    Docker ensures consistent app environment = C [OK]
Hint: Docker bundles app and tools for same setup everywhere [OK]
Common Mistakes:
  • Thinking Docker speeds up coding
  • Confusing Docker with Laravel features
  • Believing Docker replaces Laravel's internal functions
2. Which file is typically used to define the environment and instructions to build a Docker image for a Laravel app?
easy
A. .env
B. docker-compose.yml
C. Dockerfile
D. composer.json

Solution

  1. Step 1: Identify Dockerfile's purpose

    Dockerfile contains commands to build the Docker image step-by-step.
  2. Step 2: Differentiate from other files

    docker-compose.yml runs containers; .env holds app configs; composer.json manages PHP packages.
  3. Final Answer:

    Dockerfile -> Option C
  4. Quick Check:

    Dockerfile builds image = A [OK]
Hint: Dockerfile builds image; compose runs containers [OK]
Common Mistakes:
  • Confusing docker-compose.yml with Dockerfile
  • Thinking .env defines Docker build steps
  • Mixing composer.json with Docker configs
3. Given this docker-compose.yml snippet for Laravel:
services:
  app:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: secret
What happens when you run docker-compose up?
medium
A. It builds the app image, runs app on port 8000, and starts MySQL with root password
B. It only runs MySQL container without the app
C. It builds app image but fails to start MySQL due to missing password
D. It runs app on port 80 without MySQL

Solution

  1. Step 1: Analyze docker-compose services

    App service builds from current folder, maps port 8000 to container's 80, mounts code volume. DB uses MySQL image with root password set.
  2. Step 2: Understand docker-compose up behavior

    It builds and starts all defined services, so both app and db run with given configs.
  3. Final Answer:

    It builds the app image, runs app on port 8000, and starts MySQL with root password -> Option A
  4. Quick Check:

    Compose runs all services with configs = B [OK]
Hint: docker-compose up runs all services with given configs [OK]
Common Mistakes:
  • Assuming only one service runs
  • Missing environment variable for MySQL password
  • Confusing port mapping direction
4. You wrote this Dockerfile for Laravel but get an error when building:
FROM php:8.1-fpm
RUN apt-get update && apt-get install -y git zip unzip
COPY . /var/www/html
RUN composer install
What is the likely cause of the build failure?
medium
A. apt-get update should be run after composer install
B. COPY command syntax is incorrect
C. PHP version 8.1-fpm is not supported by Laravel
D. Missing installation of Composer before running composer install

Solution

  1. Step 1: Check Dockerfile commands order

    Composer is a separate tool and must be installed before running composer install.
  2. Step 2: Identify missing Composer installation

    Dockerfile does not install Composer, so the command fails.
  3. Final Answer:

    Missing installation of Composer before running composer install -> Option D
  4. Quick Check:

    Composer must be installed before use = D [OK]
Hint: Install Composer before running composer commands [OK]
Common Mistakes:
  • Assuming PHP image includes Composer by default
  • Thinking COPY syntax causes build error
  • Believing PHP 8.1-fpm is incompatible with Laravel
5. You want to deploy a Laravel app with Docker using docker-compose. You need PHP, MySQL, and Redis services. Which docker-compose.yml snippet correctly sets up these services with proper ports and environment variables?
hard
A. services: app: image: laravel/php ports: - "80:80" db: image: mysql environment: MYSQL_PASSWORD: secret cache: image: redis ports: - "6379:6379"
B. services: app: build: . ports: - "8080:80" volumes: - .:/var/www/html db: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: secret cache: image: redis:latest ports: - "6379:6379"
C. services: app: build: . ports: - "80:8080" db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: root cache: image: redis ports: - "6379:6379"
D. services: app: build: . ports: - "8000:8000" db: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: secret cache: image: redis:latest

Solution

  1. Step 1: Check app service configuration

    services: app: build: . ports: - "8080:80" volumes: - .:/var/www/html db: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: secret cache: image: redis:latest ports: - "6379:6379" builds app from current folder, maps port 8080 to container port 80, and mounts code volume correctly.
  2. Step 2: Verify db and cache services

    MySQL uses image mysql:8 with root password set; Redis uses latest image and exposes default port 6379.
  3. Step 3: Compare other options for correctness

    services: app: image: laravel/php ports: - "80:80" db: image: mysql environment: MYSQL_PASSWORD: secret cache: image: redis ports: - "6379:6379". This option misses the volume mount for app code and uses wrong environment variable for MySQL (MYSQL_PASSWORD instead of MYSQL_ROOT_PASSWORD); the option with app ports "80:8080" has wrong port mapping; the option without ports for Redis misses exposing the Redis port.
  4. Final Answer:

    services: app: build: . ports: - "8080:80" volumes: - .:/var/www/html db: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: secret cache: image: redis:latest ports: - "6379:6379" -> Option B
  5. Quick Check:

    Correct service setup with ports and env = A [OK]
Hint: Check ports and env vars carefully for all services [OK]
Common Mistakes:
  • Swapping port mappings (host:container)
  • Using wrong environment variable names
  • Omitting volume mounts for app code
  • Not exposing Redis port