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] sanne@Surendras-MacBook-Pro:~ > 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.

 

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] sanne@Surendras-MacBook-Pro:~ > docker start 6ac657c2e560
6ac657c2e560
[5668] sanne@Surendras-MacBook-Pro:~ > 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.

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] sanne@Surendras-MacBook-Pro:~ > 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] sanne@Surendras-MacBook-Pro:~ > docker attach 6ac657c2e560
root@6ac657c2e560:/# 
root@6ac657c2e560:/# 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] sanne@Surendras-MacBook-Pro:~ > 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] sanne@Surendras-MacBook-Pro:~ > docker start -a 6ac657c2e560
root@6ac657c2e560:/#

In our next post we will see Docker command examples:

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.