Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a docker container to the state: dead for debugging?

I need to get some containers to dead state, as I want to check if a script of mine is working. Any advice is welcome. Thank you.

like image 697
qubsup Avatar asked Jun 09 '17 10:06

qubsup


1 Answers

You've asked for a dead container.

TL;DR: This is how to create a dead container

Don't do this at home:

ID=$(docker run --name dead-experiment -d -t alpine sh)
docker kill dead-experiment
test "$ID" != "" && chattr +i -R /var/lib/docker/containers/$ID
docker rm -f dead-experiment

And voila, docker could not delete the container root directory, so it falls to a status=dead:

docker ps -a -f status=dead
CONTAINER ID        IMAGE         COMMAND       CREATED             STATUS        PORTS         NAMES
616c2e79b75a        alpine        "sh"          6 minutes ago       Dead                        dead-experiment

Explanation

I've inspected the source code of docker and saw this state transition:

container.SetDead()
// (...)
if err := system.EnsureRemoveAll(container.Root); err != nil {
    return errors.Wrapf(err, "unable to remove filesystem for %s", container.ID)
}
// (...)
container.SetRemoved()

So, if docker cannot remove the container root directory, it remain as dead and does not continue to the Removed state. So I've forced the file permissions to not permit root remove files (chattr -i).

PS: to revert the directory permissions do this: chattr -i -R /var/lib/docker/containers/$ID

like image 133
Robert Avatar answered Nov 15 '22 05:11

Robert