How to Launch a PHP Project in VS Code Dev Container

Sergey Lisovskiy
3 min readDec 2, 2023

--

As a designer and frontend developer, I often find myself working on various projects. Recently, while working on one of legacy projects, I needed to make updates to a website that was built with PHP. After conducting some research, I discovered that the best option for creating a PHP development environment for my case would be to utilize the VS Code Dev Container feature.

The Dev Container feature in VS Code allows developers to define and configure a development environment using containers. You can define the runtime, tools, and dependencies required for your project, ensuring consistency and reproducibility across different development machines.

Prerequisites:

  1. Visual Studio Code
  2. Dev Containers extension
  3. Docker

To get started with the Dev Container feature in VS Code, you need to create a .devcontainer folder in your project's root directory. Inside this folder, you can define a devcontainer.json file that specifies the Docker image, runtime settings, and any additional configurations required for your PHP project.

devcontainer.json

So, essentially, that’s all 🙃. We can now use php -S inside the container to launch the dev server. But the container also already includes Apache, which is used in my project's production environment. Consequently, I've opted to utilize it and automate the configuration and launch of Apache as well.

In my case, I extended the default configuration by adding some scripts to the postCreateCommand field in the devcontainer.json file.

devcontainer.json

Here is the shell script that I added:

postCreateCommand.sh

These scripts perform the following actions:

  • Grant execute permission to the public_html directory, where my PHP files are located.
  • Remove the default files in the /var/www/html directory.
  • Create a symbolic link from the public_html directory to the /var/www/html directory.
  • Activate the rewrite module in Apache configuration.
  • Turn off PHP error reporting by adding a configuration file no-warn.ini.
  • Start the Apache server.

Great, everything is ready. Now, to seamlessly integrate our configured development environment, run Docker and navigate to the bottom left corner of VS Code, where you’ll find the “Remote Window” button. Click on it, and choose the “Reopen in Container” option.

Selecting “Reopen in Container” triggers VS Code to rebuild the container based on the configurations we’ve set in the .devcontainer folder. Once the container rebuild is complete, you'll find yourself working inside the containerized environment, seamlessly incorporating the specified PHP runtime and the configured Apache server.

Now you can open your web browser and navigate to localhost:8080. Here, you’ll see your PHP application running within the container.

In conclusion, the VS Code Dev Container feature proves to be a convenient and efficient tool for setting up and managing PHP development environments. Its ability to streamline the development process ensures consistency across various machines, making it an excellent choice for PHP project work.

--

--

Sergey Lisovskiy
Sergey Lisovskiy

Written by Sergey Lisovskiy

Designer, frontend developer, architecture lover, vegan, lisovskiy.work

No responses yet