Multi-container Applications using Docker Compose and Swarm

Docker Compose to Orchestrate Containers shows how to run two linked Docker containers using Docker Compose. Clustering Using Docker Swarm shows how to configure a Docker Swarm cluster.

This blog will show how to run a multi-container application created using Docker Compose in a Docker Swarm cluster.

Updated version of Docker Compose and Docker Swarm are released with Docker 1.7.0.

Docker 1.7.0 CLI

Get the latest Docker CLI:

and check the version as:

Docker Machine 0.3.0

Get the latest Docker Machine as:

and check the version as:

Docker Compose 1.3.0

Get the latest Docker Compose as:

and verify the version as:

Docker Swarm 0.3.0

Swarm is run as a Docker container and can be downloaded as:

You can learn about Docker Swarm at docs.docker.com/swarm or Clustering using Docker Swarm.

Create Docker Swarm Cluster

The key components of Docker Swarm are shown below:

and explained in Clustering Using Docker Swarm.

  1. The easiest way of getting started with Swarm is by using the official Docker image:
    This command returns a discovery token, referred as <TOKEN> in this document, and is the unique cluster id. It will be used when creating master and nodes later. This cluster id is returned by the hosted discovery service on Docker Hub.

    It shows the output as:

    The last line is the <TOKEN>.

    Make sure to note this cluster id now as there is no means to list it later. This should be fixed with#661.

  2. Swarm is fully integrated with Docker Machine, and so is the easiest way to get started. Let’s create a Swarm Master next:

    Replace <TOKEN> with the cluster id obtained in the previous step.

    --swarm configures the machine with Swarm, --swarm-master configures the created machine to be Swarm master. Swarm master creation talks to the hosted service on Docker Hub and informs that a master is created in the cluster.

  3. Connect to this newly created master and find some more information about it:

    This will show the output as:

  4. Create a Swarm node

    Replace <TOKEN> with the cluster id obtained in an earlier step.

    Node creation talks to the hosted service at Docker Hub and joins the previously created cluster. This is specified by --swarm-discovery token://... and specifying the cluster id obtained earlier.

  5. To make it a real cluster, let’s create a second node:

    Replace <TOKEN> with the cluster id obtained in the previous step.

  6. List all the nodes created so far:

    This shows the output similar to the one below:

    The machines that are part of the cluster have the cluster’s name in the SWARM column, blank otherwise. For example, “lab” and “summit2015” are standalone machines where as all other machines are part of the “swarm-master” cluster. The Swarm master is also identified by (master) in the SWARM column.

  7. Connect to the Swarm cluster and find some information about it:

    This shows the output as:

    There are 3 nodes – one Swarm master and 2 Swarm nodes. There is a total of 4 containers running in this cluster – one Swarm agent on master and each node, and there is an additional swarm-agent-master running on the master.

  8. List nodes in the cluster with the following command:

    This shows the output as:

Deploy Java EE Application to Docker Swarm Cluster using Docker Compose

Docker Compose to Orchestrate Containers explains how multi container applications can be easily started using Docker Compose.

  1. Use the docker-compose.yml file explained in that blog to start the containers as:
    The docker-compose.yml file looks like:
  2. Check the containers running in the cluster as:
    to see the output as:
  3. “swarm-node-02” is running three containers and so lets look at the list of containers running there:
    and see the list of running containers as:
  4. Application can then be accessed again using:
    and shows the output as:

Latest instructions for this setup are always available at: github.com/javaee-samples/docker-java/blob/master/chapters/docker-swarm.adoc.

Enjoy!

Be Sociable, Share!
  • Tweet

11 thoughts on “Multi-container Applications using Docker Compose and Swarm

  1. the biggest question I still have with running Java webapps on multiple containers is how to automate discovery of available containers and making them available for users via load balancing

  2. @Thomas: still learning here, but what you need to google for is called “Service Discovery”. Look for tools like Consul/Registrator/consul-haproxy for examples.

    Basically, each app publishes where they are deployed to some central place (that is: consul, or etcd…). That can be done automatically from outside the container with tools like Registrator. Dependencies can typically be done using DNS and the DNS interface of tools like Consul (see http://2mohitarora.blogspot.fr/2014/08/service-discovery-using-consul.html to understand how for example).

    HTH

    Cheers

  3. in step 3 about node-02 : eval “$(docker-machine env swarm-node-02)” and docker ps -a, i get no docker image form cluster :
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    7b89a35e5c38 swarm “/swarm list token://” 17 minutes ago Exited (0) 17 minutes ago suspicious_hawking
    049a9526f9f3 swarm:latest “/swarm join –advert” 30 minutes ago Up 30 minutes 2375/tcp swarm-agent
    whet does it happen ?
    here is the docker info of the swarm-master node :
    stage:wildfly-mysql-javaee7 root# eval “$(docker-machine env –swarm swarm-master)”
    stage:wildfly-mysql-javaee7 root# docker info
    Containers: 8
    Images: 9
    Role: primary
    Strategy: spread
    Filters: affinity, health, constraint, port, dependency
    Nodes: 3
    swarm-master: 192.168.99.101:2376
    └ Containers: 3
    └ Reserved CPUs: 0 / 1
    └ Reserved Memory: 0 B / 1.022 GiB
    └ Labels: executiondriver=native-0.2, kernelversion=4.0.9-boot2docker, operatingsystem=Boot2Docker 1.8.1 (TCL 6.3); master : 7f12e95 – Thu Aug 13 03:24:56 UTC 2015, provider=virtualbox, storagedriver=aufs
    swarm-node-01: 192.168.99.102:2376
    └ Containers: 3
    └ Reserved CPUs: 0 / 1
    └ Reserved Memory: 0 B / 1.022 GiB
    └ Labels: executiondriver=native-0.2, kernelversion=4.0.9-boot2docker, operatingsystem=Boot2Docker 1.8.1 (TCL 6.3); master : 7f12e95 – Thu Aug 13 03:24:56 UTC 2015, provider=virtualbox, storagedriver=aufs
    swarm-node-02: 192.168.99.103:2376
    └ Containers: 2
    └ Reserved CPUs: 0 / 1
    └ Reserved Memory: 0 B / 1.022 GiB
    └ Labels: executiondriver=native-0.2, kernelversion=4.0.9-boot2docker, operatingsystem=Boot2Docker 1.8.1 (TCL 6.3); master : 7f12e95 – Thu Aug 13 03:24:56 UTC 2015, provider=virtualbox, storagedriver=aufs
    CPUs: 3
    Total Memory: 3.065 GiB
    Name: d3ca82e151d6

  4. and it run on swarm-node-01 :
    stage:~ root# curl http://$(docker-machine ip swarm-node-01):9080/employees/resources/employees
    1Penny2Sheldon3Amy4Leonard5Bernadette6Raj7Howard8Priyastage:~ root#

  5. Eric,

    “docker ps -a” will give you the list of Docker containers, not the images.

    The behavior is quite consistent with what I’m seeing as well. What is your exact question?

  6. The link to the image at the beginning of “Create Docker Swarm Cluster” part seems to be broken.
    (https://github.com/javaee-samples/docker-java/raw/master/images/docker-swarm-
    components.png)

  7. It might be a good idea to make it clear the examples here will only work on Mac or Windows, “docker-machine” is for Mac and Windows as far as I can see, there is no docker-machine for Linux

  8. I will strongly recommended this website. The are best. Fast delivery. Good price. Any problem, they will contact you right away.

Leave a Reply

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