Try MongoDB and Laravel in 1-click via GitHub Codespaces
Tags: php, laravel, containers, mongodb
Published: 2023-10-24 15:24:06

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](https://www.mongodb.com/developer/?utm_campaign=devrel&utm_source=cross-post&utm_medium=cta&utm_content=learn-with-php&utm_term=hubert.nguyen), and GitHub [Codespaces](https://github.com/features/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](https://www.mongodb.com/developer/languages/php/laravel-mongodb-tutorial/?utm_campaign=devrel&utm_source=cross-post&utm_medium=cta&utm_content=learn-with-php&utm_term=hubert.nguyen). Our goal with the tutorial is to show how the [Laravel MongoDB integration](https://www.mongodb.com/compatibility/mongodb-laravel-integration?utm_campaign=devrel&utm_source=cross-post&utm_medium=cta&utm_content=learn-with-php&utm_term=hubert.nguyen) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtdr9gyd9mi5da95bz3s.png)

2. Visual Studio in the virtual environment
![Visual Sutio in CodeSpaces](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cjr8sjfzocrd3bmcg5s.png)

3. The laravel app is already configured and running
![Laravel 10 app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xvb73k3bv8az5yylmts.jpg)

##Our Codespaces configuration

We'll use our [laravel-mongodb-tutorial](https://github.com/mongodb-developer/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](https://containers.dev/). 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](https://www.mongodb.com/docs/drivers/php/?utm_campaign=devrel&utm_source=cross-post&utm_medium=cta&utm_content=learn-with-php&utm_term=hubert.nguyen). 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)
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn83smqq9k6d3n77souz.jpg)

We have also explored and implemented running Docker inside Docker to create a fun PHP coding quest with [Wilco](https://www.trywilco.com/customerexperience), 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](https://www.mongodb.com/events/practice-with-php?utm_campaign=devrel&utm_source=cross-post&utm_medium=cta&utm_content=learn-with-php&utm_term=hubert.nguyen) 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](https://www.mongodb.com/compatibility/docker?utm_campaign=devrel&utm_source=cross-post&utm_medium=cta&utm_content=learn-with-php&utm_term=hubert.nguyen) 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.