惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
V
Visual Studio Blog
Jina AI
Jina AI
博客园 - 司徒正美
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
小众软件
小众软件
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
WordPress大学
WordPress大学
爱范儿
爱范儿
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏

Lei Mao's Log Book

2026 FIFA World Cup 备受嘲讽的会徽 Vargas Plateau Regional Park 徒步 Vargas Plateau Regional Park Apex 2026 FIFA World Cup 小组赛赛程 Retaining EXIF Metadata In GIMP San Francisquito Creek Joint Powers Authority 2025 Calendar Photo 麦当劳 The FIFA World Cup 套餐 Ardenwood Historic Farm 徒步 Ardenwood Historic Farm Synchronizations With TorchRec KeyedJaggedTensor Pacific Commons Linear Park 徒步 Pacific Commons Linear Park 2026 San Jose Half Marathon 竞赛 目标 Mountain View Shoreline Park 徒步 Mountain View Shoreline Park PyTorch AOTInductor Hybrid Lowering Carquinez Strait Regional Shoreline 徒步 Carquinez Strait Regional Shoreline PyTorch Triton Kernel Transparent Tracing and Compilation 脸庞 PyTorch Fake Export 2026 BRAIN Foundation 10K 竞赛 2026 Wild and Scenic Film Festival 参观 2026 Wild and Scenic Film Festival 系统工程程序员修 Bug FIFA 官方网站的语言 PyTorch Custom Operation 汉堡王 The Mandalorian and Grogu 套餐
Python Debugging Via VS Code In Docker Container
Lei Mao · 2026-06-14 · via Lei Mao's Log Book

Introduction

Debugging is an essential part of software development, and it can be particularly challenging when working with applications running inside Docker containers. Visual Studio Code (VS Code) provides powerful debugging capabilities that can be leveraged to debug Python applications running in Docker containers.

In this blog post, we will explore how to set up Python debugging in VS Code for applications running inside Docker containers.

Example Program

This is a simple Python program with an intentional bug that we will use to demonstrate debugging in VS Code. We could insert breakpoints in the code lines marked with comments, and then use VS Code to step through the code and inspect variables to find the bug.

debug_math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


def multiply_by_two(x):

return x * 2

def buggy_sum(arr):


total = 0

for i in range(len(arr) - 1):

total += arr[i]
return total

if __name__ == "__main__":

data = [3, 5, 1, 8, 14]
print("Array:", data)





doubled = [multiply_by_two(x) for x in data]
print("Doubled Array:", doubled)
result = buggy_sum(doubled)
print("Sum:", result)

print("Python sum:", sum(doubled))

Debugging Via Attaching To A Running Container

To start a Docker container for debugging, the Docker container has to have debugpy installed and the port for debugpy (default is 5678) has to be exposed. We can use the following command to start a Docker container with the necessary configurations for debugging:

1
$ docker run -it --rm --gpus all -v $(pwd):/mnt -w /mnt -p 5678:5678 nvcr.io/nvidia/pytorch:26.04-py3

The docker container nvcr.io/nvidia/pytorch:26.04-py3, which has debugpy pre-installed, is used for quick demonstration.

To start the program for debugging in Docker container, we can use the following command to start the debugpy server and wait for the VS Code client to connect:

1
$ python -m debugpy --listen 0.0.0.0:5678 --wait-for-client debug_math.py

In VS Code, with our launch.json where attach and port are configured, we can start debugging by Command Palette Ctrl + Shift + D and hitting F5. This will connect to the debugpy server running in our Docker container, allowing us to set breakpoints and step through our code as if it were running locally.

Debugging In Development Container

To start the development container for debugging, we can use the Remote-Containers: Reopen in Container command via the Command Palette Ctrl + Shift + P in VS Code. This will open the current workspace inside the development container, allowing us to run and debug our code directly within the container environment.

The development container and VS Code extension setups are a little bit more complicated. The reference setup could be found in the GitHub repository for this example.

References