I created a container with -d
so it's not interactive.
docker run -d shykes/pybuilder bin/bash
I see that the container has exited:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6c45e8cc5f0 shykes/pybuilder:latest "bin/bash" 41 minutes ago Exited (0) 2 seconds ago clever_bardeen
Now I would like to run occasional commands on the machine and exit. Just to get the response.
I tried to start the machine. I tried attaching. I thought I could call run
with a container, but that does not seem to be allowed. Using start
just seems to run and then exist quickly.
I'd like to get back into interactive mode after exiting.
I tried:
docker attach d6c45e8cc5f0
But I get:
2014/10/01 22:33:34 You cannot attach to a stopped container, start it first
But if I start it, it exits anyway. Catch 22. I can't win.
Pipe a command to stdin
Must remove the
-t
for it to work:This can be more convenient that using CLI options sometimes.
Tested with:
then on another shell:
Then on first shell:
Tested on Docker 1.13.1, Ubuntu 16.04 host.
For Mac:
if you want to connect as root user:
I had to use bash -c to run my command:
docker exec -it CONTAINER_ID bash -c "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql"
Assuming the image is using the default entrypoint
/bin/sh -c
, running/bin/bash
will exit immediately in daemon mode (-d
). If you want this container to run an interactive shell, use-it
instead of-d
. If you want to execute arbitrary commands in a container usually executing another process, you might want to trynsenter
ornsinit
. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for the details.Some of the answers here are misleading because they concern containers that are running, not stopped.
Sven Dowideit explained on the Docker forum that containers are bound to their process (and Docker can't change the process of a stopped container, seemingly due at least to its internal structure: https://github.com/docker/docker/issues/1437). So, basically the only option is to
commit
the container to an image andrun
it with a different command.See https://forums.docker.com/t/run-command-in-stopped-container/343
(I believe the "
ENTRYPOINT
with arguments" approach wouldn't work either, since you still wouldn't be able to change the arguments to a stopped container.)