Now that we have a base configuration of setting up a Docker host, let us move to the next level and set up a Docker Swarm cluster automatically:
name 'dockerhost' # ... version '0.1.0' depends 'docker', '~> 4.7' depends 'chef-vault', '~> 3.1' # update here
- Next, we will create a Chef recipe for initializing the manager node in
cookbooks/dockerhost/recipes/manager.rb
:
include_recipe 'dockerhost::base' execute 'init swarm' do command 'docker swarm init' not_if 'docker info -f "{{.Swarm.LocalNodeState}}" | egrep "^active"' end
Note that we used Chef's guard clauses to prevent docker swarm init
from running multiple times. This makes our Chef recipe idempotent.
- Back in our base recipe,
cookbooks/dockerhost/recipes/base.rb
...