Bird
Raised Fist0
Laravelframework~20 mins

Docker deployment in Laravel - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Docker Deployment Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when running Laravel inside Docker with this Dockerfile?
Consider this Dockerfile for a Laravel app. What will be the result when you build and run the container?

FROM php:8.1-fpm
WORKDIR /var/www/html
COPY . .
RUN docker-php-ext-install pdo pdo_mysql
CMD ["php-fpm"]
Laravel
FROM php:8.1-fpm
WORKDIR /var/www/html
COPY . .
RUN docker-php-ext-install pdo pdo_mysql
CMD ["php-fpm"]
AThe container runs PHP-FPM serving the Laravel app, but no web server is included so no HTTP requests are handled.
BThe container runs a full Apache server serving the Laravel app out of the box.
CThe container fails to build because pdo_mysql extension is not available for PHP 8.1.
DThe container runs but Laravel will not connect to the database because no database service is included.
Attempts:
2 left
💡 Hint
Think about what PHP-FPM does and what is missing to serve HTTP requests.
📝 Syntax
intermediate
2:00remaining
Which docker-compose.yml snippet correctly defines a Laravel app with MySQL service?
You want to run Laravel with a MySQL database using Docker Compose. Which snippet is correct?
Laravel
version: '3.8'
services:
  app:
    build: .
    ports:
      - "8000:80"
    depends_on:
      - db
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: laravel
      MYSQL_USER: user
      MYSQL_PASSWORD: secret
AThe app service should expose port 3306 for MySQL, not 8000.
BThe snippet is missing volumes for persistent MySQL data, so data will be lost on restart.
CThe snippet is correct and will start Laravel app on port 8000 with MySQL database.
DThe environment variables for MySQL are incorrect; MYSQL_USER is not valid.
Attempts:
2 left
💡 Hint
Check if the services and environment variables match Laravel and MySQL requirements.
🔧 Debug
advanced
2:00remaining
Why does Laravel fail to connect to MySQL in this Docker setup?
Given this docker-compose.yml snippet:
services:
  app:
    build: .
    environment:
      DB_HOST: localhost
      DB_DATABASE: laravel
      DB_USERNAME: user
      DB_PASSWORD: secret
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: laravel
      MYSQL_USER: user
      MYSQL_PASSWORD: secret

Laravel inside the app container cannot connect to MySQL. Why?
Laravel
services:
  app:
    build: .
    environment:
      DB_HOST: localhost
      DB_DATABASE: laravel
      DB_USERNAME: user
      DB_PASSWORD: secret
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: laravel
      MYSQL_USER: user
      MYSQL_PASSWORD: secret
ADB_HOST should be 'db' not 'localhost' because containers have separate networks.
BThe MySQL image version 8 is incompatible with Laravel's database driver.
CThe environment variables must be set in a .env file, not in docker-compose.yml.
DThe app container needs to expose port 3306 to connect to MySQL.
Attempts:
2 left
💡 Hint
Think about how Docker containers communicate by service name, not localhost.
❓ lifecycle
advanced
2:00remaining
What happens if you run 'docker-compose down' in a Laravel Docker setup with volumes?
You have a Laravel app and MySQL running with Docker Compose. The MySQL service uses a named volume for data. What happens when you run 'docker-compose down'?
Laravel
version: '3.8'
services:
  app:
    build: .
  db:
    image: mysql:8
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:
AThe command fails because volumes cannot be used with 'docker-compose down'.
BThe containers and the MySQL data volume are deleted, losing all database data.
COnly the app container is removed; the db container and volume remain running.
DThe containers stop and are removed, but the MySQL data volume remains intact.
Attempts:
2 left
💡 Hint
Consider the difference between 'docker-compose down' and 'docker-compose down -v'.
🧠 Conceptual
expert
3:00remaining
Which is the best way to optimize Laravel Docker image size for production?
You want to deploy a Laravel app with Docker in production. Which approach best reduces the final image size while keeping functionality?
AUse a single Dockerfile with all build tools and runtime in one image to avoid complexity.
BUse a multi-stage Dockerfile: build assets and dependencies in one stage, then copy only needed files to a smaller PHP runtime image.
CUse the official php:8.1-fpm image and install all composer dependencies and node modules inside it directly.
DUse Alpine Linux base image and install all Laravel dependencies manually to minimize size.
Attempts:
2 left
💡 Hint
Think about separating build environment from runtime environment.

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