This blog aims to introduce the minimum efforts to set up a C++ environment with Docker and VS Code. Hope it could be helpful for beginners.

Why Docker and Visual Studio Code

I am teaching “Introduction to C++” this quarter. In previous quarters, students are suggested to use Visual Studio or Xcode as the integrated development environment(IDE) when learning C++. However, I feel IDEs are not the best tools for beginners to learn C++ (or programming in general), due to the following reasons.

  • Visual Studio and Xcode compilers could behave differently in some cases. This leads to some trouble with grading. Ideally, the students can use the same compiler when learning and doing homework.
  • IDEs create project files, e.g., .sln files in Visual Studio, which could be confusing for beginners.
  • It also takes time for beginners to get familiar with the user interfaces. Even creating a new project or opening an existing project could be a nightmare for beginners.

I would prefer to teach beginners to write their code in editors, then compile and run their programs with command lines. Docker should be a good choice to set up the compiler, due to the following reasons:

  • All the students can easily set up the same environment and the same compiler, no matter what operating system they are using.
  • The environment/compiler is flexible and easy to switch. This could be helpful if one wants to compare different versions of C++.
  • The students want to learn other languages, they can also set up the environment easily with Docker.
  • Docker is almost essential in modern software development. It’s beneficial for beginners to try it early.

Then comes the editor. Perhaps the most popular editor nowadays is Visual Studio Code (VS Code). With remote development extensions, VS Code works perfectly with Docker containers.

Set up Docker

Install Docker

You can install Docker following the official website.

Pull Image

In your Terminal(Mac/Linux) or PowerShell(Windows), run

docker pull gcc:12.2.0

Here we download an image with gcc compiler in it. The tag 12.2.0 stands for the version of gcc, which is the latest one when I write this blog. You can check here for more information about versions.

Run Container

In your Terminal(Mac/Linux) or PowerShell(Windows), run

docker run -d -t --name gcc -v /path/to/your/folder/:/workspace/ gcc:12.2.0

Please replace /path/to/your/folder/ with the path where you want to write your code. For example, you can replace it with ~/Documents/ for Mac/Windows system.

Here we create and run a container with our image gcc:12.2.0 as the “blueprint”.

  • -d stands for detached mode so that we can run the container in the background.
  • -t stands for allocating a pseudo-TTY so that the container will keep running.
  • --name gcc means naming the created container as gcc. You may change the name when creating other containers.
  • -v /path/to/your/folder/:/workspace/ means mounting the volumn, so that /workspace/ will be linked to /path/to/your/folder/ in your computer.

Optionally, you can run

cd /path/to/your/folder
docker run -d -t --name gcc -v $(pwd):/workspace/ gcc:12.2.0

Here $(pwd) will get your current path.

More options for docker run can be found in docker docs.

Validation

Now, in your Terminal(Mac/Linux) or PowerShell(Windows), run

docker ps

You should see something like this:

CONTAINER ID   IMAGE        COMMAND   CREATED       STATUS       PORTS     NAMES
xxxxxxxxxxxx   gcc:12.2.0   "bash"    xxxxxxxxxxx   xxxxxxxxxx             gcc

So we know that the container is running.

Set up Visual Studio Code

Install Visual Studio Code

You can install VS Code following the official website.

Install Remote Development Extensions

Follow the official website to install the Remote Development extension pack.

Attach to a Docker Container

Press F1, type and select Dev Containers: Attach to Running Container, then select the container we just created. Your VS Code will be attached to the selected container. Now if your open /workspace/ in VS Code, you can see the contents in /path/to/your/folder/ in your computer. You can view /workspace/ as an alias of /path/to/your/folder/.

Run Compiler

Now you can create a file main.cpp in /workspace/ in VS Code, and write your code here. From the menu bar of VS Code, select Terminal/New Terminal, you will create a Terminal where you can run command line commands. You can compile by running

g++ main.cpp -o out

and run the executable file out by running

./out

Optional: C++ extension for VS Code

Follow the official website to install the C++ extension for VS Code. After you install the extension, you will have syntax highlighting, smart completions, error checking, etc. in a .cpp file.