Docker: running basic containers (hello world style)

By | 10/02/2016

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!

Leave a Reply

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