DEV Community

Cover image for Try MongoDB and Laravel in 1-click via GitHub Codespaces
Hubert Nguyen for MongoDB

Posted on

Try MongoDB and Laravel in 1-click via GitHub Codespaces

Cloud-based development environments can be great platforms to make it easier for new collaborators to get up to speed and contribute to code with minimal setup time. We've explored ways to distribute our tutorials, demos, and workshops, and GitHub Codespaces has emerged as a favorite among many great options.

Why using containers boosts PHP developers’ productivity

Building a reliably configured PHP development environment that a team of developers and even external contributors can share is not always an easy task, especially if there are configuration updates along the way.

It is quite common to hear about developers wasting time because not everyone is using the same configuration, and "it works on my machine" is still a thing in 2023.

The thought of "setting up" a new project to perform a small, short-term test or code contribution can also deter casual contributors from participating in a project, which is a pity since many developers could add valuable code and feedback if given the chance.

Using a containerized development environment reduces or nearly eliminates these problems, but doing so in a cloud environment makes things even more seamless. The easier it is to get up and running, the more people will participate. With a tool like Codespaces, you can have contributors up and running in one click and start debugging and contributing code quickly.

Case study: our Laravel tutorial repo on Codespaces

We recently published a tutorial that explains how to use MongoDB inside a Laravel app. Our goal with the tutorial is to show how the Laravel MongoDB integration works and that it is even possible to smoothly branch out from the native Eloquent ORM into MongoDB's native query API to exploit the latest MongoDB features fully.

Typically, such PHP tutorials start with "How to install PHP and install our MongoDB PHP driver." However, wouldn't it be more engaging if the reader could simply deploy the app in one click and follow along or look at the running app?

This logic would also be true for in-person workshops where "setup time" can put a significant dent in the overall schedule if a few attendees fall behind because of misconfigurations. Having a container-based environment reduces the odds of something like that happening.

  1. Click and launch a new codespace
    Launch in CodeSpaces

  2. Visual Studio in the virtual environment
    Visual Sutio in CodeSpaces

  3. The laravel app is already configured and running
    Laravel 10 app

Our Codespaces configuration

We'll use our laravel-mongodb-tutorial repository as an example here, and the README.md file explains how to launch that minimal Laravel application in one click using Codespaces.

Codespaces is built to run Dev Containers, an open standard for Development Containers. The Dev Container will reference a Docker build file, which describes the software and services our app is running on. It also defines things related to our development environment, including IDE plugins, network ports, and more.

This configuration is stored in our development container configuration's /.devcontainer/devcontainer.json file.

In our Laravel app repository, we placed our Docker configuration in /.devcontainer/docker-compose.yml. The .docker subdirectory contains two folders named nginx and php with specific configuration files for both.

The docker-compose.yml file describes how our microservices should be configured, and to make things more readable, we use variables from the .devcontainer/.env file.

Here, we define the usual services and install the MongoDB PHP Driver. You can look at the .docker/php/Dockerfile file to get all the details, but the driver installation command is:

RUN bash -c '[[ -n "$(pecl list | grep mongodb)" ]]\

 || (pecl install mongodb && docker-php-ext-enable mongodb)
Enter fullscreen mode Exit fullscreen mode

The NGINX configuration is minimal but could be customized to match specific production environments. Sometimes, it makes sense to develop your app using PHP's built-in webserver, which would make things simpler. However, more complex apps might rely on URL rewrites and more from a web server like NGINX or Apache.

In our current Codespaces configuration, we want developers to have a running Laravel application immediately. In our .devcontainer file, we select the php container as the service we want to develop. As such, contributors won't be able to log into the other containers as docker admins.

Alternatively, it is also possible to configure Codespaces to launch a basic Linux container and then ask the contributor to manually run "docker compose up" to run Docker inside Docker.

Practice with PHP Virtual Event

practice with php event image

We have also explored and implemented running Docker inside Docker to create a fun PHP coding quest with Wilco, and you can see how that version works when the quest goes online.

Starting October 30, PHP developers can join our Practice with PHP Virtual Event to learn how the MongoDB/Laravel integration works, interact live with MongoDB's PHP driver team, and hear great insights from a surprise guest from the PHP community. You won't want to miss these.

And, of course... there's SWAG to win for those who complete the quest the fastest!

Conclusion

Containers are extremely valuable tools for developers using stacks based on multiple services, each with potentially complex configurations. We highly recommend learning how they work, starting with the basics of Docker containers and building from there.

Once comfortable with deploying your PHP development environment using containers, you can configure cloud-based development environments to attract testers and contributors who can focus their valuable time and efforts on what matters most: improving your application.

The next step after that is to use containers' flexibility to (automatically) test your applications on dozens if not hundreds of possible PHP stacks and versions if your application is destined to be hosted in a PHP environment you don't directly control. That will take your application's quality and reliability to the next level.

Top comments (0)