Creating docker environment for laravel project with Devilbox
Tags: devilbox, docker, php, laravel
Published: 2022-04-13 15:26:40

In this article, when we want to develop a project locally, or when we download an existing project to our computer and do development, the first problem is to prepare the local environment for the technologies we will use in the project. Although this job may seem simple, unfortunately it is a tiring process. In addition, if we work simultaneously on projects in more than one different infrastructure, the situation can become even more difficult. So what are the solutions we have?

- **Localhost programs**: wamp,mamp ,xampp,laragon etc.
- **Stack by operating system**: LEMP,LAMP,MEAN
- **Virtualization**: vagrant, virtualbox, vmware, valet etc.
- **docker**

The subject of our article is a docker tool that is used to quickly and effectively prepare a development environment for php frameworks and node js-based applications.

## What can we do with Devilbox?

With basic docker knowledge, you can create your development environment that is fast and does not tire your computer. Separate environments for an unlimited number of applications.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cbecpck244ur86jh3ad.png)

**Features:**
- We can use the desired versions of our server, programming language parameters without making any adjustments. For example: PHP 7.2 and mysql 5.6 are needed, we just need to activate these versions.
- Since we will work in a Docker environment, it does not exploit our computer by using our system resources at the minimum level.
- It has automatic dns configuration, it is easy to create virtual domains.
- It is easy to enable or disable the necessary modules for php.
- It offers this opportunity to those who want to use a visual interface.
- It comes with the necessary admin panels for different databases. eg: phpmyadmin, pgadmin etc.
- It offers infrastructure for almost every php framework.
- It has clear and simple documentation.
- Provides reverse proxy.
- It allows us to make custom (customized) containers and to use ready-made ones.
- It also allows us to easily generate ssl certificates with devilbox cli service and nginx acme application as a 3rd party application.

**It's time to make our first application**

> **NOTE:** I am using Mac OS as the operating system in this article. However, it can be considered the same for every operating system, except for minor operations. With that in mind, I suggest you try it.

**COMMANDS TO KNOW:**

- Basic git commands
- Docker Compose commands (up,stop,kill,rm,logs,ps)

Clone the repo to the directory you want (I chose to add it to the home directory) from the terminal.

```
git clone https://github.com/cytopia/devilbox
```
Once the download is complete, go to the Devilbox folder and create the .env file by giving the command.

```
cp env-example .env
```
Then by opening the .env file:
Search for the **PHP_SERVER** line and comment out the php version you want. Comment the others and leave it that way.In the same way, set your database, cache and server in this way.If you are a mac user, the UID and GID values may be different from linux.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gxlycc6rtfia73vh2vl.png)

```
id -u
id -g
```

You need to learn from the terminal with the and commands and change it in the .env file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a20u2mw4mclijku4twbq.png)

Since the directory where the Laravel project we install will run will be "public", we change the "**HTTPD_DOCROOT_DIR**" part of Devilbox's env file to "public".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z22feaxrtdrnw6mnqn5r.png)

For performance on Mac, let's activate this **MOUNT_OPTIONS=,cached** section.

**RUNNING THE DEVILBOX:**

Yes, if we have come this far without any problems, it means that the fun part begins. First of all, run the Docker application and confirm that “Docker desktop is runnig” by clicking the Docker icon on your taskbar.

Let's get the containers we set up by typing the following command into the terminal.

```
docker-compose up -d php mysql
```
With this command, php, mysql and standard Nginx containers will be built first and then stand up.This process may take some time to install the necessary packages. To see the running containers when the process is complete, type the command:

```
docker ps
```
If our terminal output looks like this, it means that our containers have successfully stood up.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgvkbcrg97rcygczk6f2.png)

If we open our browser and type localhost, we will see the administration panel of devilbox.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3gi475dhpjdd1hd3gl4.png)

You can find many information or perform many operations on this screen.

## LARAVEL PROJECT INSTALLATION

First of all, there are a few things we will do before we set up our project. I want to keep my local projects under **Desktop/project/** directory. The local location of our projects in Devilbox is under the **devilbox/data/www/** directory. If you wish, you can store your projects under this directory or you can use them in different locations like mine. To do this, let's write the following command.

```
ln -s /Users/username/Desktop/projects/Users/username/devilbox/data/www
```
Thus, we will be able to keep the project files in the directory that I have determined, not in the standard directory of devilbox.
To install our Laravel project, let's go to the **/Users/username/devilbox** directory and run “**shell.sh**” for linux and mac users and “**shell.bat**” for windows users to log into the container.

```
./shell.sh
```
If we see the following screen when we run the command, it means that we have successfully logged into the container.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsxdpvw0f7h5khftownp.png)

```
laravel new example 
```

Let's start setting up our Laravel project named example with the command.
After the installation is complete, if you type "localhost" in the browser and browse to the virtual hosts tab from the devilbox administration panel, you will see the root directory of the project and an error message. We need to save it in the hosts file of our computer as projectname.loc with the ip address of 127.0.0.1. For this, we should do the following by stating that it will differ in operating systems:

```
sudo nano /etc/hosts
```
With this command, we open our hosts file, add the following and save and close it.

**127.0.0.1 example.loc**

If we refresh the Virtual hosts tab, we will see that this time it says "ok" instead of the error message.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4az6ex9iz3uw2v93716.png)

Finally, when we open “example.loc” in our browser, we will now see the homepage of our Laravel project.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s66ns0c2miy5h6yf2bcf.png)