Here is a playbook for Ansible to install Docker and Docker Compose on Debian.
You can of course, use a role from Galaxy. You have Gerling’s version or Debops (Ansible stack for Debian).
But here I wanted to implement the install guide from Docker – and translate this into an Ansible playbook (which then should be turn into a custom role for more reusability).
The official guide give use the following install steps:
- Update the apt package index
- Install packages to allow apt to use a repository over HTTPS
- Add Docker’s official GPG key
- Add Docker’s official Apt Repository
- Then refresh the apt index
- Install docker from the package
Yeah, that’s a few steps. But that’s the best way to make sure you always have the last version of docker. Docker is a project that is moving fast and you cannot wait for your favourite distrib to give you the right package, it will always be dirty old version you don’t want. Maybe in some future when things get a bit more stable it will be enough to install it from debian default – but for now that’s really a bad idea.
So, let’s cut the chase here is a the playbook – with comments to make things easy to follow if anything is unclear.
|
|
Now this works pretty fine – but we still need to add docker compose – so we do the same – we follow the official documentation – which surprisingly enough do not provide a package but instruct us to curl something – not even with a “latest” tag… so this will require some maintaining – which is annoying but will do for now.
So the step are simple:
- Download the latest version of Docker Compose
- Add executable permissions to the binary
For this we use the module get_url
from Ansible – which replace the curl
and allow us to also define the permissions on the fly.
|
|
And that’s it.
As you can see we also made use of {{ansible_system}} and {{ ansible_userspace_architecture }} to replace respectively uname -s
and uname -m
– if you want a full list of Ansible variables available – you can go check the doc here.
That’s it!