我怎样才能gdb连接到在docker集装箱运行的进程?

我在docker容器中有一个长期运行的进程,我想用gdb来查看哪些线程正在运行并获得堆栈跟踪。 我可以附加到主机的进程,但我不能解决任何符号,因为可执行文件是在文件系统中的不同位置(它是在docker安装的卷),共享系统库都停留在docker文件系统映像/ var / lib / docker中的某处。

我能够生成一个核心文件,并使用gdb通过指定可执行文件的主机path来查看它,但由于系统库全部位于错误的位置,并且被加载到corefile中的错误位置,所以我没有从这个信息。

我有任何我忽略的选项吗?

你可以通过lxc-attach连接到正在运行的容器来连接到运行在你的容器中的gdb实例的进程。

注意: gdb必须已经安装在那个容器中,或者你必须安装它。

 # find your container ID sudo docker ps # list of your containers - container ID is 1234567890 # find your full container ID sudo docker ps --no-trunc -q| grep <short ID> sudo lxc-attach -n <container long ID> root@1234567890:/# # optionally, you can install gdb now if it is not installed # yum install gdb root@1234567890:/# gdb ... (gdb) attach 1 

更新2017-04:

使用docker exec现在有一个更简单的工作stream程(感谢@ 42n4)。

 # find your container ID sudo docker ps # list of your containers - container ID is 1234567890 docker exec -i -t 1234567890 /bin/bash root@1234567890:/# # optionally, you can install gdb now if it is not installed # yum install gdb root@1234567890:/# gdb ... (gdb) attach 1