Puppet module for managing docker
Puppet module for installing, configuring and managing Docker from the official repository or alternatively from EPEL on RedHat based distributions.
This module is currently tested on:
It may work on other distros and additional operating systems will be supported in the future. It's definitely been used with the following too:
The module includes a single class:
include 'docker'By default this sets up the docker hosted repository if necessary for your OS and installs the docker package and on Ubuntu, any required Kernel extensions.
If you don't want this module to mess about with your Kernel then you can disable this feature like so. It is only enabled (and supported) by default on Ubuntu:
class { 'docker':
manage_kernel => false,
}If you want to configure your package sources independently, inform this module to not auto-include upstream sources (This is already disabled on Archlinux as there is no further upstream):
class { 'docker':
use_upstream_package_source => false,
}Docker recently launched new official repositories which are now the default for the module from version 5. If you want to stick with the old repositories you can do so with the following:
class { 'docker':
package_name => 'lxc-docker',
package_source_location => 'https://get.docker.com/ubuntu',
package_key_source => 'https://get.docker.com/gpg',
package_key => '36A1D7869245C8950F966E92D8576A8BA88D21E',
package_release => 'docker',
}Docker also provide a commercially supported version of the Docker Engine, called Docker CS, available from a separate repository. This can be installed with the module using the following:
class { 'docker':
docker_cs => true,
}The module also now uses the upstream repositories by default for RHEL based distros, including Fedora. If you want to stick with the distro packages you should use the following:
class { 'docker':
use_upstream_package_source => false,
package_name => 'docker',
}By default the docker daemon will bind to a unix socket at /var/run/docker.sock. This can be changed, as well as binding to a tcp socket if required.
class { 'docker':
tcp_bind => ['tcp://127.0.0.1:4243','tcp://10.0.0.1:4243'],
socket_bind => 'unix:///var/run/docker.sock',
ip_forward => true,
iptables => true,
ip_masq => true,
bridge => br0,
fixed_cidr => '10.20.1.0/24',
default_gateway => '10.20.0.1',
}For TLS setup you should upload related files (such as CA certificate, server certificate and key) and use their paths in manifest
class { 'docker':
tcp_bind => ['tcp://0.0.0.0:2376'],
tls_enable => true,
tls_cacert => '/etc/docker/tls/ca.pem',
tls_cert => '/etc/docker/tls/cert.pem',
tls_key => '/etc/docker/tls/key.pem',
}Unless specified this installs the latest version of docker from the docker repository on first run. However if you want to specify a specific version you can do so, unless you are using Archlinux which only supports the latest release. Note that this relies on a package with that version existing in the reposiroty.
class { 'docker':
version => '0.5.5',
}And if you want to install a specific rpm package of docker you can do so:
class { 'docker' :
manage_package => true,
use_upstream_package_source => false,
package_name => 'docker-engine'
package_source => 'https://get.docker.com/rpm/1.7.0/centos-6/RPMS/x86_64/docker-engine-1.7.0-1.el6.x86_64.rpm',
prerequired_packages => [ 'glibc.i686', 'glibc.x86_64', 'sqlite.i686', 'sqlite.x86_64', 'device-mapper', 'device-mapper-libs', 'device-mapper-event-libs', 'device-mapper-event' ]
}And if you want to track the latest version you can do so:
class { 'docker':
version => 'latest',
}In some cases dns resolution won't work well in the container unless you give a dns server to the docker daemon like this:
class { 'docker':
dns => '8.8.8.8',
}To add users to the Docker group you can pass an array like this:
class { 'docker':
docker_users => ['user1', 'user2'],
}To add daemon labels you can pass an array like this:
class { 'docker':
labels => ['storage=ssd','stage=production'],
}The class contains lots of other options, please see the inline code documentation for the full options.
The next step is probably to install a docker image; for this we have a defined type which can be used like so:
docker::image { 'base': }This is equivalent to running docker pull base. This is downloading a large binary so on first run can take a while. For that reason this define turns off the default 5 minute timeout for exec. Takes an optional parameter for installing image tags that is the equivalent to running docker pull -t="precise" ubuntu:
docker::image { 'ubuntu':
image_tag => 'precise'
}Note: images will only install if an image of that name does not already exist.
A images can also be added/build from a dockerfile with the docker_file property, this equivalent to running `docker build -t ubuntu - '/tmp/Dockerfile'
}
Images can also be added/build from a directory containing a dockerfile with the `docker_dir` property, this is equivalent to running `docker build -t ubuntu /tmp/ubuntu_image`
```puppet
docker::image { 'ubuntu':
docker_dir => '/tmp/ubuntu_image'
}You can trigger a rebuild of the image by subscribing to external events like Dockerfile changes:
docker::image { 'ubuntu':
docker_file => '/tmp/Dockerfile'
subscribe => File['/tmp/Dockerfile'],
}
file { '/tmp/Dockerfile':
ensure => file,
source => 'puppet:///modules/someModule/Dockerfile',
}You can also remove images you no longer need with:
docker::image { 'base':
ensure => 'absent'
}
docker::image { 'ubuntu':
ensure => 'absent',
image_tag => 'precise'
}If using hiera, there's a docker::images class you can configure, for example:
---
classes:
- docker::images
docker::images::images:
ubuntu:
image_tag: 'precise'Now you have an image you can launch containers:
docker::run { 'helloworld':
image => 'base',
command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"',
}This is equivalent to running the following:
docker run -d base /bin/sh -c "while true; do echo hello world; sleep 1; done"This will launch a Docker container managed by the local init system.
Run also takes a number of optional parameters:
docker::run { 'helloworld':
image => 'base',
command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"',
ports => ['4444', '4555'],
expose => ['4666', '4777'],
links => ['mysql:db'],
net => 'my-user-def-net',
volumes => ['/var/lib/couchdb', '/var/log'],
volumes_from => '6446ea52fbc9',
memory_limit => '10m', # (format: '', where unit = b, k, m or g)
cpuset => ['0', '3'],
username => 'example',
hostname => 'example.com',
env => ['FOO=BAR', 'FOO2=BAR2'],
env_file => ['/etc/foo', '/etc/bar'],
dns => ['8.8.8.8', '8.8.4.4'],
restart_service => true,
privileged => false,
pull_on_start => false,
before_stop => 'echo "So Long, and Thanks for All the Fish"',
before_start => 'echo "Run this on the host before starting the Docker container"',
after => [ 'container_b', 'mysql' ],
depends => [ 'container_a', 'postgres' ],
extra_parameters => [ '--restart=always' ],
}
…
puppet
docker::run { 'helloworld':
image => 'ubuntu:precise',
command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"',
}By default the generated init scripts will remove the container (but not any associated volumes) when the service is stopped or started. This behaviour can be modified using the following, with defaults shown:
docker::run { 'helloworld':
remove_container_on_start => true,
remove_volume_on_start => false,
remove_container_on_stop => true,
remove_volume_on_stop => false,
}If using hiera, there's a docker::run_instance class you can configure, for example:
---
classes:
- docker::run_instance
docker::run_instance::instance:
helloworld:
image: 'ubuntu:precise'
command: '/bin/sh -c "while true; do echo hello world; sleep 1; done"'As of Docker 1.9.x, Docker has official support for networks. The module
now exposes a type, docker_network, used to manage those. This works
like:
docker_network { 'my-net':
ensure => present,
driver => 'overlay',
subnet => '192.168.1.0/24',
gateway => '192.168.1.1',
ip_range => '192.168.1.4/32',
}Only the name is required, along with an ensure value. If you don't pass
a driver Docker network will use the default bridge. Note that some
networks require the Docker daemon to be configured to use them, for
instance for the overlay network you'll need a cluster store configured.
You can do that on the docker class like so:
extra_parameters => '--cluster-store=://172.17.8.101: --cluster-advertise=:2376'If using hiera, there's a docker::networks class you can configure, for example:
---
classes:
- docker::networks
docker::networks::networks:
local-docker:
ensure: 'present'
subnet: '192.168.1.0/24'
gateway: '192.168.1.1'The network defined can then be used on a docker::run resource with the net parameter.
Docker Compose allows for describing a set of containers in a simple
YAML format, and then running a command to build and run those
containers. The docker_compose type included in the module allows for
using Puppet to run Compose. This means you can have Puppet remediate
any issues and make sure reality matches the model in your Compose
file.
Before using the docker_compose type make sure the docker-compose utility is installed:
class {'docker::compose':
ensure => present,
}Here's an example. Given the following Compose file:
compose_test:
image: ubuntu:14.04
command: /bin/sh -c "while true; do echo hello world; sleep 1; done"That could be added to the machine you're running Puppet using a file
resource or any other means.
Then define
No open issues yet, or sync has not completed.