PureDevOps Community

How to setup docker based local/dev environment for Laravel application development

The Laravel solution

Laravel is an open-source MVC framework for PHP. It’s comprehensive in the sense that it also includes things like an Object Relational Mapper (ORM), direct database access, a packaging system, and more. Laravel can do a lot. And in order to experience it, you really need to have at least a database when you’re getting started. Normally this would require the user to install not just PHP, but a database as well - usually MySQL. That’s a significant ask when a user is simply trying your framework on for size.

Laravel addresses this with containerized dev environments and a tool called Sail. To get started from scratch with Laravel, a MySQL Server, and a Redis Cache, you only have to run a single command…

    curl -s "https://laravel.build/example-app?with=mysql,redis" | bash

This creates a new project with a docker-compose file. This file sets up three containers - an application container, a MySQL container, and a Redis container. You don’t have to know anything about containers or any of those three services. Sail abstracts all of this away for you. You then execute the Sail command to spin up the environment…

    ./vendor/bin/sail up

The sample application just runs. No installing PHP. No Laravel. No dependency resolutions steps. Just immediate success.

I specified that our project has a MySQL Server and a Redis Cache, so we actually get three containers when the project spins up. We can see that using the Docker extension for VS Code.

These containers are networked together so that we can call the MySQL or Redis cache containers from the app container.

If you connect an interactive terminal to the sail-8.1/app container, you’ll see your project in the /var/www/html folder. Docker “mounts” the project from your machine into the container, so any changes you make while developing are reflected in the application when you refresh.

Adding Remote Containers#

Support has also been added for the Remote - Containers extension. To add the proper dev container configuration to this project, you can scaffold the same project and add the &devcontainer flag.

    curl -s "https://laravel.build/example-app?with=mysql,redis&devcontainer" | bash

Note that if you want to add a devcontainer to an existing Sail/Laravel project, you can do that by running php artisan sail:install --devcontainer.

This creates the same project configuration, but will include a .devcontainer folder. VS Code will automatically detect that folder and prompt you to reopen the project in a container thereby skipping the required sail up step.

VS Code attaches to the container, so you are developing within the container environment as opposed to your local one. You’ll know that because the Remote Indicator in the lower left-hand corner of VS Code tells you so…

Developing in the container as opposed to outside of it has some distinct benefits.

Development context mirrors app context#

When connected to the container, the context you are developing in is the same as the one where the application is running. So your terminal becomes the terminal of the container…

The Remote - Containers extension also gives you a more complete view of what’s going on, such as which ports are forwarded - just in case you forget where your application is running.

The Laravel application starts automatically, and the application logs are piped to the container logs. Since you probably want to see what’s going on in the application, the Remote - Containers extension provides a new view in VS Code, where you can see all running containers, as well as connect to stream container logs.

Automate the dev environment setup#

The best possible developer experience is going to include customizations for the editor. This includes settings for the editor itself, and any extensions or other support that needs to be added to the out-of-the-box experience.

For VS Code and Laravel, extensions are suggested in the devcontainer.json, but commented out so that they are not installed automatically. This allows the user to pick from a set of already identified extensions instead of having to go hunt for the right way to configure their editor.

    ...
    "extensions": [
        // "mikestead.dotenv",
        // "amiralizadeh9480.laravel-extra-intellisense",
        // "ryannaddy.laravel-artisan",
        // "onecentlin.laravel5-snippets",
        // "onecentlin.laravel-blade"
    ],

Ref: code.visualstudio.com