Skip to content

Instantly share code, notes, and snippets.

@mkfares
Created August 12, 2020 17:16
Show Gist options
  • Save mkfares/b1eb06ad33a655e7524bf17da4f94392 to your computer and use it in GitHub Desktop.
Save mkfares/b1eb06ad33a655e7524bf17da4f94392 to your computer and use it in GitHub Desktop.
Docker Swarm - Stacks

Docker Swarm - Managing Stacks

A docker stack represents an application with multiple services such as database, API, and front-end services. The services are defined in a docker compose file format.

Managing stacks consists of deploying of a stack, listing the services and tasks in a stack, and removing a stack. All these commands need to be executed on a docker manager.

The format of the command to manage a stack is as follows:

$ docker stack [options] command

Docker compose file

The docker swarm supports the version 3.0 and above of the docker compose file.

Docker swarm ignores the build commands in the docker compose file. Only docker compose can build images.

Docker swarm ignores some instructions in the docker compose file. See the docker compose file documentation to know more about the ignored instructions.

Prepare the voting application

The voting application is an example of multi-service applications. The application uses Python, Node.js, .NET Core, with Redis for messaging and PostgreSQL for storage.

To get the source code of the application with the docker yaml files, clone the following GitHub repository then change to the created directory.

$ git clone https://github.com/dockersamples/example-voting-app.git
$ cd example-voting-app

Deploy a new stack

After initiation of the swarm mode, A stack can be created, or deployed, on the swarm using a compose file. The deploy command has to be run on a swarm manager.

The deploy command is also used to update a stack after a change in stack configuration.

To initiate the docker swarm mode.

$ docker swarm init

The following commands instruct the deployment of a stack named "vote" using the docker-stack.yml file as configuration.

$ docker stack deploy [options] <stack-name>
$ docker stack deploy --compose-file docker-stack.yml vote

The voting app (python) is accessed on the link: http://localhost:5000

The results of the vote can be accessed on the link: http://localhost:5001

List existing stacks

The ls command lists the stacks running on a swarm. This command is executed on a swarm manager.

$ docker stack ls

The command displays the names and number of services in each stack.

List tasks in a stack

The ps command displays the running tasks that belong to a specific tasks. This command is executed on a swarm manager.

docker stack ps [options] <stack-name>
docker stack ps vote

Options:
-q : Limit the output to tasks Ids only
--no-truc: The output of the command is not truncated. The digests and error messages are shown.

List services in a stack

The services command lists the running services under a specific task.This command is executed on a swarm manager.

$ docker stack services [options] <task-name>
$ docker stack services vote

The command displays the service id, name, mode, number of replicas, image name, and exposed ports.

Remove a stack

The rm command removes a stack from a swarm. This command is executed on a swarm manager. The services, networks, and secrets associated with the stack will be also removed.

$ docker stack rm vote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment