Network overlays

In our example, we launched a simple service running a single application. Say we wanted to add a database layer in our application, which is typically a fixed point within the network; how could we do this?

Docker Swarm's network overlay layer extends the network you launch your containers in across multiple hosts, meaning that each service or stack can be launched into its own isolated network. This means that our database container, running MongoDB, will be accessible to all other containers running on the same overlay network on port 27017, no matter which of the hosts the containers are running on.

You may be thinking to yourself Hang on a minute; does this mean I have to hard-code an IP address into my application's configuration? Well that wouldn't fit well with the problems Docker Swarm is trying to resolve, so no, you don't.

Each overlay network has its own inbuilt DNS service, which means that every container launched within the network is able to resolve the hostname of another container within the same network to its currently assigned IP address. This means that when we configure our application to connect to our database instance, we simply need to tell it to connect to, say, mongodb:27017, and it will connect to our MongoDB container.

This will make our diagram look like the following:

There are some other considerations you will need to take into account when adopting this pattern, but we will cover those in Chapter 12, Docker Workflows.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset