391

Consider:

docker run -it centos /bin/bash

I pressed Ctrl+D to exit it.

I want to continue to run this container, but I found I can't.

The only method is

docker commit `docker ps -q -l` my_image
docker run -it my_image /bin/bash

Am I right? Is there a better method? (I'm using docker 0.8.0.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel YC Lin
  • 15,050
  • 18
  • 63
  • 96
  • 2
    Have you tried to assign a name to the container, and use that name to control it? By the way, by "exit it", you mean detach from it, right? – Eric Platon Feb 21 '14 at 08:12
  • 17
    Docker *is* a tool for programming, at least that's how I use it. If the majority disagree, then someone should tell Docker to not point to StackOverflow as a place to ask questions. I'm sure this isn't the first "DevOps"-like question that could go either here or SuperUser. – Ted M. Young Feb 21 '14 at 21:48
  • 6
    Docker is a developer tool. It is already a 'docker' tag here. So, I think it is proper to ask in stackoverflow. I use it to build image which require build from clean environment. – Daniel YC Lin Feb 25 '14 at 00:57
  • 6
    I wish the folks who tagged this as "off-topic" would explain why! – jpetazzo Feb 26 '14 at 01:31
  • 2
    To support this question as not being off-topic: http://meta.stackoverflow.com/q/276579/210336 and http://meta.stackoverflow.com/q/271279/210336. – Matthijs Wessels Sep 14 '15 at 09:01

15 Answers15

418

You can restart an existing container after it exited and your changes are still there.

docker start  `docker ps -q -l` # restart it in the background
docker attach `docker ps -q -l` # reattach the terminal & stdin
Luca G. Soave
  • 12,271
  • 12
  • 58
  • 109
  • 4
    to brief as one line: ```docker start `docker ps -q -l` && docker attach `docker ps -q -l` ``` – Daniel YC Lin Feb 21 '14 at 08:24
  • 10
    To brief as one line: `docker start -i $(docker ps -q -l)`. ;-) Naming can help to better control which container is selected. `-l` get the last, ok... – Eric Platon Feb 21 '14 at 09:01
  • 134
    For those new to Linux, the `\`docker ps -q -l\`` bit is an expansion. It will be replaced with the id of the last (-l) docker container created. -q merely suppresses other info (bedsides the id) usually output by `\`docker ps\``. ---- _note:_ Backtick is not a quotation sign, it has a very special meaning. Everything you type between backticks is evaluated (executed) by the shell before the main command - http://unix.stackexchange.com/questions/27428/what-does-backquote-backtick-mean-in-bash – bnieland Oct 04 '15 at 15:28
  • 2
    I have found the container getting into a state called `Created...` from which it can not be started with a `docker start ...`. It can however be restarted using `docker restart ...` – Voltaire Jun 13 '17 at 12:20
  • 2
    Note: `-l` only gets the "latest" docker. If you're like me and `docker ps -a` shows more than one thing that you need to start up, then you can restart "all" (-a) of them by changing the command above to `docker start \`docker ps -q -a\``. `-q` just makes it "quiet" (not output logging). – Rock Lee Jan 15 '19 at 18:24
  • How does it know which container you want to start? – majorgear Aug 18 '23 at 23:04
200
docker start -a -i `docker ps -q -l`

Explanation:

docker start start a container (requires name or ID)
-a attach to container
-i interactive mode
docker ps List containers
-q list only container IDs
-l list only last created container

Paglian
  • 6,162
  • 3
  • 26
  • 20
70

Use:

docker start $(docker ps -a -q --filter "status=exited")

This will start all containers which are in the exited state.

docker exec -it <container-id> /bin/bash

This will connect to the particular container.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kgs
  • 1,654
  • 2
  • 18
  • 19
  • 2
    I found this was the only one that worked for me.. due to that crucial second line. `docker exec -it /bin/bash` (or just bash). – barlop Feb 27 '18 at 02:06
  • 1
    This worked for me, But i hade to run it two times since i had two containers. So if the first one starts and the second one gets an error. just hit the same command again and it should start the next container. – Andrija Jostergård Oct 09 '18 at 12:57
55

If you want to do it in multiple, easy-to-remember commands:

  1. list stopped containers:

docker ps -a

  1. copy the name or the container id of the container you want to attach to, and start the container with:

docker start -i <name/id>

