VS Code Dev Containers

“This containers revolution is changing the basic act of software consumption. It’s redefining this much more lightweight, portable unit, or atom, that is much easier to manage… It’s a gateway to dynamic management and dynamic systems.” – Craig McLuckie, Google.

Visual Studio Code (https://code.visualstudio.com/)  has a pretty nifty extension called Dev Containers - Visual Studio Marketplace. Through it you can use Docker or Podman to create a development container. In this tutorial we will use Docker (https://www.docker.com/) to create an Ubuntu (https://ubuntu.com/) version 22.04 container via a Windows Subsystem for Linux (https://learn.microsoft.com/en-us/windows/wsl/about) on Windows 10 (https://en.wikipedia.org/wiki/Windows_10).

There is an example of my implementation of a VS Code dev container at https://github.com/mday299/keypuncher/tree/main/C%2B%2B/VSCode/DevContainer.

Install Docker

On Windows 10 install Docker Desktop: https://www.docker.com/products/docker-desktop/. Note that if you are sitting at a desk at your company then Docker Desktop is not necessarily free: https://www.docker.com/pricing/ but it is free for personal use at this time. Then install the Docker VS Code extension: https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker.

Once Docker Desktop is installed and started you should be able to enter the command in PowerShell (https://learn.microsoft.com/en-us/powershell/) or cmd (https://en.wikipedia.org/wiki/Cmd.exe):

docker pull ubuntu

which should pull the image from dockerhub https://hub.docker.com/_/ubuntu.

However, as we’ll see later, this image doesn’t include the Ubuntu packages required to build C++ executables. We’ll change to an image that does support that a little later.

If necessary, configure the VS Code extension for Docker as follows:

Dev › Containers: Docker Path

Docker (or Podman) executable name or path.

docker

Dev › Containers: Docker Compose Path

Docker Compose executable name or path.

docker-compose

Install Dev Containers

Also on Windows 10, install the VS Code extension for Dev Containers at https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers. I used these instructions to configure my dev container: Create a development container using Visual Studio Code Remote Development.

When I installed this container I had to re-install some of my VS Code extensions including the C/C++ Extension pack: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools. This is because the installation pertains to the container vs the standard VS Code installation.

Building C++

The default Ubuntu containers at https://hub.docker.com/_/ubuntu don’t include the packages needed to build C++ code. As such I have elected to use one of the numerous public repos that include those. I have chosen https://hub.docker.com/r/rockdrilla/ubuntu-buildd-helper/tags.

.devcontainer.json

Create a new file called .devcontainer.json and enter the following text into VS Code:

{
    "image": "rockdrilla/ubuntu-buildd-helper:22.04"
}

hello.cpp

Create a new file called hello.cpp and enter the following text into VS Code:

#include <iostream>

//the main function is where a C++ program begins exectution
int main() {

    std::cout << "Hello World" << std::endl;

    //0 means there was no error in the program
return 0;
}

Makefile

Create a file called Makefile and enter the following text into VS Code:

all:
    g++ hello.cpp -o hello

Previous
Previous

Network Sockets

Next
Next

WSL: Red Hat Enterprise Linux 8