How to continue a Docker container which has exited

前端 未结 10 1905
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 14:51

Consider:

docker run -it centos /bin/bash

I pressed Ctrl+D to exit it.

I wa

相关标签:
10条回答
  • 2020-11-29 14:59

    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
    
    0 讨论(0)
  • 2020-11-29 15:00
    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

    0 讨论(0)
  • 2020-11-29 15:01

    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
    
    0 讨论(0)
  • 2020-11-29 15:03

    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.

    0 讨论(0)
  • 2020-11-29 15:03

    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
    
    0 讨论(0)
  • 2020-11-29 15:09

    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.

    0 讨论(0)
提交回复
热议问题