Laravel Docker

broken image


Docker will create our laravel network and then create the three containers we've specified in the services section of our docker-compose.yml file. If you're curious about the -d flag, it stands for detached and keeps the containers running after all of their commands have processed. Laravel & Docker In this article, we'll discuss how to create containers for Laravel applications. Each application will have a host assigned, and the configuration will be flexible enough to be easily adapted to various projects. Larapacket is an open-source GitHub repository that has a docker ready configuration to setup all the components mentioned above and getting your environment ready for laravel application. Larapacket is a drop-in files for Laravel applications to start using Docker in development and production.

This is just a quick post sharing my basic docker setup for new laravel projects.

Setting up a new laravel project can be quite some hassle, the current options with valet, homestead or vagrant are fine but personally don't give me the flexibility I need.

That's why I created a basic laravel docker boilerplate I copy across my projects.

Laravel Docker Image

First of all you need docker installed on your pc and a laravel projects. Then copy the next two files into the root of your project:

Once you copied the files, you'll need to run docker-compose up in the root of your project directory to start the dev environment. Head over to http://localhost and you'll have your laravel project up and running.

Laravel Docker

--

The following file is optional and meant for if you want to use your own docker container image. The docker-compose above uses my pre-build docker image which is hosted on docker hub.

If you want to know more about docker or how to setup your own stack, let me know down below and I'll write something about that.

Thanks

In this article, we will learn how to set up the Laravel development environment with Docker. We are going to use the Docker Compose to set up the development environment. Let's start the steps without delay.

This may also help: Laravel, Nginx and MySQL configuration with Docker Compose

Video Tutorial

Step 1 – Download Laravel to your local machine

The first step to download the Laravel to your local machine and you can achieve this by downloading the Laravel from it's official GitHub repository. If you have git installed then you can use the git clone command or you can directly download it.

Step 2 – Create Docker Compose File

Let's create the docker-compose.yml file under our working directory i.e. laravelapp. We required only two services for the basic laravel development setup. So, we are going to set up the PHP and Apache through app service and the db service for the database setup for our Laravel application.

You can name anything instead of [app] or [db] to the services. But it's a good practice to give the relevant name.

Step 2.1 – Create Database Service

Let's understand the above steps one by one. Let's start with the image key. We have to provide the name of the official public image that you can find at hub.docker.com, as you see that we have defined the image name i.e. mysql:5.7 where 5.7 is the version of MySQL image.

The next key is restart:always the reason for this markup is because a container does not restart if it fails under any circumstance.

As the name suggests, the ports key is used to define the local port over the image's own port. In our case, we have mapped the db directory with the host's /var/lib/mysql directory.

The next key is an environment, which is used to assign a value to the specific image. In our case, we are using the mysql:5.7 image and this image have various a variety of environment variables that you can find through the official image's documentation page.

The next one is the volumes key. The volumes key mounts the project directory (current directory) on the host.

Laravel docker image

Step 2.2 – Create Separate Service for PHP and Apache Server

As you see, the app service set up is a bit different from the db service. And the reason because we don't have any official image dedicated to Laravel. We have to build our own setup for the Laravel with PHP and Apache image. The build key is used to set and use its own configuration with the help of Dockerfile. If your Dockerfile is on the root of the project directory then you don't need to use the context and the dockerfile keys and you can use instead build: . that's it.

But in our case, we have separated the custom docker configuration in a .docker directory. And later defined the Dockerfile path through the dockerfile key in our app service.

Step 2.3 – Setup Laravel Development Environment through Dockerfile

The first step is to create a .docker directory in your project's root directory. Now create a file with the name Dockerfile under the .docker directory. We required PHP, Apache, and Composer for the Laravel application. So, we are going to use the PHP docker image with apache and we will install the rest of the requirements. Now open the Dockerfile and paste the given codes.

Start with the FROM that creates a layer for the official php:7.4.1-apache Docker image. Next, define the USER and the WORKDIR for the host machine as the name suggests. Next, Install the required packages and the PHP extensions for the Laravel application. We are leaving the Dockerfile as it as for a moment and moving to another step that required to set up the DocumentRoot for the apache machine.

Step 2.4 – Virtual Host Environment Settings for Apache

Let's create a virtual host configuration file and add that to the host machine. Let's create a file vhost.conf under the .docker directory.

Laravel Docker

Keep in mind that the DocumentRoot should be public for the Laravel application.

As we already mapped in our app service through the docker-composer.yml file our Laravel application exists on the /var/www/html/ host directory. And we assigned the DocumentRoot as /var/www/html/public for our application.

Now move to Dockerfile and use the COPY directive to copy our local vhost configuration to apache configuration.

Then next step we followed is to install the composer to our machine. Next set up the ownership to the working directory and enable the apache's rewrite module.

The complete Dockerfile should look like to the given codes.

And the docker-compose.yml should look like the below example.

Step 3 – Start Laravel Application with Docker Compose

Now its time to start our Laravel application. Go to the terminal and navigate the laravelapp ( as per this article ) directory and run the given command.

The given command starts the docker on detachable mode as the -d option. It will take some time at very first time because the docker first checks for the existing images and it will download the images in case not found in local machine.

Next check the running Container ID to login to the host machine. And you need to run the docker ps command to check all the running containers.

Now time to access the hosting machine through the container id i.e. 83ad4ef2b5dc as per the screenshot and it should be different if you check in your local machine.

You should see the output similar to the below image.

Now you can install the composer packages by using the composer install command.

Step 4 – Laravel Database Connection in Docker

