How to continue a Docker container which has exite

2020-01-27 09:00发布

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

标签: docker
9条回答
放我归山
2楼-- · 2020-01-27 09:33

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.

查看更多
地球回转人心会变
3楼-- · 2020-01-27 09:35
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

查看更多
聊天终结者
4楼-- · 2020-01-27 09:36

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
    
查看更多
贼婆χ
5楼-- · 2020-01-27 09:41

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
查看更多
\"骚年 ilove
6楼-- · 2020-01-27 09:41

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

docker start  `docker ps -a -q --filter "name=elas"`
查看更多
爷、活的狠高调
7楼-- · 2020-01-27 09:42

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.

查看更多
登录 后发表回答