How to Use Docker Stack to Deploy Docker Containers

What is Docker Stack ? Docker Stack can be used to deploy a complete application stack to a docker swarm. This is great for deploying multi tiered applications.

This short article goes through how you can use docker stack deploy to deploy a complete application stack to a docker swarm.

Example Docker Stack Compose File

The first example I will be using is a simple compose file that will create a NGINX and MYSQL container, which mimics the common webserver and database combination, like a lamp stack. The format of the file is the same as a docker compose file.

I’ve saved the above file as web-app.yml, which will be used in the following examples.

Docker Stack Deploy

We can deploy a docker stack using the above file by using the docker stack deploy command, and pointing it to the file we created:

$ docker stack deploy -c web-app.yml webapp

This will create a stack called webapp. When running the deploy command, it will give the following output, based on the services it has created:

Creating network webapp_default
Creating service webapp_web
Creating service webapp_mysql

Along with the nginx and mysql services, it has also created a default network for running the services.

List Docker Stacks, Services and Tasks

You can list the docker stacks with the docker stack ls command:

$ docker stack ls
NAME                SERVICES            ORCHESTRATOR
webapp              2                   Swarm

The services making up the stack can also be listed:

$ docker stack services webapp
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
j0uprwm4oua9        webapp_mysql        replicated          1/1                 mysql:latest        
qg4nqt7sdmo3        webapp_web          replicated          1/1                 nginx:latest        *:8081->80/tcp

The tasks making up the services can be listed with:

$ docker stack ps webapp
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ow1buo3i4mfj webapp_mysql.1 mysql:latest node1 Running Running 12 minutes ago
jnv24yvl3ekz webapp_web.1 nginx:latest node2 Running Running 12 minutes ago

Delete a Docker Stack

A docker stack can be deleted using the docker stack rm command:

$ docker stack rm webapp
Removing service webapp_mysql
Removing service webapp_web
Removing network webapp_default

Creating a WordPress Docker Stack

As a more practical example, we can create a docker stack to provide a wordpress website. For this we can use the following compose file:

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - web_data:/var/www/html
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress

volumes:
    db_data: {}
    web_data: {}

If you’ve seen my article on docker compose you will notice that this is the same file I used when demonstrating docker compose! Used with docker stack, this will create a wordpress instance using the wordpress image, connected to a database on the mysql image. Though rather than all container tasks being ran on the same host, as in the docker compose article, when running as a stack the containers will be distributed across nodes in the docker swarm. Note that this example is only for testing purposes.

Docker Stack vs Docker Compose

This is a fairly common question. As shown above, both docker stack and docker compose use a .yml file to define the docker resources they will create. The command syntax is also similar:

$ docker-compose -f docker-compose up
$ docker stack deploy -c docker-compose.yml web-app

Docker compose is a tool that requires a separate download, whilst the docker stack commands are built into the docker engine and require no additional download.

Both docker-compose and docker stack commands can be used with docker-compose.yml files. Note that docker stack is for deploying onto a docker swarm, whilst docker-compose can be used to deploy to standalone docker nodes.

If you are using Docker Swarm to orchestrate your containers, then you should have a look at using docker stack. One thing to be aware of is that the compose files you use with docker stack must be version 3.

Final Thoughts

We’ve gone through how to deploy using docker stack, how to list and remove docker stacks, and listed some examples of how to deploy multi tiered applications/services. Finally, we also went through some of the similarities and differences between docker stack and docker compose. As always, for more detail on this topic, check out the official documentation – link below!

https://docs.docker.com/engine/swarm/stack-deploy/

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More