'how to generate core file in docker container?

using ulimit command, i set core file size.

ulimit -c unlimited

and I compiled c source code using gcc - g option. then a.out generated. after command

./a.out there is runtime error .

(core dumped)

but core file was not generated.(ex. core.294340)

how to generated core file?



Solution 1:[1]

First make sure the container will write the cores to an existing location in the container filesystem. The core generation settings are set in the host, not in the container. Example:

echo '/cores/core.%e.%p' | sudo tee /proc/sys/kernel/core_pattern

will generate cores in the folder /cores.

In your Dockerfile, create that folder:

RUN mkdir /cores

You need to specify the core size limit; the ulimit shell command would not work, cause it only affects at the current shell. You need to use the docker run option --ulimit with a soft and hard limit. After building the Docker image, run the container with something like:

docker run --ulimit core=-1 --mount source=coredumps_volume,target=/cores ab3ca583c907 ./a.out

where coredumps_volume is a volume you already created where the core will persist after the container is terminated. E.g.

docker volume create coredumps_volume

Solution 2:[2]

If you want to generate a core dump of an existing process, say using gcore, you need to start the container with --cap-add=SYS_PTRACE to allow a debugger running as root inside the container to attach to the process. (For core dumps on signals, see the other answer)

Solution 3:[3]

I keep forgetting how to do it exactly and keep stumbling upon this question which provides marginal help.

All in all it is very simple:

  1. Run the container with extra params --ulimit core=-1 --privileged to allow coredumps:
docker run -it --rm \
--name something \
--ulimit core=-1  --privileged \
--security-opt seccomp=unconfined \
--entrypoint '/bin/bash' \
$IMAGE
  1. Then, once in container, set coredump location and start your failing script:
sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t
myfailingscript.a
  1. Enjoy your stacktrace
cd /tmp; gdb -c `ls -t /tmp | grep core | tail -1`

Solution 4:[4]

Well, let's resurrect an ancient thread.

If you're running Docker on Linux, then all of this is controlled by /proc/sys/kernel/core_pattern on your raw metal. That is, if you cat that file on bare metal and inside the container, they'll be the same. Note also the file is tricky to update. You have to use the tee method from some of the other posts.

 echo core | sudo tee /proc/sys/kernel/core_pattern

If you change it in bare metal, it gets changed in your container. So that also means that behavior is going to be based on where you're running your containers.

My containers don't run apport, but my bare metal did, so I wasn't getting cores. I did the above (I had already solved the ulimit -c thing), and suddenly I get core files in the current directory.

The key to this is understanding that it's your environment, your bare metal, that controls the contents of that file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gert van den Berg
Solution 3 y.selivonchyk
Solution 4 Joseph Larson