Daniel YC Lin
Daniel YC Lin

Reputation: 15992

How to continue a Docker container which has exited

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

Upvotes: 416

Views: 476227

Answers (16)

Vinay
Vinay

Reputation: 31

Steps are below to check and resume your exited containers

  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>
    

Upvotes: 3

Anand Deep Singh
Anand Deep Singh

Reputation: 2620

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

docker run -d -it node:latest

Upvotes: -1

kristianp
kristianp

Reputation: 5895

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

Upvotes: 66

Sugandha Jain
Sugandha Jain

Reputation: 353

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

Upvotes: 9

PHZ.fi-Pharazon
PHZ.fi-Pharazon

Reputation: 2073

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.

Upvotes: 0

Henrique Barros
Henrique Barros

Reputation: 49

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

Upvotes: 3

burnsac
burnsac

Reputation: 31

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)

Upvotes: 0

vahid sabet
vahid sabet

Reputation: 583

Run your container with --privileged flag.

docker run -it --privileged ...

Upvotes: 2

karol wołonciej
karol wołonciej

Reputation: 119

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

Upvotes: 3

Deepank Varshney
Deepank Varshney

Reputation: 1

docker start `docker ps -a | awk '{print $1}'`

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

Upvotes: 0

Anil Jain
Anil Jain

Reputation: 171

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
    

Upvotes: 17

Gaurav Gupta
Gaurav Gupta

Reputation: 575

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

Upvotes: 13

Nelson Dinh
Nelson Dinh

Reputation: 465

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

docker start  `docker ps -a -q --filter "name=elas"`

Upvotes: 20

kgs
kgs

Reputation: 1864

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.

Upvotes: 75

Paglian
Paglian

Reputation: 6712

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

Upvotes: 214

Luca G. Soave
Luca G. Soave

Reputation: 12679

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

Upvotes: 454

Related Questions