skip to content
~/bermudev/blog

Microservices with DevContainer in VSCode

/ 3 min read

DevContainers logo

I have recently started working on a new project with microservices and I have come across one of the tools that has impressed me the most in this time. This tool is the DevContainer extension in Visual Studio Code (VSCode), and now I will explain why I think it is indispensable for working in microservices development.

What is DevContainer?

DevContainer is an extension provided by VSCode that allows developers to define and manage development environments within a container. It makes use of Docker’s power to build isolated environments that are uniform across many projects. With it, you can ensure that every member of the team is working with the same dependencies, configurations, and tools, regardless of their local development setup.

Streamlining Microservices Development

As you may know, working with microservices architecture often involves dealing with multiple services, each with its own set of dependencies and configurations. DevContainer provides a solution to this complexity by enabling you to define a unique development environment for each microservice.

And how does it do it? Just with a simple configuration file, with this, you can set up a container with all the necessary tools and dependencies specific to the microservice you are working on. Isn’t that great?

Benefits of Using DevContainer in Microservices Development

  1. Consistency across Microservices: DevContainer ensures that every microservice in your project is developed and tested in an identical environment. This reduces the chances of environment-related issues and makes collaboration between team members much smoother.

  2. Easy Setup and Onboarding: With a DevContainer configuration file included in your repository, setting up a new developer’s environment is really easy. New team members can get started quickly without worrying about installing various dependencies manually and fixing poss

  3. Isolation and Security: As you are running the code inside the container, it provides isolation between your local system and the development environment. This isolation prevents conflicts with system-level dependencies and ensures that your project-specific tools and libraries do not interfere with other projects.

  4. Reproducibility: Having a DevContainer configuration version-controlled alongside your code allows you to easily reproduce DevContainer exact development environment used for each microservice.

How to Get Started with DevContainer

Getting started with DevContainer is straightforward:

  1. Create a .devcontainer folder: At the root of each microservice’s repository, create a .devcontainer folder, just like you would do with the .vscode folder.

  2. Define a DevContainer Configuration: Inside the .devcontainer folder, create a Dockerfile and/or a devcontainer.json file to specify the development environment for the microservice. For example, the Docker file for a simple Flask python server installation could be:

FROM python:3.11.4-bookworm

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD ["python3", "-m" , "flask", "run", "--host=0.0.0.0"]
  1. Customize as Needed: Customize the DevContainer configuration to include the necessary tools, dependencies, extensions, and settings specific to each microservice. An example of this DevContainer configuration is:
{
    "name": "Flask app",
    "build": {
        "dockerfile": "Dockerfile",
        "context": ".."
    },
    "customizations": {
        "vscode": {
            "settings": {
                "python.defaultInterpreterPath": "/usr/local/bin/python",
                "python.formatting.blackPath": "/usr/local/py-utils/bin/black"
            },
            "extensions": [
                "ms-python.python",
                "ms-python.vscode-pylance"
            ]
        }
    }
}
  1. Open in DevContainer: When you open the microservice’s repository in VSCode, it will detect the DevContainer configuration and prompt you to reopen the project in the container. If not, you can always open the command console (Shift + Control + P) and look for the option to rebuild the container.