Now we have to connect the database to our laravel application. Which is pretty simple though the .env file.

Laravel Docker Deploy

Laravel

As we know that database is running through a separate service i.e. db, So in this case, the DB_HOST should be db instead of localhost.

I hope you guys enjoyed this article and if you like this article, then please follow us for more interested and helpful tutorials. You can follow us on Facebook and Twitter.

Related posts:

Laravel Docker Vs Homestead

Laravel

--

The following file is optional and meant for if you want to use your own docker container image. The docker-compose above uses my pre-build docker image which is hosted on docker hub.

If you want to know more about docker or how to setup your own stack, let me know down below and I'll write something about that.

Thanks

In this article, we will learn how to set up the Laravel development environment with Docker. We are going to use the Docker Compose to set up the development environment. Let's start the steps without delay.

This may also help: Laravel, Nginx and MySQL configuration with Docker Compose

Video Tutorial

Step 1 – Download Laravel to your local machine

The first step to download the Laravel to your local machine and you can achieve this by downloading the Laravel from it's official GitHub repository. If you have git installed then you can use the git clone command or you can directly download it.

Step 2 – Create Docker Compose File

Let's create the docker-compose.yml file under our working directory i.e. laravelapp. We required only two services for the basic laravel development setup. So, we are going to set up the PHP and Apache through app service and the db service for the database setup for our Laravel application.

You can name anything instead of [app] or [db] to the services. But it's a good practice to give the relevant name.

Step 2.1 – Create Database Service

Let's understand the above steps one by one. Let's start with the image key. We have to provide the name of the official public image that you can find at hub.docker.com, as you see that we have defined the image name i.e. mysql:5.7 where 5.7 is the version of MySQL image.

The next key is restart:always the reason for this markup is because a container does not restart if it fails under any circumstance.

As the name suggests, the ports key is used to define the local port over the image's own port. In our case, we have mapped the db directory with the host's /var/lib/mysql directory.

The next key is an environment, which is used to assign a value to the specific image. In our case, we are using the mysql:5.7 image and this image have various a variety of environment variables that you can find through the official image's documentation page.

The next one is the volumes key. The volumes key mounts the project directory (current directory) on the host.

Step 2.2 – Create Separate Service for PHP and Apache Server

As you see, the app service set up is a bit different from the db service. And the reason because we don't have any official image dedicated to Laravel. We have to build our own setup for the Laravel with PHP and Apache image. The build key is used to set and use its own configuration with the help of Dockerfile. If your Dockerfile is on the root of the project directory then you don't need to use the context and the dockerfile keys and you can use instead build: . that's it.

But in our case, we have separated the custom docker configuration in a .docker directory. And later defined the Dockerfile path through the dockerfile key in our app service.

Step 2.3 – Setup Laravel Development Environment through Dockerfile

The first step is to create a .docker directory in your project's root directory. Now create a file with the name Dockerfile under the .docker directory. We required PHP, Apache, and Composer for the Laravel application. So, we are going to use the PHP docker image with apache and we will install the rest of the requirements. Now open the Dockerfile and paste the given codes.

Start with the FROM that creates a layer for the official php:7.4.1-apache Docker image. Next, define the USER and the WORKDIR for the host machine as the name suggests. Next, Install the required packages and the PHP extensions for the Laravel application. We are leaving the Dockerfile as it as for a moment and moving to another step that required to set up the DocumentRoot for the apache machine.

Step 2.4 – Virtual Host Environment Settings for Apache

Let's create a virtual host configuration file and add that to the host machine. Let's create a file vhost.conf under the .docker directory.

Keep in mind that the DocumentRoot should be public for the Laravel application.

As we already mapped in our app service through the docker-composer.yml file our Laravel application exists on the /var/www/html/ host directory. And we assigned the DocumentRoot as /var/www/html/public for our application.

Now move to Dockerfile and use the COPY directive to copy our local vhost configuration to apache configuration.

Then next step we followed is to install the composer to our machine. Next set up the ownership to the working directory and enable the apache's rewrite module.

The complete Dockerfile should look like to the given codes.

And the docker-compose.yml should look like the below example.

Step 3 – Start Laravel Application with Docker Compose

Now its time to start our Laravel application. Go to the terminal and navigate the laravelapp ( as per this article ) directory and run the given command.

The given command starts the docker on detachable mode as the -d option. It will take some time at very first time because the docker first checks for the existing images and it will download the images in case not found in local machine.

Next check the running Container ID to login to the host machine. And you need to run the docker ps command to check all the running containers.

Now time to access the hosting machine through the container id i.e. 83ad4ef2b5dc as per the screenshot and it should be different if you check in your local machine.

You should see the output similar to the below image.

Now you can install the composer packages by using the composer install command.

Step 4 – Laravel Database Connection in Docker

Now we have to connect the database to our laravel application. Which is pretty simple though the .env file.

Laravel Docker Deploy

As we know that database is running through a separate service i.e. db, So in this case, the DB_HOST should be db instead of localhost.

I hope you guys enjoyed this article and if you like this article, then please follow us for more interested and helpful tutorials. You can follow us on Facebook and Twitter.

Related posts:

Laravel Docker Vs Homestead

Laravel Docker Image

  1. Containerize Nginx, Laravel and MySQL with Docker Compose
  2. Working with Laravel and Docker
  3. Laradock Laravel database connection refused
  4. Setup Node.js app with Docker
  5. Laravel Echo, Socket.io and Redis Setup
  6. Building a Todo app with Node.js, MongoDB and Docker




broken image