Home > database >  cannot start interactive shell with docker container
cannot start interactive shell with docker container

Time:02-23

I am trying to start an interactive shell with a docker container I have. I wish to run docker exec as follows:

➜  ~ docker ps -a
CONTAINER ID   IMAGE                                                                              COMMAND                  CREATED          STATUS                      PORTS     NAMES
a6e4b07ed32d   da34aa05cadd                                                                       "/bin/sh"                20 minutes ago   Exited (0) 2 minutes ago              blissful_williamson
➜  ~ docker start a6e4b07ed32d
a6e4b07ed32d
➜  ~ docker exec -it a6e4b07ed32d bash
Error response from daemon: Container a6e4b07ed32d1db3697b1afb5494230176c72503fccd8196785c651a4549201a is not running

What do I need to do in order to make sure the container is running, so that I can use docker exec to start a shell with it? As far as I know docker start should make sure the container is running.

CodePudding user response:

docker start will re-run the main process in an existing container. In practice I almost never need it: it's usually cleaner to docker rm the stopped container and docker run a new one.

When you do docker run the container you can tell it to run an interactive shell instead of the main process, and this can help debug the startup sequence. This works even if the main process doesn't start up on its own, and you need to understand what files are in the wrong place to debug your Dockerfile.

# Delete the stopped and broken container
docker rm a6e4b07ed32d

# Run a new container, running "bash" instead of the main process
# (Alpine-based images may need "sh" instead)
# (Images that use `ENTRYPOINT` and do not accept a shell `CMD` need
# an awkward `--entrypoint bash` option before the image name instead)
docker run --rm -it da34aa05cadd bash

If you're using a Compose setup, docker-compose run your-container bash does the same thing.

  • Related