Docker: Run applications on Docker Swarm using CLI and Dockerfile

By | 17/04/2016

Introduction

In the previous post, we created a Swarm Cluster on Virtualbox. The Swarm cluster was consisting of:

  • Swarm Manager (swarm-manager)
  • Swarm Node 1 (swarm-node1)
  • Swarm Node 2 (swarm-node2)

In this post, we will experiment with running some containers. Some of them pretty easy and just using Docker Engine. Others we will try to run with Docker Compose. Let’s get started!

Start with a simple container

Let’s start out very simple. We will run a simple container on the Swarm Cluster. You might remember we also did it in this post but the difference was that it was done on a single, independent node.

WAUTERW-M-G007:Downloads wauterw$ docker run -t -i ubuntu /bin/bash
root@dccf2656ae94:/#

Let’s now verify on which node the Swarm Manager decided to run our simple container.

WAUTERW-M-G007:Downloads wauterw$ eval $(docker-machine env --swarm swarm-manager)
WAUTERW-M-G007:Downloads wauterw$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS                                     NAMES
dccf2656ae94        ubuntu              "/bin/bash"              About a minute ago   Exited (0) 13 seconds ago                                             swarm-node01/pensive_minsky
c65805511449        swarm:latest        "/swarm join --advert"   32 minutes ago       Up 32 minutes               2375/tcp                                  swarm-node02/swarm-agent
a293eeeda6d7        swarm:latest        "/swarm join --advert"   34 minutes ago       Up 34 minutes               2375/tcp                                  swarm-node01/swarm-agent
5b50f145fe2d        swarm:latest        "/swarm join --advert"   36 minutes ago       Up 36 minutes               2375/tcp                                  swarm-manager/swarm-agent
b82aca867319        swarm:latest        "/swarm manage --tlsv"   36 minutes ago       Up 36 minutes               2375/tcp, 192.168.99.107:3376->3376/tcp   swarm-manager/swarm-agent-master
WAUTERW-M-G007:Downloads wauterw$

It looks like it was decided to run it on swarm-node01. Note that this container has stopped. We knew that, since as soon as the main process is finished, also the container will be stopped.

Continuing with another simple hello world container

Next, we will deploy the Hello World container. Again very straightforward.

WAUTERW-M-G007:Downloads wauterw$ docker run ubuntu /bin/echo 'Hello world'
Hello world

Again, let’s see where the Swarm Manager decided to launch the container.

WAUTERW-M-G007:Downloads wauterw$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                                     NAMES
e3f30dbe5a2d        ubuntu              "/bin/echo 'Hello wor"   2 minutes ago       Exited (0) 2 minutes ago                                                 swarm-node02/dreamy_babbage
dccf2656ae94        ubuntu              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                                             swarm-node01/pensive_minsky
c65805511449        swarm:latest        "/swarm join --advert"   About an hour ago   Up About an hour               2375/tcp                                  swarm-node02/swarm-agent
a293eeeda6d7        swarm:latest        "/swarm join --advert"   About an hour ago   Up About an hour               2375/tcp                                  swarm-node01/swarm-agent
5b50f145fe2d        swarm:latest        "/swarm join --advert"   About an hour ago   Up About an hour               2375/tcp                                  swarm-manager/swarm-agent
b82aca867319        swarm:latest        "/swarm manage --tlsv"   About an hour ago   Up About an hour               2375/tcp, 192.168.99.107:3376->3376/tcp   swarm-manager/swarm-agent-master

This time apparently, it decided to have it launched on swarm-node02. Kind of expected I would think.

Run webapplication via Dockerfile on Swarm cluster

In this post, we already ran a simple webapplication that returned ‘Hello World’. Again easy, but it might be interesting just to see how Dockerfile is working together with the swarm cluster. To follow along, use the index.js, package.json and Dockerfile from that post. It should work just as fine.

First thing we have to do is to build the image again.

WAUTERW-M-G007:app wauterw$ docker build -t ubuntu-express-app-swarm .
....
Removing intermediate container 1cb1e19c884f
Step 12 : COPY . /usr/src/app
 ---> 0a36f070eaa0
