Skip to content

Dev Containers

Thomas Hahn edited this page Nov 2, 2025 · 7 revisions

The Dev Containers extension allows us to use a docker container as a full-featured development environment. The source code can either be mounted into the container from the local filesystem or copied or cloned into it once it is running.

Install the extension

To install/enable the extension, follow the same steps as in the previous sections.

We also install the Docker extension (although it is not strictly necessary).

devcont_extensions

Get started

Docker container

In order to use the Dev containers extension, we have to build a docker container beforehand or at least provide a Dockerfile.

For our toy repository, we use the following Dockerfile based on Ubuntu 24.04.

The container can be built by running

cd moderncpp_vscode.src/vscode
docker build -t moderncpp:vscode .

from the terminal.

To take a look around in the container, execute

docker run -it --rm moderncpp:vscode

devcontainer.json file

The devcontainer.json file can be used to specify

  • the container image,
  • the VS Code extensions for the container,
  • the VS Code settings for the container,
  • how to mount/copy/clone the workspace and
  • various other things.

Create a new directory .devcontainer in your local workspace and copy the devcontainer.json file:

mkdir .devcontainer
cp moderncpp_vscode.src/vscode/devcontainer.json .devcontainer/
devcont_json

You should not have to adjust any settings there.

Let's take a look at the most important ones:

  • "image": "moderncpp:vscode": The container image. We use the one that we have built in the previous step.
  • "customizations": { "vscode": { "extensions": [ ... ] } }: The VS Code extensions that we want to use in the container.
  • "customizations": { "vscode": { "settings": { ... } } }: The VS Code settings for the container. We can mostly use the local user settings except for some adjustments to path variables.
  • "workspaceMount": "source=${localWorkspaceFolder},target=/home/moderncpp/moderncpp_vscode,type=bind": Specifies that we want to mount our local workspace folder in the container at /home/moderncpp/moderncpp_vscode. Since we mount the workspace, any changes we make to our workspace in the container will also apply to our local workspace.
  • "workspaceFolder": "/home/moderncpp/moderncpp_vscode": Workspace folder that should open when connecting to the container.

Open the folder in the container

Now we are ready to start our development container.

  • Open your local workspace folder moderncpp_vscode (if it is not opened already)
  • Run >Dev Containers: Reopen in Container

If successful, you should see something like

devcont_running

in the bottom left corner.

Container settings

To check/change the VS Code container settings,

  • Run >Preferences: Open Remote Settings (JSON) (Dev Container: vscode @ desktop-linux)

Extensions

To install/enable/disable extensions in the container,

  • Go to Extensions in the activity bar
devcont_extension_bar

Configure CMake

Before running CMake, we have to comment out our project settings in .vscode/settings.json since they are only valid for the local machine:

devcont_project_settings

Then we can follow the same steps as in CMake Tools.

CMake Tools should now configure your project and create a new build directory (./build.Clang-18.1.3-Debug in our case):

devcont_configure

Note: This is where using presets instead of kits would be an advantage. One could simply define different presets for the local and container setup.

Build and Run

  • Run >CMake: Build

This results in a compilation error since we are using Clang 18.1.3 which does not support std::print yet. To fix this, we simply fall back to std::cout:

  • Open ./examples/ex3.cpp
  • Change its content to
#include <iostream>

int main() {
    std::cout<< "Hello world!" << std::endl;
}
  • Run >CMake: Build again

Now everything should compile and you can run tests and examples just like you would locally:

devcont_run_tests

clangd

To check that clangd works as expected, we open again ./src/linalg.cpp and hover over nda::matrix.

clangd should give us some information on the matrix type:

devcont_clangd

Debugging

CodeLLDB

To debug our code in the container with CodeLLDB, we just have to add a new configuration to the launch.json file in the .vscode folder:

devcont_launch_json

Then we can follow the steps described in the Run the debugger section:

  • Click on Run and Debug in the activity bar
  • Click the green play icon next to Debug ex2 devcont
  • Start debugging
devcont_debug

CMake Tools + Microsoft's C/C++ extension

On macOS, using CMake Tools in combination with Microsoft's C/C++ extension in a container for debugging purposes seems to be not so straightforward (at least on the author's machine).

This might be different on your machine and you can just try to follow the same steps as explained on the debugging page.

Clone this wiki locally