<!--
  canonical-url: https://www.avonture.be/blog/php-devcontainer
  published:     2024-02-23
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# Install a PHP Docker environment in a matter of seconds

> Quickly set up a PHP Docker Dev Container environment for VSCode. Get Composer and pre-installed static analysis tools like PHP-CS-Fixer and Rector instantly.

---
![Install a PHP Docker environment in a matter of seconds](/img/v2/devcontainer.webp)

**TL;DR**

This is the short, quick-start version of the full devcontainer article: download the `php_devcontainer` skeleton with `curl`, open the folder in VSCode, and click "Reopen in Container" to get a ready-to-use PHP environment with Composer, PHP-CS-Fixer, PHPCS/PHPCBF, SonarLint and Rector preinstalled — for both brand-new projects and existing codebases.

This article is the very short and straightforward version of [PHP development in a devcontainer with preinstalled code quality tools](/blog/vscode-devcontainer). If you just want to follow a very few steps and get your PHP environment, this article is for you.

**By following the steps described in this article, you'll obtain**

1. A Docker PHP environment so you can code using vscode,

2. The PHP `composer` package manager installed,

3. A few static analysis tool like `php-cs-fixer`, `phpcs`, `phpcbf`, `sonarlint` and `Rector` installed

4. A devcontainer ready-to-use environment.

Just follow the guide...

> **The aim of this article is to create a development environment (aka devcontainer):**
>
> Please refers to my other articles about [Docker](/blog/tags/docker) to see how to Dockerize the application and be able to run the application using [Apache](/blog/tags/apache) and a database service like [PostgreSQL](/blog/tags/postgresql).

## What the environment gives you

Here is PHP-CS-Fixer reformatting a file, inside the container, a couple of minutes after the download — nothing installed on my host machine, no PHP, no Composer, no extension to configure:

![PHP-CS-Fixer](./images/php-cs-fixer.webp)

The same is true for [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) (`phpcbf.phar`), [Rector](https://github.com/rectorphp/rector) and SonarLint: they're already in the image, already configured through the `.config` folder shipped with the skeleton. You'll find the exact commands in the *You are ready to use tools* section below.

Two ways to get there, depending on whether you're starting from scratch or adding this to an existing codebase.

## You have nothing and wish to create a fully new project

### 1. Install the php\_devcontainer skeleton

For this blog article, I'll create a temporary folder in `/tmp/devcontainer_php`. Don't hesitate to update the path to something like `~/my_project` to match your need.

```bash
$ mkdir /tmp/devcontainer_php && cd $_
$ curl -LOJ --silent https://github.com/cavo789/php_devcontainer/archive/refs/heads/main.tar.gz
$ tar -xzvf php_devcontainer-main.tar.gz --strip-components 1 && rm -f php_devcontainer-main.tar.gz
```

> **Download a specific version:**
>
> If you don't want to download the latest version but a given one, specify the tags like: `curl -LOJ --silent https://github.com/cavo789/php_devcontainer/archive/refs/tags/1.0.0.tar.gz`.

You've thus downloaded code from my <https://github.com/cavo789/php_devcontainer> repo into your folder.

### 2. Open VSCode

Start VSCode on your machine:

```bash
$ cd /tmp/devcontainer_php
$ code .
```

### 3. Switch to the Dev Container

VSCode will automatically suggest opening a Dev Container. Click on the `Reopen in Container` button.

![VSCode automatically suggests opening a Dev Container](./images/vscode_starting.webp)

If you don't have this popup, just press `CTRL`+`SHIFT`+`P` to open the **Command Palette** and search for `Dev Containers: Rebuild and Reopen in Dev Container` and click on it.

VSCode will now take a while to build the Docker image then start a Docker container before you can work. It can take a few minutes depending on the speed of your machine.

### You are ready to use tools

Press `CTRL`+`ù` to open the **Terminal** pane and type there `/usr/local/bin/php-cs-fixer.phar fix --config /var/www/html/.config/.php-cs-fixer.php index.php` to run [PHP-CS-Fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) — that's the command that produced the screenshot at the top of this article.

You can also run `/usr/local/bin/phpcbf.phar --standard=/var/www/html/.config/phpcs.xml /var/www/html/index.php` for reformatting your code using PHP\_CodeSniffer.

You can run Rector too by running `vendor/bin/rector process index.php --config .config/rector.php`.

As you can see, in just three actions you've downloaded and installed a functional PHP environment using Docker and VSCode.

## You already have an existing project

In a real-life use case, you already have a PHP project.

Start a terminal and open the folder where you've your codebase.

For this blog post, I'll download a very old learning Laravel codebase I've done years ago:

```bash
$ cd /tmp
$ git clone https://github.com/cavo789/laravel_todos.git
```

So, my project will be in folder `/tmp/laravel_todos`.

In my project folder, now, I'll run:

```bash
$ cd /tmp/laravel_todos
$ curl -LOJ --silent https://github.com/cavo789/php_devcontainer/archive/refs/tags/1.0.0.tar.gz
$ tar -xzvf php_devcontainer-1.0.0.tar.gz --strip-components 1 && rm -f php_devcontainer-1.0.0.tar.gz
```

This will download the PHP devcontainer files in my project.

I will run `code .` to open VSCode and, like before, VSCode will suggest opening the folder in a Dev Container. Let's do this.

And voilà, I've Dockerized my PHP coding environment. In my case, I've made `Laravel Todos` in 2018, using PHP 7.1.3 and Laravel 5.x; an eternity then.

And like in the previous chapter, I can run the commands below to fix coding style issues and fixes all files in my repo:

```bash
$ /usr/local/bin/php-cs-fixer.phar fix --config /var/www/html/.config/.php-cs-fixer.php .
$ /usr/local/bin/phpcbf.phar --standard=/var/www/html/.config/phpcs.xml .
```

And I **will be able** to run Rector to refactor my entire codebase:

```bash
$ composer require rector/rector --dev
$ vendor/bin/rector process . --config .config/rector.php
```

but in my case, I can't right now because my Laravel Todos is an old one and I need first to make some changes to my `composer.json` file (upgrading PHP and Laravel versions first).

## Conclusion

A `curl`, a `tar` and a click on `Reopen in Container`: that's the whole distance between an empty folder and a PHP environment where the quality tools are already there, at the same version for everyone who clones the repository. Nothing has been installed on your machine, and deleting the folder deletes the environment.

Same recipe, other stacks: [Docker - Python devcontainer](/blog/docker-python-devcontainer). And when the time comes to ship, [One Docker Image for Production and Devcontainers - The Clean Way](/blog/docker-prod-devcontainer) shows how to keep all this tooling out of your production image.
