Search code examples
docker

How to start a stopped Docker container with a different command?


I would like to start a stopped Docker container with a different command, as the default command crashes - meaning I can't start the container and then use docker exec command.

Basically I would like to start a shell so I can inspect the contents of the container.

Luckily I created the container with the -it option!


Solution

  • Find your stopped container id

    docker ps -a
    

    Commit the stopped container:

    This command saves modified container state into a new image named user/test_image:

    docker commit $CONTAINER_ID user/test_image
    

    Start/run with a different entry point:

    docker run -ti --entrypoint=sh user/test_image
    

    Entrypoint argument description:

    https://docs.docker.com/engine/reference/run/#/entrypoint-default-command-to-execute-at-runtime

    Note:

    Steps above just start a stopped container with the same filesystem state. That is great for a quick investigation; but environment variables, network configuration, attached volumes and other stuff is not inherited. You should specify all these arguments explicitly.

    Steps to start a stopped container have been borrowed from here: (last comment) https://github.com/docker/docker/issues/18078