Running Laravel on Docker is really easy with Kool
Tags: laravel, docker, opensource, kool
Published: 2021-06-22 18:31:04

In just 3 simple steps, you can use **Kool CLI's** [`kool create`](https://kool.dev/docs/commands/kool-create) command to start a new Laravel application running in a local Docker environment. [Kool](https://kool.dev) is an open source dev tool that makes using Docker for local development a lot easier.

Before you start, if you haven't done so already, you need to [install Docker and **Kool CLI**](https://kool.dev/docs/getting-started/installation).

## 1. Create a new Laravel application

```console
$ kool create laravel my-project
```

This command may take a minute or two to complete while [Composer](https://getcomposer.org/) downloads Laravel's vendor dependencies. After installing Laravel, `kool create` automatically runs the [`kool preset laravel`](https://kool.dev/docs/commands/kool-preset) command, which helps you set up your project's development environment using an interactive wizard.

### PHP

```console
? What app service do you want to use  [Use arrows to move, type to filter]
> PHP 8.0
  PHP 7.4
```

### Database

```console
? What database service do you want to use  [Use arrows to move, type to filter]
> MySQL 8.0
  MySQL 5.7
  MariaDB 10.5
  PostgreSQL 13.0
  none
```

### Cache

```console
? What cache service do you want to use  [Use arrows to move, type to filter]
> Redis 6.0
  Memcached 1.6
  none
```

### Package Manager

```console
? What javascript package manager do you want to use  [Use arrows to move, type to filter]
  npm
> yarn
```

---

After selecting your preferences, the latest version of Laravel will be installed and ready for you in your `my-project` folder, along with your new `kool` environment.

![kool preset laravel - output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miifgrnahvz6jjl69xnn.png)

Move into your new Laravel project:

```console
$ cd my-project
```

The `kool preset` command auto-generated the following configuration files and added them to your project, which you can modify and extend.

```diff
+docker-compose.yml
+kool.yml
```

## 2. Update `.env.example`

Next, we need to make a few tweaks to the default **.env.example** file that ships with Laravel in order to match the services in your **docker-compose.yml** file. For example, we need to change the hosts from *localhost* to the appropriate service container name, which are accessible from within Docker.

### Database Services

MySQL 5.7 and 8.0 / MariaDB 10.5

```diff
-DB_HOST=127.0.0.1
+DB_HOST=database
```

PostgreSQL 13.0

```diff
-DB_CONNECTION=mysql
+DB_CONNECTION=pgsql

-DB_HOST=127.0.0.1
+DB_HOST=database

-DB_PORT=3306
+DB_PORT=5432
```

### Cache Services

Redis

```diff
-REDIS_HOST=127.0.0.1
+REDIS_HOST=cache
```

Memcached

```diff
-MEMCACHED_HOST=127.0.0.1
+MEMCACHED_HOST=cache
```

## 3. Run `kool run setup`

Now, spin up your local environment for the first time using the `setup` script in your **kool.yml** file:

```console
$ kool run setup
```

> **Note**: if you're running this command for the first time, Docker may need to download all the container images that are being used, which can take a few minutes, depending on your internet connection.

That's it!

After `kool run setup` has finished, you can access your new Laravel app at [http://localhost](http://localhost).

```console
$ kool run setup

Creating network "my-project_kool_local" with the default driver
Creating volume "my-project_database" with default driver
Creating volume "my-project_cache" with default driver
Creating my-project_cache_1    ... done
Creating my-project_app_1      ... done
Creating my-project_database_1 ... done
```

![kool laravel welcome screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9wrpikyu5gfr1gpy603.png)

## Dive Deeper

* [Read the docs for our Laravel Preset](https://kool.dev/docs/presets/laravel) to learn more about using Kool with Laravel.
* [Go to kool.dev](https://kool.dev) to learn more about the Kool open source project.

{% github kool-dev/kool %}