Laravel 8 Development with DDEV using the configuration from Laravel Sail

February 07, 2021

Getting Started

By the end of this blog, you should have a running instance of Laravel 8 using DDEV for your local development environment. If you haven't used DDEV before, it's a great tool for managing multiple sites in various PHP platforms and CMS's, including Laravel, Drupal, and WordPress. I happen to be developing on Mac, but developers can use DDEV to work seamlessly across Mac, Linux, or Windows. If you're on a team that is managing multiple projects with developers using different operating systems, then DDEV might be a great fit.

Installing pre-requisites

This tutorial does assume that you already have Docker Desktop installed, as well as the DDEV CLI tool.

Docker Desktop

DDEV

DDEV and Laravel

Create the new Laravel 8 project with Composer

$ composer create-project laravel/laravel ddev-laravel && cd ddev-laravel

Configure and start the project with DDEV

DDEV has a simple command that will walk you through the configuration of your project. In my case, DDEV actually detected the Laravel project type and the public directory as the root of the application without doing anything, which is awesome.

# from /ddev-laravel

$ ddev config

# DDEV will walk you through setup here.
# It will also try to auto-detect your project type and your docroot.

Configuration complete. You may now run 'ddev start'.

Matching Docker configuration with Laravel Sail

If you were to start the app right now, it would work, but for Laravel purists out there, you may want to stay true to the container stack that Taylor provides with Laravel Sail (you can check out his docker-compose.yml here). From this file, I can see we are going to want to change a few things in default DDEV to best match the environment that Taylor recommends:

  • By default, DDEV will use PHP 7.3, and Taylor is running PHP 8.0 ⚡️
  • By default, DDEV will use MariaDB, and Taylor is using MySQL 8.0
  • Taylor provides a Redis container running redis:alpine, which DDEV doesn't provide out of the box, but is super simple to add

Note: Taylor is also providing MailHog, as he did on Laravel Homestead, but DDEV actually provides this out of the box without any additional configuration.

Updating the PHP version and change to MySQL in DDEV's configuration file

Fortunately, DDEV makes this really easy. Simply change the php_version and the mysql_version within .ddev/config.yaml file it just generated. Make sure you also leave the mariadb_version as an empty string:

# config.yaml

...

php_version: "8.0"
mariadb_version: ""
mysql_version: "8.0"

...

Adding the Redis service

To add a new service in DDEV, you can create a new file called docker-compose.redis.yaml like the example in this repo shown here, only changing to redis:alpine:

# ddev-laravel/.ddev/docker-compose.redis.yaml

version: "3.6"

services:
  redis:
    container_name: ddev-${DDEV_SITENAME}-redis
    image: redis:alpine
    ports:
      - 6379
    labels:
      com.ddev.site-name: ${DDEV_SITENAME}
      com.ddev.approot: $DDEV_APPROOT
    volumes:
      - ".:/mnt/ddev_config"
      - "./redis:/usr/local/etc/redis"
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  web:
    depends_on:
      - redis
    links:
      - redis:redis

Note both the ddev- prefix on the container_name, and also the labels assigned to the container

Start the app

Now, I can just start the project with ddev start. The first time you run this command, DDEV might have to retrieve Docker images from the cloud and you will see these images get downloaded through your terminal.

# from /ddev-laravel

$ ddev start

# The first time you run this, DDEV will install the image here
# You can see each container get created

Creating ddev-ddev-laravel-db    ... done
Creating ddev-ddev-laravel-redis ... done
Creating ddev-ddev-laravel-dba   ... done
Creating ddev-ddev-laravel-web   ... done

Successfully started ddev-laravel
Project can be reached at http://ddev-laravel.ddev.site http://127.0.0.1:32785

Running Laravel Artisan from the container

If you're familiar with Laravel Homestead or Laravel Sail, you know that typically you will be running Laravel's Artisan CLI tool from within the container, not from your host machine. DDEV works in a similar way, and provides a ddev artisan command to run that caters specifically to Laravel projects to run artisan in the container:

# from /ddev-laravel

$ ddev artisan

Voila!

From here you should be able to get started using the workflow you always use with Laravel.

Join my mailing list to stay updated with web development news.

About the Author

Jed Darrohn is a software developer that specializes in Laravel, Vue.js, and React. He lives in Denver, Colorado with his Australian Shepherd, Bessie.

© 2021 Jed Darrohn

Sitemap