The -i flag tells docker to attach to the container's stdin.

If the container wasn't started with an interactive shell to connect to, you need to do this to run a shell:

docker start <name/id>
docker exec -it <name/id> /bin/sh

The /bin/sh is the shell usually available with alpine-based images.

If you're having problems with the container exiting immediately when you start it above, you can re-run it with an interactive shell with the following. You need the name of the image here, not the container. Because restarting didn't work, the only way to debug the problem is to delete and run it again. You are put into a shell, where you can try the CMD from the Dockerfile to see its output, or to debug why it is exiting immediately.

docker rm <name/id>
docker run -it --entrypoint /bin/sh <image-name> -s
kristianp
  • 5,496
  • 37
  • 56
  • 1
    Underrated answer, this helped me find the container i was looking for much easier than the "answer" on this question. – LizardKing Oct 17 '19 at 16:10
  • 6
    `Error response from daemon: Container is not running` – Cerin Nov 11 '19 at 21:03
  • @Cerin, I have added a last section for your problem (only 3 years late!), where the container exits immediately after you start it. – kristianp Feb 28 '23 at 03:52
  • this answer is better than others because it breaks down what the command means – Ooker Aug 30 '23 at 14:07
20

If you want to continue exactly one Docker container with a known name:

docker start  `docker ps -a -q --filter "name=elas"`
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nelson Dinh
  • 465
  • 6
  • 14
17

Follow these steps:

  1. Run below command to see that all the container services both running and stopped on. Option -a is given to see that the container stops as well

    docker ps -a
    
  2. Then start the docker container either by container_id or container tag names

    docker start <CONTAINER_ID> or <NAMES>
    

    enter image description here

    Say from the above picture, container id 4b161b302337
    So command to be run is

    docker start 4b161b302337
    
  3. One can verify whether the container is running with

    docker ps
    
zx485
  • 28,498
  • 28
  • 50
  • 59
Anil Jain
  • 171
  • 1
  • 3
13

If you have a named container then it can be started by running

docker container start container_name

where container_name is name of the container that must be given at the time of creating container. You can replace container_name with the container id in case the container is not named. The container ID can be found by running:

docker ps -a
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gaurav Gupta
  • 575
  • 1
  • 5
  • 17
9

These commands will work for any container (not only last exited ones). This way will work even after your system has rebooted. To do so, these commands will use "container id".

Steps:

  1. List all containers by using this command and note the container id of the container you want to restart: docker ps -a

  2. Start your container using container id: docker start <container_id>

  3. Attach and run your container: docker attach <container_id>

NOTE: Works on linux

dor00012
  • 362
  • 4
  • 17
Sugandha Jain
  • 333
  • 2
  • 6
3

by name

sudo docker start bob_the_container

or by Id

sudo docker start aa3f365f0f4e

this restarts stopped container, use -i to attach container's STDIN or instead of -i you can attach to container session (if you run with -it)

sudo docker attach bob_the_container
karol wołonciej
  • 109
  • 1
  • 1
  • 10
3

If you just want to start a container with status 'Exited', just type:

sudo docker start container_name

Without sudo. See more

docker start container_name

If container name doesn't work, replace the name to container id

2

Run your container with --privileged flag.

docker run -it --privileged ...
vahid sabet
  • 485
  • 1
  • 6
  • 16
  • Good find. the --privileged option seems to keep the container around after the original run exits. Also helps to have "--name myName" so "docker start ..." and "docker exec ..." commands work. – Jay Elston Dec 17 '21 at 18:40
0
docker start `docker ps -a | awk '{print $1}'`

This will start up all the containers that are in the 'Exited' state

0

For those coming here in 2021 and beyond, the following command will do what the accepted answer will do in one line.

$ docker start -ai $(docker ps -q -l)
burnsac
  • 21
  • 2
0

In my case, the docker container exits cleanly when I start it so none of the above worked. What I needed was a way to change the command to be run.

With docker-compose I was able to change the command by running:

docker-compose run <container name in docker-compose.yml> bash

e.g.

docker-compose run app bash

Note! This actually recreates the container, so it is not run on the previous instance.

PHZ.fi-Pharazon
  • 1,479
  • 14
  • 15
-1

for me, I was testing node version which was not working. So I tried below command

docker run -d -it node:latest
Anand Deep Singh
  • 2,560
  • 3
  • 22
  • 28