The docker container

This is a small post on how to start an exited docker container. If you are in IT space for a couple of years, you will know how Docker disrupted IT. Docker just changed many things and simplified many people’s work. In this post, we will see how to start an exited docker container.

If you are working on Docker containers, you may see some of your containers are excited. If you want to know how to start them, then this post is for you.

List all exited docker containers

Docker provide’s docker subcommands, one of that is ps. The ps command will show running containers(processes). To see only containers which are exited(killed) use below command.

docker ps -f "status=exited"

Output:

[5666] [email protected]:~ > docker ps -f "status=exited"
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                    PORTS               NAMES
6ac657c2e560        f6f26c66be5e                 "/bin/bash"              6 days ago          Exited (0) 2 days ago                         friendly_minsky
761978f8ed0f        f6f26c66be5e                 "/bin/bash"              7 days ago          Exited (127) 7 days ago                       youthful_noether
304eb369fb17        linuxnix/buildimage:latest   "/usr/sbin/apache2..."   3 weeks ago         Exited (0) 7 days ago                         upbeat_hugle

To list all the running/exited containers we can just use below command.

docker ps -a

We have two methods to bring a container to running state.

Related concept:   Understanding DOCKER containers in Linux?

 

Method 1: Start and attache exited docker container

Once you get your container ID from the above command list, you can use whatever container ID you want to start with the following command.

docker start <container_ID>

Example: I want to start “6ac657c2e560″, use below command for that.

[5667] [email protected]:~ > docker start 6ac657c2e560
6ac657c2e560
[5668] [email protected]:~ > docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6ac657c2e560        f6f26c66be5e        "/bin/bash"         6 days ago          Up 2 seconds        80/tcp              friendly_minsky

If you want to attach to this started container, you can execute attach command.

Related concept:   Uninstall docker in Apple mac book in three steps

Note: If you are new to docker don’t be worried if you don’t see prompt when you connect a container. That depends on what app you containerized. In this above example, we containerized bash shell that way you get prompt when you attach to it.

docker attach <container_ID>

Example:

[5668] [email protected]:~ > docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6ac657c2e560        f6f26c66be5e        "/bin/bash"         6 days ago          Up 2 seconds        80/tcp              friendly_minsky
[5669] [email protected]:~ > docker attach 6ac657c2e560
[email protected]:/# 
[email protected]:/# hostname
6ac657c2e560

Method2: Start exited container with docker start command

Sometimes the above process will not work. The best bet is to start an exited container and attach with a single command.

docker start -a <container_id>

Example:

[5670] [email protected]:~ > docker ps -f "status=exited"
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                       PORTS               NAMES
6ac657c2e560        f6f26c66be5e                 "/bin/bash"              6 days ago          Exited (130) 6 seconds ago                       friendly_minsky
761978f8ed0f        f6f26c66be5e                 "/bin/bash"              7 days ago          Exited (127) 7 days ago                          youthful_noether
304eb369fb17        linuxnix/buildimage:latest   "/usr/sbin/apache2..."   3 weeks ago         Exited (0) 7 days ago                            upbeat_hugle
[5675] [email protected]:~ > docker start -a 6ac657c2e560
[email protected]:/#

In our next post we will see Docker command examples:

Related concept:   How to install Docker in Mac OS