Issue
The following lines is an example of how to reproduce this error.
# Pull the latest version of alpine image from docker hub
➜ docker pull alpine
# Create a container from the alpine image and get on to the terminal of that container
➜ docker run -it --name alpine_bash alpine ash
➜ exit # inside the container
# Execute the command sh in that container
➜ docker exec -it alpine_bash sh
Here is the output.
Error response from daemon: Container 00f5f2aeae9f2d74b8f92d769da5086b4a72eaa01bdae8351fbd77b753e74b51 is not running
➜ docker run -it --name alpine_bash alpine sh
docker: Error response from daemon: Conflict. The container name "/alpine_bash" is already in use by container "00f5f2aeae9f2d74b8f92d769da5086b4a72eaa01bdae8351fbd77b753e74b51". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
We got this error because the container is stopped immediately after we exit the shell, no process is running anymore, and the docker exec is only working on running containers.
Solution
Remove the container automatically.
docker run -it --rm --name alpine_bash alpine sh
This is the description of --rm from official docker reference.
If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the
--rm
flag
Or we delete the container mannually.
➜ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e877fcd9fe0e alpine "ash" 7 seconds ago Exited (0) 5 seconds ago alpine_bash
# Delete container by name
docker container rm alpine_bash