Docker: start, stop and remove containers

By | 25/02/2016

This post is a very short intermezzo just to show how to stop and restart containers and eventually also to remove them.
We’ve seen that with ‘docker ps’, we get an overview of the containers that are currently running. Stopping is as simple as issuing the ‘docker stop ‘ command. See below for the execution:

root@ubuntu-demo:/home/cloud-user# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
baf2ec48bc80        training/webapp     "python app.py"     33 minutes ago      Up 33 minutes       0.0.0.0:32768->5000/tcp   mad_tesla
root@ubuntu-demo:/home/cloud-user# docker stop mad_tesla
mad_tesla

Checking if the webapplication effectively stopped:

root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
baf2ec48bc80        training/webapp     "python app.py"          34 minutes ago      Exited (137) 8 seconds ago                           mad_tesla
b8a0540c2b11        ubuntu              "/bin/sh -c 'while tr"   About an hour ago   Exited (137) About an hour ago                       elegant_jones
009b46701d4a        ubuntu              "/bin/echo 'Hello Wor"   About an hour ago   Exited (0) About an hour ago                         lonely_jang
a383bfe27397        ubuntu              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                         amazing_wing
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   About an hour ago   Exited (0) About an hour ago                         cranky_davinci

In order to restart the container again, just issue the ‘docker start ‘ command.

root@ubuntu-demo:/home/cloud-user# docker start mad_tesla
mad_tesla
root@ubuntu-demo:/home/cloud-user# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
baf2ec48bc80        training/webapp     "python app.py"     34 minutes ago      Up 3 seconds        0.0.0.0:32769->5000/tcp   mad_tesla

Let’s say we don’t need the container any longer. We can just remove it using the ‘docker rm ‘ command. However, it’s not that easy. You first have to ensure to stop the container first before you could remove it. Below is the console output going through this process.

root@ubuntu-demo:/home/cloud-user# docker rm mad_tesla
Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
Error: failed to remove containers: [mad_tesla]
root@ubuntu-demo:/home/cloud-user#
root@ubuntu-demo:/home/cloud-user# docker stop mad_tesla

mad_tesla
root@ubuntu-demo:/home/cloud-user# docker rm mad_tesla
mad_tesla
root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
b8a0540c2b11        ubuntu              "/bin/sh -c 'while tr"   About an hour ago   Exited (137) About an hour ago                       elegant_jones
009b46701d4a        ubuntu              "/bin/echo 'Hello Wor"   About an hour ago   Exited (0) About an hour ago                         lonely_jang
a383bfe27397        ubuntu              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                         amazing_wing
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   2 hours ago         Exited (0) 2 hours ago                               cranky_davinci

Leave a Reply

Your email address will not be published. Required fields are marked *