Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue a Docker container which has exited

Tags:

docker

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

like image 691
Daniel YC Lin Avatar asked Feb 21 '14 08:02

Daniel YC Lin


People also ask

How do I resume a docker container?

Enter the docker start command with the Container ID in the command line to resume the Container. Note: This preserves environment variables set during the initial docker run statement.

How do I run a container again?

To restart an existing container, we'll use the start command with the -a flag to attach to it and the -i flag to make it interactive, followed by either the container ID or name. Be sure to substitute the ID of your container in the command below: docker start -ai 11cc47339ee1.

What happens when a docker container exits?

When this happens, the program will stop, and the container will exit. The container has been stopped using docker stop : You can manually stop a container using the docker stop command. The Docker daemon has restarted, and it terminated and restarted the container: Docker can restart containers if you need it to.


2 Answers

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 
like image 112
Luca G. Soave Avatar answered Sep 25 '22 10:09

Luca G. Soave


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

like image 24
Paglian Avatar answered Sep 24 '22 10:09

Paglian