How to Docker a Laravel Project in 2024?

Tuan Burah
5 min readMar 27, 2024

Laravel in Docker | Dev-ops | Containerization

Docker Cover
Docker Cover

In the vast ocean of software development practices, Docker stands out as a guiding star on the path towards streamlined deployment and consistent development environment. For newbies working there way onto this realm of practices, understanding Docker’s significance and the basic principles can be a key to charting a course through the uncharted waters.

Fear no more for this blog will solve all your doubts from the basics until you are confident enough to run a project with Docker yourself.

So what is Docker?

Text book wise at its core, Docker is a containerization platform that enables developers to encapsulate applications and their dependencies into lightweight, portable containers. In simple words, Docker can be considered as a shipping container which carries its own set own development environment such as Node, React, Vue, .net or .net core, Laravel, Ruby, etc. Docker is a containerization platform technology widely used by development teams to maintain a project’s environmental configuration whilst maintaining its state without interfering other projects working in parallel and provide consistent deployment and feasibility.

Docker Containerized Application
Docker Containerized Application Overview

Setting Sail with Laravel

First, let’s set our course with Laravel. Born out of a desire for an expressive and elegant syntax, Laravel provides developers with a rich set of tools and features for building modern web applications. From robust authentication and authorization mechanisms to powerful ORM (Object-Relational Mapping) capabilities, Laravel simplifies common development tasks and promotes best practices, allowing developers to focus on building meaningful features rather than reinventing the wheel.

Docking Laravel with Docker

Now that we understand the uses of both Laravel and Docker, we can move on to using both of them to have a streamlined operation.

Using Docker, you can create a standardized development environment for your Laravel projects. By defining a Dockerfile and a Docker Compose configuration, you can specify the dependencies and services required for your application, such as PHP, MySQL, and Redis. With a single command, developers can spin up the entire development environment, ensuring consistency across different machines and operating systems.

Step 01 — Creating a Compose file

Making a compose file is a compulsory with a Laravel project defining the environment variables and the commands. This file is usually labelled as docker-compose.yml. The file must be created inside the Laravel project’s root folder. Inside it the contents will be categorized into sections as below,

  • Services
  • Networks
  • Volumes

All the packages required for the development environment will be defined under the Services section such as, the build image, mysql, redis, etc. Here’s a piece of code used by an industrial docker-compose.yml file.

version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.2
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.2/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
IGNITION_LOCAL_SITES_PATH: '${PWD}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
- meilisearch
- mailpit
- selenium
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: '${DB_HOST}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test:
- CMD
- mysqladmin
- ping
- '-p${DB_PASSWORD}'
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test:
- CMD
- redis-cli
- ping
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- 'sail-meilisearch:/meili_data'
networks:
- sail
healthcheck:
test:
- CMD
- wget
- '--no-verbose'
- '--spider'
- 'http://localhost:7700/health'
retries: 3
timeout: 5s
mailpit:
image: 'axllent/mailpit:latest'
ports:
- '${FORWARD_MAILPIT_PORT:-1025}:1025'
- '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
selenium:
image: selenium/standalone-chrome
extra_hosts:
- 'host.docker.internal:host-gateway'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
environment:
- PMA_HOST=mysql
- PMA_PORT=3306
- UPLOAD_LIMIT=100000000
networks:
- sail
ports:
- 8050:80
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local
sail-meilisearch:
driver: local

Step 02 — Docker Compose: Putting it All Together

Once you’ve defined your services, networks, and volumes in the docker-compose.yml file, Docker Compose makes it easy to manage your multi-container application stack with simple commands. Here are a few common commands:

  • docker-compose up: Start the services defined in the docker-compose.yml file.
  • docker-compose down: Stop and remove the services.
  • docker-compose build: Build or rebuild services.
  • docker-compose restart: Restart services.
  • docker-compose logs: View logs for services

Managing with Docker Desktop

If you have installed docker desktop, you can manage the containers for each of your projects via the Docker Container panel. Each container will be highlighted separately with a start and stop sign. If the containers use the same image the containers cannot be run simultaneously.

Docker Desktop Container panel
Docker Container panel

When working with Docker Compose and the docker-compose.yml file, consider the following best practices:

  1. Keep it Modular: Break down your application stack into separate services, each responsible for a specific component. This promotes modularity and simplifies management.
  2. Use Environment Variables: Leverage environment variables in your docker-compose.yml file for configuration flexibility and security.
  3. Versioning: Specify the version of Docker Compose at the top of your docker-compose.yml file to ensure compatibility and consistency.
  4. Documentation: Document your docker-compose.yml file thoroughly, including service descriptions, dependencies, and configurations, to aid collaboration and understanding when running multiple projects.

Conclusion: How to Docker a Laravel Project in 2024?

In conclusion, the docker-compose.yml file serves as the blueprint for defining and managing multi-container Docker applications with Docker Compose. By understanding its syntax, capabilities, and best practices, developers can harness the power of Docker Compose to simplify the orchestration of complex application stacks and streamline the development and deployment process. With Docker Compose as your guide, you can navigate the seas of containerization with confidence and efficiency. Smooth sailing awaits!

If you have any questions regarding the process, comment below. Note that this method is functional on Laravel v10, v9 and v8 where the PHP language is stable as PHP v8. There may be small changes needed to be made for other versions due to the complexity or deprecated libraries of Laravel helpers and PHP versions.

References

--

--

Tuan Burah

M.Sc. in IT (reading) | Pg.D. in IT | Student Member of British Computer Society — Royal Charter | Member of LivePhysics Science Community