paint-brush
Dockerize Your Laravel Project in a Jiffyby@xmrrabbitx
1,178 reads
1,178 reads

Dockerize Your Laravel Project in a Jiffy

by xmrrabbitxApril 15th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Docker could virtualize OS-level and gather all the needed packages. Docker consists of a series of containers that operate independently from each other. It's excellent since all team members can work on the same items. Docker is good because of: All members are accessing the same packages. Anyone can see the latest changes made by others. All settings and versions are the same. We must establish a primary route to the Laravel folder and have access to all of them. It must be precisely this name, with a capital D.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Dockerize Your Laravel Project in a Jiffy
xmrrabbitx HackerNoon profile picture


As you may know, Docker consists of a series of containers that operate independently from each other. It's excellent since all team members can work on the same items. Docker could virtualize OS-level and gather all the needed packages. When we talk about Docker, we are talking about running a program in a container with our custom settings. We could define these settings in a file called Dockerfile.

What are the benefits of Docker?

Maybe before this, you were using an Apache server, which is good, but it's not suitable and convenient. Docker is good because of:


  • All members are accessing the same packages.

  • Anyone can see the latest changes made by others.

  • All settings and versions are the same.


So, with these benefits, we could go to the tutorial. I'm assuming you have Docker installed on your Linux system and are familiar with Laravel.

Installing Laravel

  • create a directory and name it what you want.
  • open terminal or CMD in this directory and type in composer create-project laravel/laravel example-app


  • When you installed Laravel successfully, go to the folder of Laravel, and here is your first Laravel structure.



  • go to the public folder and cut all the files there.
  • you must navigate to the root file outside the Laravel folder.
  • paste them here like this:



So we did this because we wanted to launch Laravel without the php artisan serve command. From the root, we must establish a primary route to the Laravel folder and have access to all of them.


Dockerizing Laravel

  • Now open the index.php file in your root project and alter it like below:



So we changed it and placed “Laravel” (the name of our project directory) in all the paths.

  • Simply create a Dockerfile folder at the root (which signifies the root directory of your project) and you're done. It must be precisely this name, with a capital D.

  • Now enter these codes into your Dockerfile:


FROM php:7.4-apache

COPY ./php.ini /etc/php/7.4/apache2/php.ini

RUN a2enmod rewrite

WORKDIR /var/www/html

RUN docker-php-ext-install mysqli



Be aware that you may set the PHP version as you like.

Also, the command RUN a2enmod rewrite is used to allow accessing the subfolders in your Apache root system.

WORKDIR is the path to your Apache root system. In Windows, this path is C:\HTTPD\Apache24\htdocs whereas, in Linux, it is /var/www/html .


  • In the root, create a php.ini file and leave it empty.


It is now time to dockerize our Laravel project.


  • In the root directory where Dockerfile resides, open your terminal and type in docker build -t laravel:01 .


docker build is used for creating a new image in Docker. -t means tag name, which you may set any name for it, and the dot . means all your current directories and subdirectories of your project.



  • Now you may verify with the docker images command whether the image was created or not.



It's container turn. As you may know, a container is a Docker image for storing and executing multiple packages on Docker. As a consequence, we must design our container for it to operate. So type in this command:


docker run -d -p 84:80 -name apachelaravel -v "$PWD":/var/www/html laravel:01



docker run is obvious; it implies constructing and operating the container. -p indicates setting port 84 to the true port 80 of our Apache system.

You may verify whether your container is running or not by typing in this command docker container ls -a



Now in your browser, simply type in http://localhost:84

If you see an error like this:


The stream or file "/var/www/html/laravel/storage/logs/laravel.log" could not be accessed in append mode: unable to open stream: Permission denied The exception occurred when trying to log



Don’t worry, you can resolve this simply. Just go to the Laravel directory, open your terminal, and enter these three commands one by one:


chmod -R gu+w storage

chmod -R guo+w storage

php artisan cache:clear



These instructions allow access by the Apache server to the storage directory in Laravel.

And here you are. Just reload the page, and you will see that Laravel is running successfully.