Kool.dev Blog

Kool.dev resources, tutorials, news and tips

Laravel in Docker: simple and fast development environment

2021-01-28 2 min read tutorials

Creating a new Laravel application with kool create makes it easy to leverage Docker like nothing else in 3 simple steps. Give it a try!

1. Creating new Laravel application

Requirements: you must have previously installed the kool CLI.

$ kool create laravel my-app

The command above may take a minute or two to run - time for Composer to download all of Laravel vendor dependencies. After finished installing, kool preset will be automatically executed and the wizard will ask you about how you want to setup your development environment for that project. You will be inquired for:

  • PHP version
? What app service do you want to use  [Use arrows to move, type to filter]
> PHP 7.4
  PHP 8.0
  • Database
? What database service do you want to use  [Use arrows to move, type to filter]
> MySQL 8.0
  MySQL 5.7
  PostgreSQL 13.0
  none
  • Cache
? What cache service do you want to use  [Use arrows to move, type to filter]
> Redis 6.0
  Memcached 1.6
  none

Package manager:

? What javascript package manager do you want to use  [Use arrows to move, type to filter]
  npm
> yarn
? What composer version do you want to use  [Use arrows to move, type to filter]
  1.x
> 2.x

After picking your preferences we will have a fresh installation of Laravel (latest version) in the folder my-app with kool environment almost ready to go!

kool preset laravel - output

2. Updating .env

Next we need to make a few tweaks to default .env that ships with Laravel. We must change the hosts from localhost to their containers name (accessible from within Docker).

-DB_HOST=127.0.0.1
+DB_HOST=database
-DB_USERNAME=root
+DB_USERNAME=kool
-DB_PASSWORD=
+DB_PASSWORD=secret

-MEMCACHED_HOST=127.0.0.1
+MEMCACHED_HOST=cache

-REDIS_HOST=127.0.0.1
+REDIS_HOST=cache

Pro tip: apply these changes not only to your .env but also to .env.example so future clones/other developers will be ready for running it out of the box.

3. kool start

Last step, fire up the local environment:

$ cd my-app/
$ kool start

Note: if you are running kool start for the first time, it is possible that Docker will have to download all of the container images that will be used, in that case it can take a few minutes depending on your internet connection.

That is it! After kool start has finished you can checkout your fresh Laravel installation at http://localhost.

$ kool start
Creating network "web_kool_local" with the default driver
Creating web_app_1      ... done
Creating web_database_1 ... done
Creating web_cache_1    ... done

kool start laravel - output