Adding a new application to the test swarm - tooltwist/documentation GitHub Wiki
New applications can be added to the test swarm by starting containers in the swarm, rather than on a specific server. For multi-container applications the preferred approach is to use Compose (See Using Docker Compose to define an application).
First, find the names of the available servers:
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
dev virtualbox Saved
dm-test-4 digitalocean Running tcp://45.55.166.108:2376
phil-master digitalocean Running tcp://128.199.84.133:2376 phil-master (master)
phil-worker-23725 * digitalocean Running tcp://188.166.245.99:2376 phil-master
staging-1 digitalocean Running tcp://188.166.249.200:2376
From here, find the name of the master of the test swarm. In this case I'm using a swarm named phil, so the machine name is phil-master
. We can now set environment variables to access the swarm:
$ eval `docker-machine env --swarm $(cat ../SWARM_NAME)-master`
Note the --swarm
parameter. This is important, because is sets the environment to access the swarm manager container running on the master server, rather than accessing Docker on the master server.
With this environment set, all the normal docker commands work, but refer to the entire swarm rather than a single Docker machine. The ps
command will show all the containers within the swarm, except for those used to manage the container.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfd3b77301aa tutum/hello-world "/bin/sh -c 'php-fpm 25 minutes ago Up 25 minutes 188.166.245.99:32773->80/tcp phil-worker-23725/desperate_kilby
8516769cb0c1 nginx-myproxy "nginx" 38 minutes ago Up 38 minutes 128.199.84.133:80->80/tcp phil-master/nginx_reverseproxy
81a8042f587d docker-gen "/usr/bin/docker-gen 39 minutes ago Up 39 minutes phil-master/docker-gen
6e30c7e1f424 tooltwist/ttsite:30sep2015 "/sbin/my_init" 7 hours ago Up 7 hours 22/tcp, 39393/tcp, 128.199.84.133:32768->8080/tcp phil-master/swarm2_ttdemo_1
Notice the swarm node listed as part of the container name.
Routing web requests
As you start your applications you will see the containers added to the swarm, but they may move from machine to machine resulting in different IP addresses, and their ports may change. This prevents a DNS entry from being directing requests to the container.
To get around this we use a reverse proxy named nginx, which can be seen as the container named phil-master/nginx_reverseproxy in the list above. The reverse proxy looks at the domain name used by the incoming request and forwards it to the IP address and port of the relevant web server.
To enable this functionality a web server container must provide information that is automatically used to update the routing rules of nginx (by the docker-gen container). This is done by setting an environment value named VHOST
when the web server container is started.
The following example starts a simple 'hello world' server to be called when the URL used to call the service is myhello.com
.
$ docker run -d --env "VHOST=myhello.com" -p 80 tutum/hello-world
You will need to change your DNS entry to . A common configuration is:
- an A level record (e.g. xyz.com) pointing to the IP Address of the machine running nginx container.
- a CNAME record (e.g. *.xyz.com) pointing to the A level record.
For testing purposes this allows you to create any number of domain names (e.g. server1.xyz.com, server2.xyz.com, etc) to test the swarm functionality. Remember that domains can take several hours to propagate around the Internet.
If you don't have access to a domain name that you can point to the test swarm, create an entry in your /etc/hosts file, like the following:
128.199.84.133 myHello.com
For an application defined using a docker-compose.yml
file, the VHOST environment variable can be defined as in this example:
ttdemo:
image: tooltwist/ttsite:30sep2015
links:
- db
ports:
- ":8080"
environment:
- "VHOST=mydomain.com"
db:
image: mysql
Note that the proxy will route to the first defined port for the container. In most cases it is best that there is only one port defined for the web server container.