Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I gdb attach to a process running in a docker container?

Tags:

docker

gdb

I have a long-running process in a docker container to which I would like to attach gdb to look at what threads are running and get stacktraces. I can attach to the process from the host, but I cannot resolve any symbols because the executable file is in a different location in the filesystem (it's in a docker-mounted volume) and the shared system libraries are all stuck in a docker filesystem image somewhere in /var/lib/docker.

I am able to generate a core file and use gdb to look at it by specifying the host's path to the executable file, but because the system libraries are all in the wrong places and are loaded in the wrong locations in the corefile, I get no information from that.

Do I have any options that I've overlooked?

like image 628
leif Avatar asked Jan 28 '14 01:01

leif


People also ask

How do you attach a debugger to a docker container?

To attach to a running process in a Windows Docker container: In Visual Studio, select Debug > Attach to Process (or CTRL+ALT+P) to open the Attach to Process dialog box. Set the Connection type to Docker (Windows Container). Select Find... to set the Connection target using the Select Docker Container dialog box.

Which docker command is used to attach to a running container?

Use docker attach to attach your terminal's standard input, output, and error (or any combination of the three) to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.


1 Answers

You can attach to the process with gdb instance running in your container by attaching to the running container via lxc-attach.

Note: gdb has to be already installed in that container or you have to install it.

# 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

UPDATE 2017-04:

There is an easier workflow using docker exec now available (thanks to @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
like image 78
Jiri Avatar answered Sep 18 '22 12:09

Jiri