Error response from daemon: Container-id] is not running

Hi I try to run this command:

sudo docker exec -i t container-id /bin/bash

And always get this error

Error response from daemon: [Container-id] is not running

Should I just start/run the container beforehand?

The same error goes with docker attach command.

Please make sure that the container is up and running using docker ps command then try again.

Thanks for your reply but the container is Ubuntu which stops immediately when I start it. How can I keep it running?

If you don’t have a container running that you’re trying to inspect, why are you trying to inspect it?

If you just want a shell, you can also do it directly in the image with docker run instead of docker exec. For example:

docker run -it ubuntu:latest /bin/bash
  • docker exec → run a command on an existing/running container
  • docker run → create a new container from an image, and run the command there

Thanks for your reply. Actually I just want to use current container.

When I use

docker run -it

and then exit with ctrl P Q, I can exit the shell without shutting down the container. I just wonder if I can get into a shell of a container that is not running like Ubuntu, which stops immediately after I use docker start . Maybe that’s not possible!

Why do you want to do so?

Well, just out of curiosity, I wonder if it is possible! But I guess it’s a common operation.

The idea is that Docker containers are ephemeral. Once they stop running, you should be able to just throw them away and not care about it.

If there’s anything that might be of interest to you after the container has run, you should use volumes to save that information to your own system. E.g. logs, data files, …

So, there’s no point in inspecting a stopped container. Either, you should inspect the image that this container was based on, by creating a new image from that container (the docker run approach), or you should check out a container while it’s running (the docker exec approach).
If you need something from a container after it stopped, you should’ve saved it somewhere.

Thanks. I’ve got it.