Swarm Services

Let's learn the application aspect of services.

Everything we do in this lesson gets improved by Docker Stacks. However, it’s important that you learn the concepts here so that you’re prepared for it.

Introduction

Like we said in the Swarm Primer; services are a new construct introduced with Docker 1.12, and they only apply to swarm mode.

Services let us specify most of the familiar container options, such as name, port mappings, attaching to networks, and images. But they add important cloud-native features, including desired state and automatic reconciliation. For example, swarm services allow us to declaratively define the desired state for an application that we can apply to the swarm and let the swarm take care of deploying it and managing it.

Example

Let’s look at a quick example. Assume you have an app with a web front-end. You have an image for the web server, and testing has shown that you need 5 instances to handle normal daily traffic. You translate this requirement into a single service declaring the image to use, and that the service should always have 5 running replicas. You issue that to the swarm as your desired state, and the swarm takes care of ensuring there are always 5 instances of the web server running.

Creating services

We’ll see some of the other things that can be declared as part of a service in a minute, but before we do that, let’s see one way to create what we just described.

You can create services in one of two ways:

  1. Imperatively on the command line with docker service create
  2. Declaratively with a stack file

We’ll look at stack files in a later chapter. For now, we’ll focus on the imperative method.

Note: The command to create a new service is the same on Windows. However, the image used in this example is a Linux image and will not work on Windows. You can substitute the image for a Windows web server image and the command will work. Remember, if you are typing Windows commands from a PowerShell terminal you will need to use the backtick (`) to indicate continuation on the next line.

Get hands-on with 1200+ tech skills courses.