You can attached a vscode session in a running docker container or vscode can create a docker container to attach.
For this configuration we are going to run a docker container in detached mode and then, connect to it using vscode in remote mode.
- Running container must have installed gdb
- Install Remote-containers extensions in your local vscode application
docker run --rm -d -p 12345:2225 -it \
--name="docker-container" \
--volume=/home/user/Downloads/project:/tmp/project \
YOUR-DOCKER-IMAGE
- Click on Remote explorer (left bar)
- You will see the running docker container. For this example we will be using a docker image
danieltobon43/pcl-docker:1.12.1-alpine3.15-dev-v4
- Press on Attach or Open folder in container. See Documentation for more information.
The following vscode extensions must be install in the vscode-remote version of the running container
- C/C++
- C/C++ Extension Pack
- CMake
- CMake Tools
Remove any build folder you might have and then press the Debug button. This is because vscode CMake-Tools extension will create a build folder with debug symbols and if there is already a build/ folder you might find a permission error such as:
Error: EACCES: permission denied
You can debug your application with a custom launch.json file or using CMake-Tools.
- Create a launch.json file in the .vscode folder at the root of your project. example-json
- Run and Debug (On the left panel) and choose CMakeDebugLaunch, play button
- Click on the CMake-tools (On the left panel) and then, click right to show a new menu. Select debug
Now you can set a breakpoint in your remote-vscode session and start debugging your cpp application