This is supposed to become a series of three parts, so I’m writing down the titles of the next parts here to incentivise me to write them
- What’s a devContainer and what is it good for?
- How to setup a devContainer with Visual Studio Code
- Remote development with devContainers
This first article is an introduction to devContainers.
What is a devContainer?
You’ve probably heard about Docker containers and how you can package an application with the operating system to make it run on any hardware.
A devContainer is exactly that but for development environments. You write code, run and debug it inside a Docker container. The devContainer has all the tools you’ll need for your development, Git, dotnet, nodejs, you name it.
What problems does it solve?
Have you ever tried to onboard a new developer to the project and spent a day trying to get the development environment to run on his/hers machine? Was it the wrong version of nodejs installed or did they miss a Windows update?
A devContainer solves this by installing the correct versions of all dependencies from the Dockerfile.
Have you developed an application using the latest technologies, .NET 5 and then a year later when you are just going to fix an issue the application no longer builds because you have .NET 6 installed on your machine and there were some breaking changes between versions?
With devContainers you will stay on .NET 5 until _you_ decide it is time to upgrade the code base. The application will not stop working because you switched machines or the tools got outdated.
Have you ever had your development environment stop working because you share database with the team and someone else ran a database migration that you haven’t got yet?
With devContainers it is easy to setup dependencies like databases in the same Docker instance so everyone on the team has their own local database without any messy installations.
What applications can be devContained?
All applications that are targets for Docker could be using devContainers for development
- Expo Apps
Applications that doesn’t work as well with devContainers are Desktop, iOS and Android applications.
What tools are required?
The definition for a devContainer is written in a file called
.devcontainer/devcontainer.json. This is usually accompanied with a
docker-compose.yml and various setup scripts.
In order to run the devContainer on your local machine you need to have Docker Desktop 2.0+ setup.
Visual Studio Code has the best integration with devContainers as of yet, and you’ll hardly notice that you’re working inside a Docker container.
Okay, but isn’t it weird?
No, you will hardly notice that you’re working inside a Docker container.
- A Docker container is not a virtual machine. There is almost no performance penalties of working inside a Docker container.
- The source tree is shared between the host and the container, so you can work with your code files just as normal.
- Git credentials are automatically forwarded to the container so you do not need any extra authentication for your devContainer.
- When starting your application inside a container, vscode will automatically forward the port to the host so you can see the result on your machine. Just open a web browser to localhost:5000 as you usually do and it works like magic.
This was a short introduction to devContainers. Setting one up for your project is very easy and what we’re going to look at in part 2.