595

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Johnston
  • 20,196
  • 18
  • 72
  • 121
  • how did you know that the docker container had exited? what command did you run? – Thufir Nov 22 '17 at 08:54
  • `docker container ls -a` – Brandon Manchester Nov 07 '18 at 05:22
  • If you just need the file system: [*How to start a stopped Docker container with a different command?*](https://stackoverflow.com/questions/32353055/how-to-start-a-stopped-docker-container-with-a-different-command) (Note that environment variables and other things in memory are already lost when the container stopped.) – Franklin Yu Feb 26 '19 at 04:53

20 Answers20

626

In October 2014 the Docker team introduced docker exec command: https://docs.docker.com/engine/reference/commandline/exec/

So now you can run any command in a running container just knowing its ID (or name):

docker exec -it <container_id_or_name> echo "Hello from container!"

Note that exec command works only on already running container. If the container is currently stopped, you need to first run it with the following command:

docker run -it -d shykes/pybuilder /bin/bash

The most important thing here is the -d option, which stands for detached. It means that the command you initially provided to the container (/bin/bash) will be run in the background and the container will not stop immediately.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
Scadge
  • 9,380
  • 3
  • 30
  • 39
  • 153
    This doesn't work on a stopped container, only a running one. So if you have a container that immediately stops itself, as in the question, this won't actually work to get something else running inside it. – interfect Feb 19 '15 at 00:55
  • 7
    @interfect is right, and CDR LDN has a more comprehensive answer. – Dr. Jan-Philip Gehrcke Jun 10 '15 at 19:52
  • 2
    So, the high-level question was the same question I was seeking an answer to "How to run a command on an already existing docker container?" and this solution was the exact solution I was looking for. I didn't need to add `-it` to run a command in the docker container. I understand the contextual confusion, but this is the solution I needed for that exact question. Not sure what that means for the "correct" answer, perhaps the title should be changed? This gets my upvote either way. – Arthur Weborg Jan 26 '16 at 22:15
  • 11
    @Jan-PhilipGehrcke Btw this person's username has changed from `CDR LDN` to `cdrev` for the answer below (http://stackoverflow.com/a/26181666/149428). – Taylor D. Edmiston Nov 17 '16 at 18:22
  • 6
    Why passing `-it`? – Iulian Onofrei Dec 21 '17 at 15:19
  • 9
    omg why is this so complicated? Seems like the most basic thing you'd need to do. We must not be using it the way they intend. – sudo Feb 08 '18 at 19:50
  • 1
    This still doesn't answer how to attach to a container that wasn't ``docker run`` with ``-it`` in the first place... Question asks about a container that exits and is down because even with ``bash`` it simply exits because it doesn't keep STDIN open with ``-i`` – ortonomy May 09 '18 at 05:02
  • 3
    "docker run -it -d shykes/pybuilder /bin/bash" created another container instance – Blue Clouds Sep 22 '18 at 10:17
  • I had an error "-bash: !": event not found" until I have removed exclamation mark from "Hello form container" – Andrzej Martyna Apr 05 '19 at 15:05
  • 1
    This is incorrect answer. Please check the asnwer by @Adam below. – nagendra547 Apr 23 '19 at 07:07
  • all I do is see **dquote>** when exec that? – SuperUberDuper Aug 24 '19 at 10:42
  • Not working for me, the "docker run" command tries to start a new container, instead of starting the exiting one. – Ashish Pratap Apr 01 '21 at 11:15
  • @AshishPratap I think that is exactly what `docker run` is supposed to do - to start up a _new_ container. And if you want to start an existing container, you should use `docker start `. (Note that it's just from the top of my head, as I haven't used docker for some time already.) Then on the running container you can use `docker exec ...`. Hope this helps. – Scadge May 12 '21 at 10:28
311

Your container will exit as the command you gave it will end. Use the following options to keep it live:

  • -i Keep STDIN open even if not attached.
  • -t Allocate a pseudo-TTY.

So your new run command is:

docker run -it -d shykes/pybuilder bin/bash

If you would like to attach to an already running container:

docker exec -it CONTAINER_ID /bin/bash

In these examples /bin/bash is used as the command.

cdrev
  • 5,750
  • 4
  • 20
  • 27
193

So I think the answer is simpler than many misleading answers above.

To start an existing container which is stopped

docker start <container-name/ID>

To stop a running container

docker stop <container-name/ID>

Then to login to the interactive shell of a container

docker exec -it <container-name/ID> bash

To start an existing container and attach to it in one command

docker start -ai <container-name/ID>

Beware, this will stop the container on exit. But in general, you need to start the container, attach and stop it after you are done.

gagarine
  • 4,190
  • 2
  • 30
  • 39
Peter T.
  • 8,757
  • 3
  • 34
  • 32
104

To expand on katrmr's answer, if the container is stopped and can't be started due to an error, you'll need to commit it to an image. Then you can launch bash in the new image:

docker commit [CONTAINER_ID] temporary_image
docker run --entrypoint=bash -it temporary_image
Aaron V
  • 6,596
  • 5
  • 28
  • 31
  • 1
    FYI, I do this so much that I've put together a command called `dshell` to do it automatically in a variety of situations - https://github.com/avirshup/docker-cli-sugar – Aaron V May 02 '18 at 20:04
43

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 and run 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.)