Removing intermediate container a037fd63840e
Step 13 : EXPOSE 3001
 ---> Running in a34da1195a7c
 ---> f1c4ab91bb06
Removing intermediate container a34da1195a7c
Step 14 : CMD nodejs /usr/src/app/index.js
 ---> Running in 5b0da2b34986
 ---> b7fc7dfa402e
Removing intermediate container 5b0da2b34986
Successfully built b7fc7dfa402e

Then we check again on the containers and where they have ran:

WAUTERW-M-G007:app wauterw$ docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                            PORTS                                     NAMES
a1a5e003691a        ubuntu-express-app-swarm   "nodejs /usr/src/app/"   2 minutes ago       Exited (130) About a minute ago                                             swarm-manager/kickass_goldberg
e3f30dbe5a2d        ubuntu                     "/bin/echo 'Hello wor"   23 minutes ago      Exited (0) 23 minutes ago                                                   swarm-node02/dreamy_babbage
dccf2656ae94        ubuntu                     "/bin/bash"              About an hour ago   Exited (0) About an hour ago                                                swarm-node01/pensive_minsky
c65805511449        swarm:latest               "/swarm join --advert"   About an hour ago   Up About an hour                  2375/tcp                                  swarm-node02/swarm-agent
a293eeeda6d7        swarm:latest               "/swarm join --advert"   About an hour ago   Up About an hour                  2375/tcp                                  swarm-node01/swarm-agent
5b50f145fe2d        swarm:latest               "/swarm join --advert"   2 hours ago         Up 2 hours                        2375/tcp                                  swarm-manager/swarm-agent
b82aca867319        swarm:latest               "/swarm manage --tlsv"   2 hours ago         Up 2 hours                        2375/tcp, 192.168.99.107:3376->3376/tcp   swarm-manager/swarm-agent-master

This is something I did not expect. Seems like the express app was launched on the swarm-manager itself. I thought honestly that this was just a manager, that would have all containers run on the underlying node. But apparently I was wrong in this assumption.
Just to be sure, I tried it again. This time I ran the container in detached mode

WAUTERW-M-G007:app wauterw$ docker run -it -d -p 3002:3001 ubuntu-express-app-swarm
9f5030c981c942129c2df555dffba86fd42ff721d5b33a757af93cf47f6d3f2c
WAUTERW-M-G007:app wauterw$ docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                         PORTS                                     NAMES
9f5030c981c9        ubuntu-express-app-swarm   "nodejs /usr/src/app/"   5 seconds ago       Up 4 seconds                   192.168.99.107:3002->3001/tcp             swarm-manager/condescending_bartik
a1a5e003691a        ubuntu-express-app-swarm   "nodejs /usr/src/app/"   6 minutes ago       Exited (130) 6 minutes ago                                               swarm-manager/kickass_goldberg
e3f30dbe5a2d        ubuntu                     "/bin/echo 'Hello wor"   27 minutes ago      Exited (0) 27 minutes ago                                                swarm-node02/dreamy_babbage
dccf2656ae94        ubuntu                     "/bin/bash"              About an hour ago   Exited (0) About an hour ago                                             swarm-node01/pensive_minsky
c65805511449        swarm:latest               "/swarm join --advert"   2 hours ago         Up 2 hours                     2375/tcp                                  swarm-node02/swarm-agent
a293eeeda6d7        swarm:latest               "/swarm join --advert"   2 hours ago         Up 2 hours                     2375/tcp                                  swarm-node01/swarm-agent
5b50f145fe2d        swarm:latest               "/swarm join --advert"   2 hours ago         Up 2 hours                     2375/tcp                                  swarm-manager/swarm-agent
b82aca867319        swarm:latest               "/swarm manage --tlsv"   2 hours ago         Up 2 hours                     2375/tcp, 192.168.99.107:3376->3376/tcp   swarm-manager/swarm-agent-master

And again, it points it to the swarm-manager! Not sure if this is supposed to be like that or that I’m doing something wrong. I will continue to investigate it and update this post if I find out something.

Leave a Reply

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