Setting up a Docker container for a Laravel Application

Raj
The Dev Guide
2 min readOct 14, 2023

--

Setting up a Docker container for a Laravel application involves several steps. Here’s a guide on how to set up a simple Docker container for a Laravel application:

Prerequisites:

Before you begin, make sure you have the following:

  1. Docker installed on your machine.
  2. A Laravel application with its dependencies. If you don’t have a Laravel application, you can create one using the Laravel framework.

Step 1: Create a Dockerfile for Laravel

Create a `Dockerfile` in your Laravel application’s root directory. Below is an example Dockerfile for a Laravel application:

In this Dockerfile:

  • We use an official PHP image as the base image for our container.
  • We set the working directory to /var/www.
  • We copy the Laravel application code into the container.
  • We install system dependencies required by Laravel and PHP extensions.
  • We install Composer, a PHP package manager, and then use it to install Laravel’s dependencies.
  • We expose port 9000 and start the PHP-FPM server.

Step 2: Build the Docker Image

Navigate to the directory containing your Dockerfile and build the Docker image using the following command:

docker build -t laravel-app:latest .

Step 3: Run the Docker Container

Once the image is built, you can run a container from it using the following command:

docker run -p 8080:9000 -v /path/to/your/laravel/app:/var/www laravel-app:latest
  • -p 8080:9000 maps port 8080 on your host machine to port 9000 in the container, where PHP-FPM is running. You can change the port if needed.
  • -v /path/to/your/laravel/app:/var/www mounts your Laravel application code from your host machine to the container's /var/www directory.

Step 4: Access Your Laravel Application

You can access your Laravel application by visiting http://localhost:8080 in your web browser. If you mapped a different port, use that port number in the URL.

Your Laravel application is now running in a Docker container. You can make code changes, rebuild the image, and rerun containers as needed for development and deployment.

--

--