Docker Container Debug with VScode

2 minute read

Published:

When using the docker container, one may want to use the feature in the vscode to remotely work on the code debug. For personal server, this would guarantee a relatively clean environment together with the debug feature.

1. Install Prerequisites

Before setting up VS Code with Docker, ensure you have:

  • Docker installed ✅
  • NVIDIA Container Toolkit installed ✅
  • VS Code installed ✅
  • VS Code Extensions:
    • Remote - Containers (ms-vscode-remote.remote-containers)
    • Docker (ms-azuretools.vscode-docker)

To install VS Code extensions, open VS Code and press:
Ctrl + Shift + X → Search for Remote - Containers and install it.


2. Mount Your Project Folder into Docker

If you want to edit files directly from VS Code while they run in the container, bind a local folder to the container:

docker run --gpus '"device=0,1,2"' -it -v ~/cuda_workspace:/workspace cuda-container bash
  • This links ~/cuda_workspace from the host to /workspace inside the container.
  • Any changes made in /workspace will reflect in ~/cuda_workspace on your host.

3. Attach VS Code to the Running Container

  1. Start your container in the background:
    docker run --gpus '"device=0,1,2"' -itd -v ~/cuda_workspace:/workspace --name cuda_env cuda-container
    
    • -itd → Runs interactively but detached (in background).
    • --name cuda_env → Names the container for easier access.
  2. Open VS Code & Attach to Container
    • Open VS Code.
    • Press Ctrl + Shift + P → Search for "Remote-Containers: Attach to Running Container".
    • Select cuda_env from the list.
  3. VS Code Opens Inside the Container 🎉
    • You can now access files inside /workspace and use the terminal inside VS Code to run CUDA commands.

4. Running Jupyter Notebook in Docker + VS Code

If you’re working with Jupyter Notebook, start a container with Jupyter:

docker run --gpus '"device=0,1,2"' -it -v ~/cuda_workspace:/workspace -p 8888:8888 cuda-container bash -c "pip install notebook && jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root"
  • Access Jupyter Notebook from http://localhost:8888 on your host.

🚀 Summary: How to Use VS Code with Docker

| Action | Command | |——–|———| | Run container with a bind mount | docker run --gpus '"device=0,1,2"' -it -v ~/cuda_workspace:/workspace cuda-container bash | | Run in background (detach mode) | docker run --gpus '"device=0,1,2"' -itd -v ~/cuda_workspace:/workspace --name cuda_env cuda-container | | Attach VS Code to running container | Open VS Code → Ctrl + Shift + P → “Remote-Containers: Attach to Running Container” | | Stop the container | docker stop cuda_env | | Restart and reattach | docker start -ai cuda_env |