Docker Community Forums

Share and learn in the Docker community.

Access running process inside running container

(Bunjee) #1

Let’s say that I have a process running inside a container.
Is it possible to attach to this container and see the values of the variables of the running process?

(Sam) #2

how would you see the variables normally?

(Bunjee) #3

I’m not sure since I’m not familiar with this… For example attaching a debugger to the running process or view the memory map. It is possible in a docker container?

(Sam) #4

well, you would have to docker attach to the container and the tool would have to be text mode. other than that, the same tools should work…

(Bunjee) #5

For testing I have a c program running inside my docker container. Basically it’s a for loop which runs forever and prints sth.
If I attach to the docker container from another terminal I get the same output.
How can I execute gdb on the running process then?

(Sam) #6

how do you do gdb normally on a running process… exactly the same

(Bunjee) #7

So I get the pid of the running process inside the container and run gdb.
then, I type attach PID
and I get this:

Could not attach to process. If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.

(Sam) #8

gdb has to be installed and used INSIDE the container too, right?

(Bunjee) #9

Yes, first I run this command docker exec -t -i 0bf3c5863b0d /bin/bash
to access the container and then I run gdb.

(Sam) #10

or you could run docker attach container_id

i don’t know gdb, so I don’t know what that error means, but google search might help

(Bunjee) #11

Running docker attach container_id I see the output of the running process.
I tried to figure out what’s going on with gdb but still no luck…

(Sam) #12

gdb attach says

For a process id, you must have permission to send the process a signal,
and it must have the same effective uid as the debugger.

When using “attach” with a process id, the debugger finds the
program running in the process, looking first in the current working
directory, or (if not found there) using the source file search path
(see the “directory” command).

You can also use the “file” command
to specify the program, and to load its symbol table

(Sam) #13

so the process file needs to be debuggable (have symbols attached)… not just any executable

(Bunjee) #14

Yeah… I compiled the file with -g option and then gdb ./a.out but still have errors

(Sam) #15

and you are executing gdb from the folder with the program file in it?

(Bunjee) #16

Yes, they are both at the same folder.

(Sam) #17

ok, well that is all i know about gdb… hopefully someone else will chime in

(Bunjee) #18

I appreciate the help, thanks a lot.

(Stephen Steiner) #19

If you can set the program to debug to STDOUT, then you can view it from docker logs.

(Bunjee) #20

The process is already running, I was wondering if there is a way to inject in a running process and see the memory state and the values of variables for example…