21

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.

AnukuL
  • 595
  • 1
  • 7
  • 21
  • Is not the best solution, but I'm using [portainer](http://portainer.io) to manage my docker images. – Celso Agra Jan 09 '17 at 01:48
  • Check this documentation - https://docs.docker.com/engine/tutorials/dockerizing/ – Rao Jan 09 '17 at 02:31
  • Possible duplicate of [How to run a command on an already existing docker container?](http://stackoverflow.com/questions/26153686/how-to-run-a-command-on-an-already-existing-docker-container) – Matt Jan 09 '17 at 02:50
  • You can't for a short running process. http://stackoverflow.com/a/30269330/1318694 in the above dupe deals with if a container exits before you can use `docker exec` on it. – Matt Jan 09 '17 at 02:53
  • also https://github.com/docker/docker/pull/19746 – Matt Jan 09 '17 at 02:54
  • @Matt I've already seen http://stackoverflow.com/questions/26153686/how-to-run-a-command-on-an-already-existing-docker-container but OP there was more concerned with restarting his container (let it be image name) whereas I'm particularly interested in restarting the container with container-id. – AnukuL Jan 09 '17 at 03:18
  • @AnukuL Did you try this https://docs.docker.com/engine/reference/commandline/restart/ ? – Prabhakar Jan 09 '17 at 03:29
  • @Prabhakar Yes, but with no luck. – AnukuL Jan 09 '17 at 03:33
  • @AnukuL Didn't that restart the container? – Prabhakar Jan 09 '17 at 03:36
  • @Prabhakar No, It didn't restart the container – AnukuL Jan 09 '17 at 03:41

3 Answers3

27

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.

Community
  • 1
  • 1
gile
  • 5,580
  • 1
  • 25
  • 31
15

i tried myself:

docker restart <container_id>

docker exec -it <container_id> bash

works both perfect to restart and get into interactive terminal.

Gabbax0r
  • 1,696
  • 3
  • 18
  • 31
3

Check Docker start command

docker stop {containerId} && docker start -i {containerId}
Ankur
  • 2,171
  • 23
  • 29
  • 1
    There is no -t flag to docker start command. Also I added an edit to my question to be more precise. – AnukuL Jan 09 '17 at 05:50