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:

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

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

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

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 .

It is now time to dockerize our Laravel project.

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.

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.