We will see how to develop in VS Code using a Docker container based development environment that involves C++ development using the Kokkos library with OpenMP as the parallel programming backend on the CPU.
- Installation of Visual Studio Code.
- Installation of necessary extensions in VS Code. See below for details.
- Installation of a container management tool like Docker or Podman.
- Opening the directory
devc_hellowin VS Code. - VS Code may automatically ask you to reopen the project in a container. Otherwise, you could manually do so by opening the Command Palette (Ctrl+Shift+P) and entering ">Dev Containers: Reopen in Container".
- If all goes well, all the requirements would be installed including a C++ compiler, CMake and Kokkos. A "build" directory would also be generated by CMake within the C++ project.
- Ubuntu 22.04 is the base image of the container.
- The C++ compiler is GCC, installed via
build-essential. - Kokkos version is 4.6.01, with OpenMP parallel host backend enabled.
The steps are for a system with Ubuntu 22.04 as the OS.
How to install and use Docker on Ubuntu 22.04
Give the user permission to run Docker.
Ensure that you haven’t installed different parts of Docker in different ways (say, some via snap and some via apt) or have multiple installations of Docker (one via snap and one via apt). This will lead to error while trying to rebuild the container from inside VS Code. A few references about this point.
Docker is the default container management tool expected by VS Code. Make sure the Docker daemon is running in order to use Docker.
Docker requires the daemon to be running in the background. In order to start the daemon, one has to either be administrative privileges or have rootless mode enabled. Podman presents a solution for a user to use a system in which the necessary software already installed (VS Code and Podman), without administrative privileges.
See here for further information on using Podman with VS Code. The steps involved are:
- Installation of Podman.
- In VS Code: In Settings, changing the setting
Dev > Containers: Docker Pathfromdockertopodman.
See other resources also about installing and using Podman.
Ensure that you have stopped the Docker daemon, in case you have Docker also installed in your system.
User namespaces have to be enabled in order to fix the following error:
cannot clone: No space left on device
user namespaces are not enabled in /proc/sys/user/max_user_namespaces
Error: cannot re-exec process
The relevant files are Dockerfile and devcontainer.json in the directory .devcontainer. Dockerfile describes the Docker image to be built. devcontainer.json is used to describe the settings of the Dev Container beyond what is in the image. For example, the settings needed to build the image (like the network connection to access the internet), the settings needed to run the container and what extensions to install in VS Code while working inside the container.
The user needs to install the following extensions in host VS Code:
- “Dev Containers” extension. Note that the “Remote Development” extension contains “Dev Containers” too.
- “Docker” extension. Currently this extension will be installed in the Dev Container environment also. Note that the "Docker" extension is going to become "Container Tools" extension.
Note: “C/C++ Extension Pack” will be installed in the Dev Container environment. No harm if they are both in the host and the remote.
There is a simple C++ project that links with Kokkos to be built and run in devc_hellow. One can see in the CMakeLists.txt file that CMake expects Kokkos to be installed, in order to build the project. The container image takes care of this requirement.
In devcontainer.json, one can specify a "postStartCommand". For e.g., it is defined as "cmake -DKokkos_ROOT=/opt/kokkos-install -DKokkos_ENABLE_OPENMP=ON -B build" in our case. This ensures that a build directory is generated by CMake. See below for some observations with Docker and Podman regarding "postStartCommand".
-
Currently, "postStartCommand" works only with Docker and not with Podman. One gets an error like the following with Podman:
Error: error parsing environment variables: name "\afbfa9712-93dc-4bca-8ecc-99ffb315ae27\x1b]0;vscode@mdlspc111.extra.cea.fr: cat /proc/self/environ\aREMOTE_CONTAINERS_IPC" has white spaces, poorly formatted name [147463 ms] postStartCommand from devcontainer.json failed with exit code 125. Skipping any further user-provided commands. -
Podman takes more time to build and run the container. Probably, it takes more time to restart a container also.
-
With Podman, VS Code disconnects and reconnects to the container from time to time.
Probably, these issues with Podman can be resolved by using a more recent version of Podman. For example, VS Code recommends version 5+. However, Podman offers much better support for Red Hat distributions like Fedora.
- In order to enable networking while building the container, define
"build"as"build": { …, "options": ["--network=host"], …}. This may be needed in order togit clonethe Kokkos repository. Also, note that“runArgs”: [“--network=host”]is for enabling network connection while running the container. - Setting the environment variable
CPATHasCPATH="/opt/kokkos-install/include:$CPATH"helps with IntelliSense recognizing Kokkos header files. - In the Command Palette enter ">Dev Containers: Rebuild Without Cache and Reopen in Container" whenever you need to rebuild the container after making changes to either
devcontainer.jsonorDockerfile.
- Kokkos functions are incorrectly flagged by IntelliSense.
- Defining a non-root USER in
Dockerfile. - "postStartCommand" vs "postCreateCommand" to generate
builddirectory. - Try installing the following extensions in VS Code to check for a better experience with Podman.
- "Container Tools" and "Pod Manager". Note: These extensions have been released relatively recently.