Dockerize and Deploy a Laravel Application to Production

July 28, 2021

Édouard Bonlieu

Édouard Bonlieu

Koyeb provides developers the fastest way to deploy full stack applications and APIs globally. Want to deploy your Laravel application in minutes? Sign up today and deploy 2 services for free forever!


Laravel is a popular, simple and flexible PHP application framework. Laravel comes with many build features offering a great developer experience such as thorough dependency injection, an expressive database abstraction layer, queues and scheduled jobs, unit and integration testing, and more.

In this guide, we will explain how to dockerize and deploy a Laravel application to production using Docker to containerize our application and deploy it to the Koyeb Serverless Platform.

By deploying your Laravel application on Koyeb, you benefit from native autoscaling, autohealing, TLS encryption, global load balancing across our edge network, service discovery, and more.


To successfully follow and complete this guide, you need:

  • Docker installed on your machine
  • A Koyeb account to deploy and run the Laravel web application
  • The Koyeb CLI installed to interact with Koyeb from the command line
  • Have a registry we will use to store our Laravel web app Docker image and deploy it on Koyeb


To successfully deploy a Laravel web application on the Koyeb serverless platform, you need to follow these steps:

  1. Create a new Laravel application
  2. Dockerize the Laravel application
  3. Push the Docker image to a container registry
  4. Deploy the Dockerized Laravel web app on Koyeb

Create a new Laravel application

If you already have an existing Laravel application you want to dockerize and deploy on Koyeb, you can jump to the next step.

To get started, we will create a new Laravel project using Composer, a PHP package manager. In your terminal, run the following command:

composer create-project --prefer-dist laravel/laravel laravel-demo

This command will create a new Laravel application in the laravel-demo directory and install all the dependencies required by the app to run properly.

You can launch the application in development mode by running the following command in your terminal from the laravel-demo directory:

php artisan serve

Open a browser window and navigate to the http://localhost:8000 URL. You should see the Laravel welcome page.

Dockerize the Laravel application

With our minimalistic Laravel application ready, we can now create the Dockerfile that we will use to containerize our Laravel application.

We will use the webdevops/php-nginx:7.4-alpine as the base image which provides Nginx with PHP-FPM installed and configured. The image can be customized depending on your needs. You can read the full documentation here. In your application directory, create a new file named Dockerfile and paste the following code:

FROM webdevops/php-nginx:7.4-alpine # Install Laravel framework system requirements ( RUN apk add oniguruma-dev postgresql-dev libxml2-dev RUN docker-php-ext-install \ bcmath \ ctype \ fileinfo \ json \ mbstring \ pdo_mysql \ pdo_pgsql \ tokenizer \ xml # Copy Composer binary from the Composer official Docker image COPY --from=composer:latest /usr/bin/composer /usr/bin/composer ENV WEB_DOCUMENT_ROOT /app/public ENV APP_ENV production WORKDIR /app COPY . . RUN composer install --no-interaction --optimize-autoloader --no-dev # Optimizing Configuration loading RUN php artisan config:cache # Optimizing Route loading RUN php artisan route:cache # Optimizing View loading RUN php artisan view:cache RUN chown -R application:application .

As you probably noticed, we don't specify the CMD to tell Docker how to run our container. This is because the base image FROM webdevops/php-nginx:7.4-alpine run the Nginx and PHP-FPM in the foreground and we inherit this command.

If your application requires additional php extensions, you can install them using the docker-php-ext-install command.

In this guide we will push the Docker image to the Docker Hub. You are free to use another different registry as Koyeb allows you to deploy from any container registry.

To build the image run the following command and replace <DOCKER_HUB_USERNAME> with your own.

docker build -t <DOCKER_HUB_USERNAME>/laravel-demo .

Once the build is complete, you can run the image locally to ensure everything is working as expected.

docker run --rm -t -p 9000:80 <DOCKER_HUB_USERNAME>/laravel-demo

Push the Docker image to a container registry

Now that we have our Docker image ready, we can push it to the Docker Hub by running:

docker push <DOCKER_HUB_USERNAME>/laravel-demo

Once the image is successfully pushed to the Docker Hub, we can use it to deploy the Laravel application on Koyeb.

Deploy the Dockerized Laravel web app on Koyeb

With the previous steps completed, we can now deploy our application on production to Koyeb. This section explains how to deploy on the Koyeb serverless platform using the CLI. This operation can also be performed using the control panel.

First, we need to create a Koyeb Secret containing the Docker Hub configuration to deploy private images. In your terminal execute the following command and replace <DOCKER_HUB_USERNAME> and <DOCKER_HUB_TOKEN> with your own.

echo \ '{ "auths": { "": { "username": "<DOCKER_HUB_USERNAME>", "password": "<DOCKER_HUB_TOKEN>" } } }' | koyeb secrets create docker-hub-credentials --value-from-stdin

We can now deploy the Laravel application by running:

koyeb app init laravel-demo --docker "<DOCKER_HUB_USERNAME>/laravel-demo" --docker-private-registry-secret docker-hub-credentials

This command creates a new Koyeb App and deploy the Laravel application using the Docker image. To retrieve your Koyeb App URL and access your application, run:

$koyeb app get laravel-demo ID NAME DOMAINS UPDATED AT d58ebed1-48c0-46b7-a2f1-91f3ffdbcccc laravel-demo laravel-demo-<YOUR_ORG> 2021-06-28 12:46:55.411403 +0000 UTC

Open the URL in your browser to access your application running on Koyeb.

This guide shows how easy it is to deploy your Laravel application in production using Docker and Koyeb. Thanks to Koyeb native global load balancing edge network, your content is distributed near your users and your app comes with native TLS encryption.

To learn more about what Koyeb has to offer, check out the features page.

Welcome to Koyeb

Koyeb is a developer-friendly serverless platform to deploy any apps globally.

Start for free
Start for free, pay as you grow

Deploy 2 services for free and enjoy our predictable pricing as you grow

Deploy your first app in no time

Get up and running in 5 minutes