[![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) * [Proxy on Windows](#proxy-on-windows) * [Validating and unit testing the module](#validating-and-unit-testing-the-module) 3. [Usage - Configuration options and additional functionality](#usage) * [Images](#images) * [Containers](#containers) * [Networks](#networks) * [Volumes](#volumes) * [Compose](#compose) * [Machine](#machine) * [Swarm mode](#swarm-mode) * [Tasks](#tasks) * [Docker services](#docker-services) * [Private registries](#private-registries) * [Exec](#exec) * [Plugins](#plugins) 4. [Reference - An under-the-hood peek at what the module is doing and how](#reference) 5. [Limitations - OS compatibility, etc.](#limitations) 6. [License](#license) 7. [Development - Guide for contributing to the module](#development) 8. [Acceptance](#acceptance) ## Description 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. 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:2375'], socket_bind => 'unix:///var/run/docker.sock', ip_forward => true, iptables => true, ip_masq => true, bip => '192.168.1.1/24', fixed_cidr => '192.168.1.144/28', } ``` For more information about the configuration options for the default docker bridge, see the [Docker documentation](https://docs.docker.com/v17.09/engine/userguide/networking/default_network/custom-docker0/). The default group ownership of the Unix control socket differs based on OS. For example, on RHEL using docker-ce packages >=18.09.1, the socket file used by /usr/lib/systemd/system/docker.socket is owned by the docker group. To override this value in /etc/sysconfig/docker and docker.socket (e.g. to use the 'root' group): ```puppet class {'docker': socket_group => 'root', socket_override => true, } ``` The socket_group parameter also takes a boolean for legacy cases where setting -G in /etc/sysconfig/docker is not desired: ```puppet docker::socket_group: false ``` To add another service to the After= line in the [Unit] section of the systemd /etc/systemd/system/service-overrides.conf file, use the service_after_override parameter: ```puppet docker::service_after_override: containerd.service ``` 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 pass additional parameters to the daemon, add `extra_parameters` to the manifest file: ```puppet class { 'docker': extra_parameters => ['--experimental=true', '--metrics-addr=localhost:9323'], ``` 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 } ``` If the curl package is being managed elsewhere and the curl ensure in this module is conflicting, it can be disabled by setting the following parameter globally or in compose / machine resources: ```puppet class { 'docker': curl_ensure => false } ``` ### 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', } ```` ### Validating and unit testing the module This module is compliant with the Puppet Development Kit [(PDK)](https://puppet.com/docs/pdk/1.x/pdk.html), which provides tools to help run unit tests on the module and validate the modules' metadata, syntax, and style. To run all validations against this module, run the following command: ```sh pdk validate ``` To change validation behavior, add options flags to the command. For a complete list of command options and usage information, see the PDK command [reference](https://puppet.com/docs/pdk/1.x/pdk_reference.html#pdk-validate-command). To unit test the module, run the following command: ```sh pdk test unit ``` To change unit test behavior, add option flags to the command. For a complete list of command options and usage information, see the PDK command [reference](https://puppet.com/docs/pdk/1.x/pdk_reference.html#pdk-test-unit-command). ## 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','my-user-def-net-2'], 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_stop => 'echo "container has stopped"', after_start => 'echo "container has started"', after => [ 'container_b', 'mysql' ], depends => [ 'container_a', 'postgres' ], stop_wait_time => 10, 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. Use the `detach` param to run a container without the `-a` flag. This is only required on systems without `systemd`. This default is set in the params.pp based on the OS. Only override if you understand the consequences and have a specific use case. To execute a command before the container starts or stops, specify the `before_start` or `before_stop` parameters, respectively. Similarly, you can set the `after_start` or `after_stop` parameters to run a command after the container starts or stops. 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 dependent 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 is used whether it's 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. Therefore, it's 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. 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 => '