Hi my name is Julia, I'm a program manager on the Visual C++ team at Microsoft
and today i'm going to show you how to create and attach to a remote development container
in Visual Studio Code for your C++ projects. Let me quickly show you what the environment
setup looks like when you are developing inside a container in Visual Studio Code. So you'll open
Visual Studio Code on your local machine. So in this demo I'll be opening it on my Macbook Pro
and that's where the VS Code client is running that's where my source code lives on my Macbook
Laptop, and then when I attach VS Code to a container VS Code server along with extensions
for my project such as the C++ extension and the CMake extension those will all be running
inside the development container my source code will be mounted from my local machine into the
container and my debugger, my compiler, all of those build tools are in the container. That's where my
build, run, and debug environment is. The theme and UI extensions in this local OS part of the
diagram, those are things like your color themes, so that you have your local quality development
experience regardless of where you are building and debugging your program. For this demo I'm going
to use a simple C++ Fibonacci program. So I'm going to open that in VS Code on my Macbook Pro
and the first thing that you're going to want to do to connect to a remote container in Visual
Studio Code is install the remote containers extension, which you can do in the marketplace.
There we go, looks like this.
Once you have this extension installed then if you open the command palette and type
remote containers you'll see all of the commands provided
by the remote containers extension. So I'm going to select add development container
configuration files, because that's the next step for opening our project inside of a container,
and I'm going to select C++ and for the distribution I'll do Ubuntu 18.
So what did this just do? So what this did is it created this .devcontainer folder
in my project directory and there are two files in that folder: devcontainer.json and a docker
file. So the docker file describes the image that will be used to spin up the container and because
I selected C++ as my development environment, the remote containers extension gave me a sample
docker file for a C++ development container and you can see what the contents
are for this sample file by clicking on this link. It's on GitHub, and as you can see it it
just installs a bunch of C++ build tools, but you could change the contents of this docker
file, you could bring a different docker file into this project, this is totally customizable.
In devcontainer.json, this is where you specify things like the extensions that you want to be
installed inside the container so by default. We have the cpptools extension or the microsoft
C++ extension listed here and again that was just provided by the remote containers
extension because I selected C++ as my development environment. I'm going to add
the CMake tools extension because I have a CMakeLists script in this project and
I'm using the CMake Tools extension to build and debug my fibonacci program.
So let's save those changes. Some other things that you can do in devcontainer.json is specify
additional local file volume mounts, so you can add the mounts property for example
if you wanted to specify additional mounts between your local and target file systems.
Okay so now that we have our dev container.json file in our docker file that's all VS Code
needs to open this folder in a remote development container. So if we open the command palette again
then we can select
Open Folder in Container...
opening the fibonacci folder...
and this is starting up our development container. And you can tell that you are in your development
container by checking out this green rectangle in the bottom left corner we see here it says
Dev Container C++. Okay, so now that we are in our development container we should
be able to build and debug our project and we can check to make sure that we have all of our C++
build tools installed in our container, things that were outlined in that docker file, which we do.
So in order to build and debug using CMake Tools the first thing that you need to do
is select a kit and here all of these commands in the blue status bar at the bottom, these are
provided by the CMake Tools extension and this one here that says gcc this is the kit that I'm using,
which basically is just the the compiler that we want to be using and if you select
that from the status bar a list of compilers found on the computer will appear, so these are
compilers found in the development container. I'll select GCC. And then let's do a clean reconfigure.
Configuring done, and now
let's build and debug our project, so I'll just select this build icon from the status bar
and to debug our project let's set a breakpoint,
oh looks like we already have one set, so we can hit this debug button.
And this is asking me to enter the number of terms, so if we go to our terminal, let me put
in 10, and now we are in our debugging session, we can step through our program,
you have your local variables, you have your watch variables, your call stack, breakpoints,
so once again you're getting that local quality development experience even though it's using
gdb inside of the container and running my program inside of the development container.
So I just showed you how you can create and attach to a development container in Visual Studio Code,
but let's say you already have docker containers running and you want to attach
to those existing docker containers regardless of how they were started. You can actually do
that too in Visual Studio Code so in order to do that just open up the remote explorer
and make sure you have containers selected at the top here and then these are development
containers that I've created with Visual Studio Code but if we collapse that you'll see other
containers, so these are other docker containers that you have running and you can attach to those
by clicking this attach to container window icon. You could also do this from the command palette.
And select attach to running container.
Thanks for watching this quick demo for how to develop C++
projects inside of a container using Visual Studio Code be sure to check out all of the
live sessions at Pure Virtual C++ on May 3rd for some more C++ plus content. Thank you!