Dev Containers for C++ in Visual Studio
We are happy to share with you that we have added Dev Container support In Visual Studio 2022 17.4 for C++ projects using CMake Presets.
Containers are a great way to package up everything for running an application. Through a Dockerfile all prerequisites are captured so that there is a consistent runtime environment anywhere the container is deployed and run Dev Containers expand this concept to capture everything necessary for developing and building an application in the container. We have been observing for some time that containers are becoming more popular in the C++ community for providing reproducible build environments.
Visual Studio Code has had support for Dev Containers for some time now. Beginning in Visual Studio 2022 17.4 you can now use Dev Containers for your C++ projects as well.
Trying Dev Container support
You will need to have Docker Desktop installed on Windows to use this capability. This is part of the Linux and embedded development with C++ workload., so make sure that is selected in your installation.
We have enabled this capability for projects that use CMake Presets. You can clone this sample to get started.
git clone email@example.com:robotdad/visualstudio-devcontainer-cpp.git
This project is the same as File New CMake Project in Visual Studio with one difference. There is an additional folder .devcontainer that contains the Dockerfile and devcontainer.json which together define the Dev Container to start.
On opening this folder in Visual Studio you will be prompted to reopen the folder in a container.
On first use when the container is started the backing image is downloaded. You can see progress of the startup in the new Dev Containers output window.
When startup is finished CMake cache generation starts, when it is done you can edit and debug as normal.
You can switch between your Dev Container and other options using the Target System drop down in the tool bar.
Dev Containers have a few options under Tools Options. Most importantly you can control whether or not you want to be prompted to reopen in a container. You can also set a remote host to be the location to run containers, the remote host must be running docker services.
The feature uses Linux containers. We are not supporting Windows containers with this mechanism, as such this is not relevant for traditional Windows C++ development using MSVC. The is intended for cross platform development.
A backend service for Visual Studio Code runs within a Dev Container, this is not the case for Visual Studio. Visual Studio manages the lifecycle of Dev Containers it uses as you work, but it treats them as remote targets in a similar way to other Linux or WSL targets. The devcontainer.json configuration file may contain some VS Code specific information, like needed extensions, which are not relevant to Visual Studio. Those are ignored by Visual Studio so it is expected that there will be common Dev Container definitions used with either environment.
Do not open the same folder in Visual Studio and VS Code at the same time. This will result in them connecting to the same container which will result in conflicts between CMake running in two contexts against the same folder.
Send us your feedback
We hope that these new capabilities will enable you to choose Visual Studio for your C++ development needs and make you more productive. We are very interested in your feedback to continue to improve this experience. The comments below are open, or you can find us on Twitter (@VisualC), or via email at firstname.lastname@example.org.