SaltStack and Docker with reusable salt states

SaltStack can be used to provision almost anything. I also find it useful for provisioning of Docker containers and orchestration. However, with a lot of salt states, I’ve noticed that there are a lot of similarities in them. Salt states for building images are almost the same for every Docker image. The best solution I found is using SaltStack macros and to make some sort of templates which are then reusable salt states for building any Docker image.

In below examples I’m using SaltStack version v2015.8.5 with dockerng state module and Docker engine version 1.10 running on Ubuntu 14.04. In some future posts, I will provide GitHub repository with all those examples and detailed solution for orchestration using SaltStack on how to deploy private registry, install all dependencies and docker engine based on salt minion roles.

Using SaltStack macros is pretty simple. Here is the build template saltbase/salt/docker/build_template.sls which will build Docker image and push it to the private registry:

This template will build each Docker image from Dockerfile available locally on repo path. Then, I have some global variables which are also used in multiple salt states and can be easily included in any other salt state saltbase/salt/docker/global_vars.jinja:

Basically, just getting some values from salt pillars. Last part is using build template with Ubuntu build salt state saltbase/salt/docker/ubuntu/build.sls:

Then you can just execute Ubuntu build salt state and custom Ubuntu image will be created locally and pushed to defined Docker private registry. Then, lets say I need to build the Postgres image, but Postgres Dockerfile has a FROM command which requires cron/ubuntu image to be available FROM cron/ubuntu. This can be done with a simple include statement in Postgres build salt state saltbase/salt/docker/postgres/build.sls:

When you execute Postgres build salt state sudo salt-call -l debug state.sls both images will be built, first Ubuntu and then Postgres.

Build templates can also be used for starting Docker containers and it is possible to create a new macro template which already have included macro template inside. More on this topic in some future SaltStack and Docker orchestration tutorials.

Follow me

Alen Komljen

Building and automating infrastructure with Docker, Kubernetes, kops, Helm, Rancher, Terraform, Ansible, SaltStack, Jenkins, AWS, GKE and many others.
Follow me

Latest posts by Alen Komljen (see all)

Alen KomljenSaltStack and Docker with reusable salt states