[![Build Status](https://travis-ci.org/puppetlabs/puppetlabs-docker.svg?branch=master)](https://travis-ci.org/puppetlabs/puppetlabs-docker) [![Puppet Forge](https://img.shields.io/puppetforge/v/puppetlabs/docker.svg)](https://forge.puppetlabs.com/puppetlabs/docker) [![Puppet Forge Downloads](http://img.shields.io/puppetforge/dt/puppetlabs/docker.svg)](https://forge.puppetlabs.com/puppetlabs/docker) [![Puppet Forge Endorsement](https://img.shields.io/puppetforge/e/puppetlabs/docker.svg)](https://forge.puppetlabs.com/puppetlabs/docker) # Docker #### Table of Contents 1. [Description](#description) 2. [Setup](#setup) 3. [Usage - Configuration options and additional functionality](#usage) * [Images](#images) * [Containers](#containers) * [Networks](#networks) * [Volumes](#volumes) * [Compose](#compose) * [Swarm mode](#swarmmode) * [Tasks](#tasks) * [Docker services](#dockerservices) * [Private registries](#privateregistries) * [Exec](#exec) * [Plugins](#plugins) 4. [Reference - An under-the-hood peek at what the module is doing and how](#reference) * [Classes](#classes) * [Defined types](#definedtypes) * [Types](#types) * [Parameters](#parameters) 5. [Limitations - OS compatibility, etc.](#limitations) 6. [Development - Guide for contributing to the module](#development) ## Overview The Puppet docker module installs, configures, and manages [Docker](https://github.com/docker/docker) from the [Docker repository](https://docs.docker.com/installation/). It supports the latest [Docker CE (Community Edition)](https://www.docker.com/community-edition) for Linux based distributions and [Docker EE(Enterprise Edition)](https://www.docker.com/enterprise-edition) for Windows and Linux as well as legacy releases. ## Description This module install, configures, and manages [Docker](https://github.com/docker/docker). Due to the new naming convention for Docker packages, this module prefaces any params that refer to the release with `_ce` or `_engine`. Examples of these are documented in this README. ## Setup To create the Docker hosted repository and install the Docker package, add a single class to the manifest file: ```puppet include 'docker' ``` To configure package sources independently and disable automatically including sources, add the following code to the manifest file: ```puppet class { 'docker': use_upstream_package_source => false, } ``` The latest Docker [repositories](https://docs.docker.com/engine/installation/linux/docker-ce/debian/#set-up-the-repository) are now the default repositories for version 17.06 and above. If you are using an older version, the repositories are still configured based on the version number passed into the module. To ensure the module configures the latest repositories, add the following code to the manifest file: ```puppet class { 'docker': version => '17.09.0~ce-0~debian', } ``` Using a version prior to 17.06, configures and installs from the old repositories: ```puppet class { 'docker': version => '1.12.0-0~wheezy', } ``` Docker provides a enterprise addition of the [Docker Engine](https://www.docker.com/enterprise-edition), called Docker EE. To install Docker EE on Debian systems, add the following code to the manifest file: ```puppet class { 'docker': docker_ee => true, docker_ee_source_location => 'https://', docker_ee_key_source => 'https://', docker_ee_key_id => '', } ``` To install Docker EE on RHEL/CentOS: ```puppet class { 'docker': docker_ee => true, docker_ee_source_location => 'https://', docker_ee_key_source => 'https://', } ``` For CentOS distributions, the docker module requires packages from the extras repository which is enabled by default on CentOS. For more information, see the official [CentOS documentation](https://wiki.centos.org/AdditionalResources/Repositories) and the official [Docker documentation](https://docs.docker.com/install/linux/docker-ce/centos/). For Red Hat Enterprise Linux (RHEL) based distributions, the docker module uses the upstream repositories. To continue using the legacy distribution packages in the CentOS extras repository, add the following code to the manifest file: ```puppet class { 'docker': use_upstream_package_source => false, service_overrides_template => false, docker_ce_package_name => 'docker', } ``` To use the CE packages, add the following code to the manifest file: ```puppet class { 'docker': use_upstream_package_source => false, repo_opt => '', } ``` By default, the Docker daemon binds to a unix socket at `/var/run/docker.sock`. To change this parameter and update the binding parameter to a tcp socket, add the following code to the manifest file: ```puppet 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', } ``` When setting up TLS, upload the related files (CA certificate, server certificate, and key) and include their paths in the manifest file: ```puppet 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', } ``` To specify which Docker rpm package to install, add the following code to the manifest file: ```puppet class { 'docker' : manage_package => true, use_upstream_package_source => false, package_engine_name => 'docker-engine' package_source_location => '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' ] } ``` To track the latest version of Docker, add the following code to the manifest file: ```puppet class { 'docker': version => 'latest', } ``` To install docker from a test or edge channel, add the following code to the manifest file: ```puppet class { 'docker': docker_ce_channel => 'test' } ``` To allocate a dns server to the Docker daemon, add the following code to the manifest file: ```puppet class { 'docker': dns => '8.8.8.8', } ``` To add users to the Docker group, add the following array to the manifest file: ```puppet class { 'docker': docker_users => ['user1', 'user2'], } ``` To add daemon labels, add the following array to the manifest file: ```puppet class { 'docker': labels => ['storage=ssd','stage=production'], } ``` To uninstall docker, add the following to the manifest file: ```puppet class { 'docker': ensure => absent } ``` Only Docker EE is supported on Windows. To install docker on Windows 2016 and above the `docker_ee` parameter must be specified: ```puppet class { 'docker': docker_ee => true } ``` ### Proxy on Windows To use docker through a proxy on Windows, a System Environment Variable HTTP_PROXY/HTTPS_PROXY must be set. See [Docker Engine on Windows](https://docs.microsoft.com/en-us/virtualization/windowscontainers/manage-docker/configure-docker-daemon#proxy-configuration) This can be done using a different puppet module such as the puppet-windows_env module. After setting the variable, the docker service must be restarted. ```puppet windows_env { 'HTTP_PROXY' value => 'http://1.2.3.4:80', notify => Service['docker'], } windows_env { 'HTTPS_PROXY' value => 'http://1.2.3.4:80', notify => Service['docker'], } service { 'docker' ensure => 'running', } ```` ## Usage ### Images Each image requires a unique name; otherwise, the installation fails when a duplicate name is detected. To install a Docker image, add the `docker::image` defined type to the manifest file: ```puppet docker::image { 'base': } ``` The code above is equivalent to running the `docker pull base` command. However, it removes the default five-minute execution timeout. To include an optional parameter for installing image tags that is the equivalent to running `docker pull -t="precise" ubuntu`, add the following code to the manifest file: ```puppet docker::image { 'ubuntu': image_tag => 'precise' } ``` Including the `docker_file` parameter is equivalent to running the `docker build -t ubuntu - < /tmp/Dockerfile` command. To add or build an image from a dockerfile that includes the `docker_file` parameter, add the following code to the manifest file: ```puppet docker::image { 'ubuntu': docker_file => '/tmp/Dockerfile' } ``` Including the `docker_dir` parameter is equivalent to running the `docker build -t ubuntu /tmp/ubuntu_image` command. To add or build an image from a dockerfile that includes the `docker_dir` parameter, add the following code to the manifest file: ```puppet docker::image { 'ubuntu': docker_dir => '/tmp/ubuntu_image' } ``` To rebuild an image, subscribe to external events such as Dockerfile changes by adding the following code to the manifest file: ```puppet docker::image { 'ubuntu': docker_file => '/tmp/Dockerfile' subscribe => File['/tmp/Dockerfile'], } file { '/tmp/Dockerfile': ensure => file, source => 'puppet:///modules/someModule/Dockerfile', } ``` To remove an image, add the following code to the manifest file: ```puppet docker::image { 'base': ensure => 'absent' } docker::image { 'ubuntu': ensure => 'absent', image_tag => 'precise' } ``` To configure the `docker::images` class when using Hiera, add the following code to the manifest file: ```yaml --- classes: - docker::images docker::images::images: ubuntu: image_tag: 'precise' ``` ### Containers To launch containers, add the following code to the manifest file: ```puppet docker::run { 'helloworld': image => 'base', command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"', } ``` This is equivalent to running the `docker run -d base /bin/sh -c "while true; do echo hello world; sleep 1; done"` command to launch a Docker container managed by the local init system. `run` includes a number of optional parameters: ```puppet docker::run { 'helloworld': image => 'base', detach => true, service_prefix => 'docker-', 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', disable_network => false, 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'], labels => ['com.example.foo="true"', 'com.example.bar="false"'], 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' ], stop_wait_time => 0, read_only => false, extra_parameters => [ '--restart=always' ], } ``` You can specify the `ports`, `expose`, `env`, `dns`, and `volumes` values with a single string or an array. To pull the image before it starts, specify the `pull_on_start` parameter. To execute a command before the container stops, specify the `before_stop` parameter. Adding the container name to the `after` parameter to specify which containers start first, affects the generation of the `init.d/systemd` script. Add container dependencies to the `depends` parameter. The container starts before this container and stops before the depended container. This affects the generation of the `init.d/systemd` script. Use the `depend_services` parameter to specify dependencies for general services, which are not Docker related, that start before this container. The `extra_parameters` parameter, which contains an array of command line arguments to pass to the `docker run` command, is useful for adding additional or experimental options that the docker module currently does not support. By default, automatic restarting of the service on failure is enabled by the service file for systemd based systems. It's recommended that an image tag is used at all times with the `docker::run` define type. If not, the latest image ise used, whether it be in a remote registry or installed on the server already by the `docker::image` define type. NOTE: As of v3.0.0, if the latest tag is used, the image will be the latest at the time the of the initial puppet run. Any subsequent puppet runs will always reference the latest local image. For this this reason it highly recommended that an alternative tag be used, or the image be removed before pulling latest again. To use an image tag, add the following code to the manifest file: ```puppet docker::run { 'helloworld': image => 'ubuntu:precise', command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"', } ``` By default, when the service stops or starts, the generated init scripts remove the container, but not the associated volumes. To change this behaviour, add the following code to the manifest file: ```puppet 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, you can configure the `docker::run_instance` class: ```yaml --- 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"' ``` To remove a running container, add the following code to the manifest file. This also removes the systemd service file associated with the container. ```puppet docker::run { 'helloworld': ensure => absent, } ``` To enable the restart of an unhealthy container, add the following code to the manifest file. In order to set the health check interval time set the optional health_check_interval parameter, the default health check interval is 30 seconds. ```puppet docker::run { 'helloworld': image => 'base', command => 'command', health_check_cmd => '', restart_on_unhealthy => true, health_check_interval => '