Step 1:
docker ps -a
container Id: dd5cf6b519b4
I need to inspect inside the stopped docker container which is cannot start.
I tried with docker exec -it container-id bin/bash But this is for running container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
0dfd54557799 ubuntu "/bin/bash" 25 seconds ago Exited (1) 4 seconds ago peaceful_feynman
Commit the stopped image
$ docker commit 0dfd54557799 debug/ubuntu
now we have a new image
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debug/ubuntu <none> cc9db32dcc2d 2 seconds ago 64.3MB
create a new container from the "broken" image
$ docker run -it --rm --entrypoint sh debug/ubuntu
inside of the container we can inspect - for example, the file system
$ ls /app
App.dll
App.pdb
App.deps.json
You can start container with specific entrypoint
docker run --entrypoint sleep YOUR_IMAGE 3600
It will block current terminal for 3600 seconds. You can open new terminal tab(do not close current one) and you can verify if your container is working with the
docker ps
If you do not want to block current terminal, you can add -d flag to docker run:
docker run -d --entrypoint sleep YOUR_IMAGE 3600
Above command will start docker which will be doing nothing, then you can ssh into the container when it is working with
docker exec -ti CONTAINER HASH sh
I created a container using the command
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
Now I see the container has exited
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e5017aef3f9 ubuntu "/bin/bash -c 'echo '" 38 seconds ago Exited (0) 36 seconds ago elegant_euler
Question: How can I restart and get in to the interactive mode for this container using its container-id?
I cannot use docker run -it <image_name> since this expects image name and not container id.
I tried using docker attach , but I think this only works for running containers.
I don't want to commit this container just yet so, how can I restart and get in to interactive mode for this container using it's container-id?
EDIT: I'm able to get in to other containers using docker start {container-id} and then running docker attach {container-id}. I wonder if there is something peculiar with the way I created the container which would result in this behavior. I'm just starting out with docker so do direct me in right direction if I'm missing some basic bit.
A container exits when it completes its command. So the container started with
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
will exit as soon as the command echo is completed. In this case it doesn't make sense to restart that container.
If you run a new container in detached mode you'll be able to keep it live and to attach it in a second time.
So, in your case you should run a new container from the image in detached mode running a command like /bin/bash , then you can run the echo and attach it
docker run -d -ti ubuntu /bin/bash
docker exec -ti <containerId> /bin/bash -c "echo 'cool content' > /tmp/cool-file"
The container will be kept alive, so you can exec more commands on it, e.g.
docker exec -ti /bin/bash -c "cat /tmp/cool-file"
or run a new /bin/bash to "attach" your container and to work in it as a command prompt
docker exec -ti <containerId> /bin/bash
root#<containerId>:/# cat /tmp/cool-file
cool content
You can succesfully stop / start /restart this container
docker stop <containerId> && docker start <containerId>
or
docker restart <containerId>
Remind that when you restart a container it executes again its original command. So if you would able to restart the container of your use case (but you dont't) it would run again /bin/bash -c "cat /tmp/cool-file"
Restarting a container that run with command /bin/bash , it will run again the same command when restarting.
You normally can't change the command to RUN when restarting an existing container; to do it you can try some tricks as suggested at How to start a stopped docker container with a different command.
i tried myself:
docker restart <container_id>
docker exec -it <container_id> bash
works both perfect to restart and get into interactive terminal.
Check Docker start command
docker stop {containerId} && docker start -i {containerId}
I'm unable to connect to a container that's running on a swarm. Seems like the following doesn't work:
docker exec -it <container_ID> bash
Here is some output:
>$ docker service ls
ID NAME REPLICAS IMAGE COMMAND
4rliefwe74o5 login 1/1 login-arm64:1.0
>$ docker service ps login
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
2jk3s2xs7ce62piunbkiptypz login.1 login-arm64:1.0 odroid64-cluster4 Running Running 5 minutes ago
Then I'll run:
$ docker exec -it 2jk3s2xs7ce62piunbkiptypz bash
or
$ docker exec -it login.1 bash
and see the following errors
Error response from daemon: No such container: 2jk3s2xs7ce62piunbkiptypz
Error response from daemon: No such container: login.1
Use docker ps to find the names you can use. Look under both CONTAINER ID and NAMES, either will work.
>$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e53bff8bebfc login-arm64:1.0 "/bin/sh -c 'node ser" 27 seconds ago Up 25 seconds login.1.cg7fltcu3wfe7ixtnqzg8myy1
>$ docker exec -it e53bff8bebfc bash
root#e53bff8bebfc:/#
The long name is of the form $SERVICE_NAME.$REPLICA_NUMBER.$ID_FROM_SERVICE_PS
>$ docker exec -it login.1.cg7fltcu3wfe7ixtnqzg8myy1 bash
root#e53bff8bebfc:/#
Quite an older question, but just my two cents here: I very often run:
docker exec -it $(docker ps -q -f name="login*") sh
-q only returns the container id
-f name="login*" applies a filter based on container name, using a regex
This comes in handy because starting a new container will change the container name with some random characters in it. It's important that your filter returns just 1 container, so specify the name in a way that there will be just 1 result. For example: if you have a container "monster" and a container "monitor", you need -f name="moni*" to exclude the "monster" container.
The command will result in something like:docker exec -it login.1.cg7fltcu3wfe7ixtnqzg8myy1 sh
I have a docker image dajobe/hbase and its been built from Ubuntu. I created a container of this image and named it hb.
$ docker run -d --name hb dajobe/hbase
e1f68ff8b3b6c5e474426e2566f8c087d6a785fc5eeb58cd2aeb86176068651d
I then started the /bin/bash on hb, and checked for the availability of the vi editor.
$ docker exec -it hb /bin/bash
root#e1f68ff8b3b6:/# vi
bash: vi: command not found
I then installed vi editor using apt-get
# apt-get install vim
Reading package lists...
DoneBuilding dependency tree
Reading state information... Done
.....
.....
I wanted to commit the changes so that vi editor could persist.
$ docker commit hb dajobe/hbase
1be196188efc5a52562dc8ee1b63d0fd560ea163c49331c10dc435848d75ef64
then, when i again started dajobe/hbase, it automatically stopped.
$ docker run -d --name hb dajobe/hbase
c3e7b9f48077ef854efc6f9bab5e85986e265c98de5423bece0000c973206c38
$ docker exec -it hb /bin/bash
FATA[0000] Error response from daemon: Container hb is not running
Why is the container not running ?
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3e7b9f48077 dajobe/hbase:latest "/opt/hbase-server" 11 secs ago Exited (0) 8 secs ago hb
Why is the status "Exited" ? Before committing, this wasn't the case, the status was "Up".
I would expect the status to be Exited. Perhaps the original image you were using had an ENTRYPOINT that did something that kept the container running while you exec'ed to it. You can try this:
docker run -d --name hb dajobe/hbase sleep 60
Then try your exec, for the next 60 seconds you will connect with your interactive shell. After that, you will get the same message again.
The -d makes the container a daemon. It needs something to do, though, otherwise it just exits! Have you tried just doing the run line with the -it?
docker run -it --name hb dajobe/hbase bash
You get a shell prompt there, too, where you can make your updates to the image.
-g
In my case, it solved starting the container again, as easy as it may sound.
Search for the container name with docker ps -a (column "NAMES") if you do not know it; in your case it is known: "hb".
Then docker start hb.
Then docker exec -it hb /bin/bash.
You have used run and not start when you wanted to start an already existing but exited container. I do not see any start in your commands.
then, when i again started dajobe/hbase, it automatically stopped.
$ docker run -d --name hb dajobe/hbase
c3e7b9f48077ef854efc6f9bab5e85986e265c98de5423bece0000c973206c38
$ docker exec -it hb /bin/bash FATA[0000] Error response from daemon:
Container hb is not running
I also face the same as you to run the couchdb bash command
docker exec -it my-couchdb bash
and
docker exec -it my-couchdb /opt/couchdb/bin/remsh
Error message:
Error response from daemon: Container 54ca56353e3839ff0b824cf5468973aff021d14ad6b2531b85a1b95437b2ae13 is not running
For me the solution was...
I did the all kind of setup whenever I have created the container
Command:
docker run -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password --publish=5984:5984 --name my-couchdb --volume=$HOME/Desktop/BigData/docker-couchdb/data:/opt/couchdb/etc/local.d -d couchdb
If someone face the same problem as mine then try first others solution and if all of the others solution not work then please try once mine, hope it will work finally.
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.
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.
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.
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.
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
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.)
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"
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
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
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.
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.
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
I usually use this:
docker exec -it my-container-name bash
to continuously interact with a running container.
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.
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).
For Mac:
$ docker exec -it <container-name> sh
if you want to connect as root user:
$ docker exec -u 0 -it <container-name> sh
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>
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. :)
# docker exec -d container_id command
Ex:
# docker exec -d xcdefrdtt service jira stop
A quick way to resume and access the most recently exited container:
docker start -a -i `docker ps -q -l`
An easy solution that solved a similar problem for me:
docker run --interactive --tty <name_of_image>