Monthly Archives: February 2016

Docker: start, stop and remove containers

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

Docker: running simple Hello World web application

This post will focus on running an easy web application in a Docker container. We will run a simple web application that displays ‘Hello World’ in a web browser.

In order to do so, we will run a Docker container with the training/webapp base image which can be found here. Running the following command (see previous tutorial also) will launch a container with a Python Flask web app that returns ‘Hello World’ upon visiting the root URL.

root@ubuntu-demo:/home/cloud-user# docker run -d -P training/webapp python app.py
Unable to find image 'training/webapp:latest' locally
latest: Pulling from training/webapp
2880a3395ede: Pull complete
515565c29c94: Pull complete
98b15185dba7: Pull complete
2ce633e3e9c9: Pull complete
2ee0b8f351f7: Pull complete
2505b734adda: Pull complete
20dd0c759013: Pull complete
f95ebd363bf2: Pull complete
1952e3bf3d7e: Pull complete
abb991a4ed5e: Pull complete
7cbae6914197: Pull complete
f74dd040041e: Pull complete
54bb4e8718e8: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
baf2ec48bc809e58e9c23f187167a01877814d658c95dc50e7c8463a2885e77d

The big question now is how can I see the output using a web browser. After all, we were told this was a web application. So, the answer is pretty simple in fact. When running the ‘docker ps’ command, we see now also a PORTS section. It tells us that docker is binding the port ‘32768’ of our host system to port ‘5000’ on the container. Note that port 5000 is the default port for the Python Flask application.

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

So we can now go ahead and test if we can see the web application return the Hello World string. As I’m running on an Openstack environment, I will need to modify my security group to allow access to port 32768. If you are running on a localhost, this might me needed. After opening port 32768, I can visit the :32768 and you’ll see something similar to the below screenshot. Just means that everything worked successfully. Great!
WebAppDocker

Note: a potentially easier way to return the port mapping is by doing:

root@ubuntu-demo:/home/cloud-user# docker port mad_tesla
5000/tcp -> 0.0.0.0:32768

Let’s take a look at the logs generated by our containerised web application. Easy enough!

root@ubuntu-demo:/home/cloud-user# docker logs -f mad_tesla
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
10.55.164.119 - - [13/Apr/2016 11:49:39] "GET / HTTP/1.1" 200 -
10.55.164.119 - - [13/Apr/2016 11:49:40] "GET /favicon.ico HTTP/1.1" 404 -
10.55.164.119 - - [13/Apr/2016 11:59:51] "GET / HTTP/1.1" 200 -
10.55.164.119 - - [13/Apr/2016 11:59:51] "GET /favicon.ico HTTP/1.1" 404 -

Docker: running basic containers (hello world style)

After installing and updating the docker daemon, it’s about time to get our hands dirty and run some real applications. As you know by know, I always start easy and will fallback onto the Hello World type of applications. In later tutorials, I plan to go a bit deeper and run maybe some Express or Sinatra webapp. That’s for later, let’s start with some simpler stuff first.

Let’s first assure we are running from a newly installed and clean docker platform. Using below command, you’ll see that no containers are currently running.

root@ubuntu-demo:/home/cloud-user# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@ubuntu-demo:/home/cloud-user#

Also, no containers have run in the past:

root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@ubuntu-demo:/home/cloud-user#

As per the documentation available on the docker website, we could start with a container providing back the Hello World statement. You could do this as follows:

root@ubuntu-demo:/home/cloud-user# docker run ubuntu /bin/echo 'Hello world'
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
1fef4ef98b66: Pull complete
afaba416316a: Pull complete
5a62bb966323: Pull complete
c9ea60d0b905: Pull complete
Digest: sha256:f6e8757419147ba099af8cec4db365b3603472bd6182722b16fdae932c0bf3bf
Status: Downloaded newer image for ubuntu:latest
Hello world

Here you can see that we specified an image ‘ubuntu’ that will be OS our container is using. As we did not download the ubuntu image before, it will start the pull the latest version from the docker repository. We also said the daemon to execute the /bin/echo command and output Hello World. Pretty easy…but maybe also a bit useless. When we look at the ‘docker ps -a’ command, we can see that the container was launched, the output was generated but then it got terminated again.

oot@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   4 minutes ago       Exited (0) 4 minutes ago                       cranky_davinci

We could also run interactive containers so we could have a command prompt inside the container. You can do this by specifying the -it options. The -i flag makes the container interactive by lettings us see the STDIN of the container and -t will give you a terminal.

