Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart container using container-id?

Tags:

docker

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.

like image 687
AnukuL Avatar asked Jan 09 '17 01:01

AnukuL


3 Answers

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.

like image 101
gile Avatar answered Sep 25 '22 23:09

gile


i tried myself:

docker restart <container_id>

docker exec -it <container_id> bash

works both perfect to restart and get into interactive terminal.

like image 44
Gabbax0r Avatar answered Sep 25 '22 23:09

Gabbax0r


Check Docker start command

docker stop {containerId} && docker start -i {containerId}
like image 32
Mohsen ZareZardeyni Avatar answered Sep 25 '22 23:09

Mohsen ZareZardeyni