Deploying Docker Containers with Puppet
Puppet can also be used to manage Docker container upgrades and configuration changes. Docker has more specific tools for this purpose, but sometimes there are reasons to choose alternatives. See Benefits of Managing Docker Containers with Automation Software.
In this page you will find out what managing Docker with Puppet looks like. All the snippets in this page use the docker
resource type, supported by the Puppet company.
Contents
How to Install, Upgrade or Uninstall Docker with Puppet
Installing or upgrading Docker is simple:
class { 'docker': use_upstream_package_source => false, version => '17.09.0~ce-0~debian', }
In this example we are using our system's repositories instead of Docker official repositories, and we are specifying the desired version. To upgrade Docker later, all we need to do is to modify the version number. While specifying a version is not mandatory, it is a good idea because it makes our manifest more reproducible.
To uninstall Docker:
class { 'docker': ensure => absent }
Check the docker
resource type documentation to find out how to use more features: for example you can use Docker Enterprise Edition, or bind the Docker daemon to a TCP port.
How to Build or Pull Docker Images with Puppet
To pull an image from Dockerhub:
docker::image { 'mariadb:10.0': }
We specified the 10.0
tag to get the desired MariaDB version. If we don't, the image with the latest
tag will be used. Note that this is not desirable in production, because it can lead to unexpected upgrades.
You can also write a Dockerfile yourself, and then build it to create a Docker image. To do so, you need to instruct Puppet to copy the Dockerfile to the target and then build it::
file { '/path/to/remote/Dockerfile': ensure => file, source => 'puppet:///path/to/local/Dockerfile', } docker::image { 'image_name': docker_file => '/path/to/remote/Dockerfile' }
It is also possible to subscribe to Dockerfile changes, and automatically rebuild the image whenever a new file is found:
docker::image { 'image_name': docker_file => '/path/to/remote/Dockerfile' subscribe => File['/path/to/remote/Dockerfile'], }
To remove an image that was possibly built or pulled:
docker::image { 'mariadb': ensure => absent }
How to Deploy Containers with Puppet
To run a container:
docker::run { 'mariadb-01': image => 'mariadb:10.5', ports => ['3306:6606'] }
mariadb-01
is the contained name. We specified the optional 10.5
tag, and we mapped the guest port 3306 to the host port 6606. In production, you normally don't map ports because you don't need to connect MariaDB clients from the host system to MariaDB servers in the containers. Third-party tools can be installed as separate containers.
References
- docker resource type documentation, in Puppet documentation.
Content initially contributed by Vettabase Ltd.