root@ubuntu-demo:/home/cloud-user# docker run -t -i ubuntu /bin/bash
root@a383bfe27397:/#

So you’ll see that the prompt has changed to something different. Actually, this means you are inside of the container with short container id a383bfe27397 and can execute the regular commands you’re probably used to in Ubuntu (pwd, cat, ps, ls -a, apt-get update ….). You can return back to you host system with the exit command. You will also see that the container was terminated once you exited the terminal (pay attention to the container id a383bfe27397 in an ‘exited’ state)

root@a383bfe27397:/# exit
exit
root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
a383bfe27397        ubuntu              "/bin/bash"              2 minutes ago       Exited (0) 24 seconds ago                       amazing_wing
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   9 minutes ago       Exited (0) 9 minutes ago                        cranky_davinci
root@ubuntu-demo:/home/cloud-user#

Would be good that when we launch a container that we get back the prompt of our host system so we could run some docker commands again. We can do this by running the container in daemonized mode. Just specify the -d option to the docker command.

root@ubuntu-demo:/home/cloud-user# docker run -d ubuntu /bin/echo 'Hello World again'
009b46701d4a2b13607d717d028144304e2fb34a76b4ad464ee9e3aa819b296a

Checking again (see below output) it seems that our container is exited. The reason for this is that a docker container exits when its main process finishes. In our case, once the ‘Hello World again’ sentence was printed, it would stop the container since the main process finished.

root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
009b46701d4a        ubuntu              "/bin/echo 'Hello Wor"   2 minutes ago       Exited (0) 2 minutes ago                        lonely_jang
a383bfe27397        ubuntu              "/bin/bash"              7 minutes ago       Exited (0) 5 minutes ago                        amazing_wing
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   14 minutes ago      Exited (0) 14 minutes ago                       cranky_davinci
root@ubuntu-demo:/home/cloud-user#

Again, the fact that the container is terminated is relatively useless so let’s see if it helps if we put some kind of loop statement in our docker run command.

root@ubuntu-demo:/home/cloud-user# docker run -d ubuntu /bin/sh -c "while true; do echo Hello World; sleep 1; done"
b8a0540c2b11d16ae90c314537d19b778989460d65aeb612908dbd393417f204

Let’s see if this container keeps running:

root@ubuntu-demo:/home/cloud-user# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
b8a0540c2b11        ubuntu              "/bin/sh -c 'while tr"   19 seconds ago      Up 19 seconds                           elegant_jones
root@ubuntu-demo:/home/cloud-user#
root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
b8a0540c2b11        ubuntu              "/bin/sh -c 'while tr"   50 seconds ago      Up 49 seconds                                   elegant_jones
009b46701d4a        ubuntu              "/bin/echo 'Hello Wor"   9 minutes ago       Exited (0) 9 minutes ago                        lonely_jang
a383bfe27397        ubuntu              "/bin/bash"              14 minutes ago      Exited (0) 12 minutes ago                       amazing_wing
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   21 minutes ago      Exited (0) 21 minutes ago                       cranky_davinci

Bingo. Both commands indicate the container is still running. In fact, we can also log into the container to see if it has output something.

root@ubuntu-demo:/home/cloud-user# docker logs elegant_jones
Hello World
Hello World
Hello World
Hello World
Hello World
...

Note, you could also use the container id instead of the name ‘elegant_jones’. So docker logs b8a0540c2b11 also works perfectly well.

To stop the container, do the following:

root@ubuntu-demo:/home/cloud-user# docker stop b8a0540c2b11
b8a0540c2b11
root@ubuntu-demo:/home/cloud-user# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@ubuntu-demo:/home/cloud-user#
root@ubuntu-demo:/home/cloud-user#
root@ubuntu-demo:/home/cloud-user# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
b8a0540c2b11        ubuntu              "/bin/sh -c 'while tr"   4 minutes ago       Exited (137) 11 seconds ago                       elegant_jones
009b46701d4a        ubuntu              "/bin/echo 'Hello Wor"   13 minutes ago      Exited (0) 13 minutes ago                         lonely_jang
a383bfe27397        ubuntu              "/bin/bash"              18 minutes ago      Exited (0) 16 minutes ago                         amazing_wing
15af5e22af95        ubuntu              "/bin/echo 'Hello wor"   25 minutes ago      Exited (0) 25 minutes ago                         cranky_davinci

You will see that the container with name elegant_jones (or id b8a0540c2b11) was terminated successfully.

Let’s leave it at this for now. Hope to see you back later!