katrmr
  • 568
  • 4
  • 13
  • 2
    Notice: running `bin/bash` without `-it` wouldn't change anything in the container, so committing it isn't really necessary and CDR LDN gives the right answer for the OP's particular situation. Still, `commit` is the answer to the technical problem of how to change the container process. – katrmr Mar 30 '15 at 23:33
  • The comment by candlerb at run-command-in-stopped-container suggesting to use a throwaway image with the volume from the inactive container worked for me: docker run --rm --volumes-from CONTAINER -i busybox tar cO /var/DIR | gzip -c > ~/mydir_backup.tgz – eel ghEEz May 28 '16 at 02:11
  • This is the actual answer to the question asked. Containers are bound to their process, so the command can't be changed. – cjsimon Nov 20 '18 at 17:48
25

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"

velop
  • 3,102
  • 1
  • 27
  • 30
19

Creating a container and sending commands to it, one by one:

docker create --name=my_new_container -it ubuntu
docker start my_new_container
// ps -a says 'Up X seconds'
docker exec my_new_container /path/to/my/command
// ps -a still says 'Up X+Y seconds'
docker exec my_new_container /path/to/another/command
langlauf.io
  • 3,009
  • 2
  • 28
  • 45
  • 1
    This is the good answer to the question. If you want to start the container after creation and be able to "docker exec" commands into it, you have to create it with the "-it" flags in the docker create command. – joanlofe Oct 08 '19 at 10:47
10

If you are trying to run shell script, you need run it as bash.

docker exec -it containerid bash -c /path/to/your/script.sh
MrKulli
  • 735
  • 10
  • 19
8

This is a combined answer I made up using the CDR LDN answer above and the answer I found here.

The following example starts an Arch Linux container from an image, and then installs git on that container using the pacman tool:

sudo docker run -it -d archlinux /bin/bash
sudo docker ps -l
sudo docker exec -it [container_ID] script /dev/null -c "pacman -S git --noconfirm"

That is all.

imriss
  • 1,815
  • 4
  • 31
  • 46
8

Pipe a command to docker exec bash stdin

Must remove the -t for it to work:

echo 'touch myfile' | docker exec -i CONTAINER_NAME bash

This can be more convenient that using CLI options sometimes.

Tested with:

docker run --name ub16 -it ubuntu:16.04 bash

then on another shell:

echo 'touch myfile' | docker exec -i ub16 bash

Then on first shell:

ls -l myfile

Tested on Docker 1.13.1, Ubuntu 16.04 host.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
8

I would like to note that the top answer is a little misleading.

The issue with executing docker run is that a new container is created every time. However, there are cases where we would like to revisit old containers or not take up space with new containers.

(Given clever_bardeen is the name of the container created...)

In OP's case, make sure the docker image is first running by executing the following command:

docker start clever_bardeen

Then, execute the docker container using the following command:

docker exec -it clever_bardeen /bin/bash
Josh
  • 2,232
  • 3
  • 15
  • 33
5

I usually use this:

    docker exec -it my-container-name bash

to continuously interact with a running container.

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
  • 7
    The whole point is that, you cannot run this command on a exited container. It shows the following error: Error response from daemon: Container 31ed0... is not running – Ashish Pratap Apr 01 '21 at 11:17
  • @AshishPratap What a strange error! I just runned "docker exec -it e47e2ece292a bash" and it works properly. Maybe you need to update the Docker? – Amin Shojaei Apr 02 '21 at 06:30
  • 2
    are you sure that when you ran this command your container was not in running state already? – Ashish Pratap Apr 05 '21 at 05:42
  • @AshishPratap Ooo you are right, my mistake. This command can not be executed in a stopped container – Amin Shojaei Apr 05 '21 at 06:24
3

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 try nsenter or nsinit. Have a look at https://blog.codecentric.de/en/2014/07/enter-docker-container/ for the details.

Andreas Steffan
  • 6,039
  • 2
  • 23
  • 25
3

Unfortunately it is impossible to override ENTRYPOINT with arguments with docker run --entrypoint to achieve this goal.

Note: you can override the ENTRYPOINT setting using --entrypoint, but this can only set the binary to exec (no sh -c will be used).

wieczorek1990
  • 7,403
  • 1
  • 25
  • 19
2

For Mac:

$ docker exec -it <container-name> sh

if you want to connect as root user:

$ docker exec -u 0 -it <container-name> sh
Lyncean Patel
  • 2,433
  • 16
  • 15
1

Simple answer: start and attach at the same time. In this case you are doing exactly what you asked for.

docker start <CONTAINER_ID/CONTAINER_NAME> && docker attach <CONTAINER_ID/CONTAINER_NAME> 

make sure to change <CONTAINER_ID/CONTAINER_NAME>

Jason
  • 3,330
  • 1
  • 33
  • 38
user353305
  • 31
  • 7
1

I am running windows container and I need to look inside the docker container for files and folder created and copied.

In order to do that I used following docker entrypoint command to get the command prompt running inside the container or attach to the container.

ENTRYPOINT ["C:\\Windows\\System32\\cmd.exe", "-D", "FOREGROUND"]

That helped me both to the command prompt attach to container and to keep the container a live. :)

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
0
# docker exec -d container_id command 

Ex:

# docker exec -d xcdefrdtt service jira stop 
4b0
  • 21,981
  • 30
  • 95
  • 142
Rajesh Gurram
  • 46
  • 1
  • 5
0

A quick way to resume and access the most recently exited container:

docker start -a -i `docker ps -q -l`
Pierz
  • 7,064
  • 52
  • 59
0

An easy solution that solved a similar problem for me:

docker run --interactive --tty <name_of_image>
LC117
  • 426
  • 4
  • 12