The Docker Cookbook provides resources for installing docker as well as building, managing, and running docker containers.
This cookbook is concerned with the Docker container engine as distributed by Docker, Inc. It does not address Docker ecosystem tooling or prerequisite technology such as cgroups or aufs.
This cookbook is maintained by the Sous Chefs. The Sous Chefs are a community of Chef cookbook maintainers working together to maintain important cookbooks. If you’d like to know more please visit sous-chefs.org or come chat with us on the Chef Community Slack in #sous-chefs.
- Network accessible web server hosting the docker binary.
- SELinux permissive/disabled if CentOS Docker Issue #15498
- Amazon Linux 2
- Debian 9/10/11
- Fedora
- Ubuntu 18.04/20.04/22.04
- CentOS 7/8
This cookbook automatically sets up the upstream Docker package repositories. If you would like to use your own repositories this functionality can be disabled and you can instead setup the repos yourself with yum_repository/apt_repository resources or the chef-apt-docker / chef-yum-docker cookbooks.
If you are not using the official docker repositories you may run into issues with the docker group being different. RHEL is a known issue that defaults to using dockerroot for the service group. Add the group property to the docker_service.
docker_service'default'dogroup'dockerroot'action[:create,:start]end- Add
depends 'docker'to your cookbook's metadata.rb - Use the resources shipped in cookbook in a recipe, the same way you'd use core Chef resources (file, template, directory, package, etc).
docker_service'default'doaction[:create,:start]enddocker_image'busybox'doaction:pullenddocker_container'an-echo-server'dorepo'busybox'port'1234:1234'command"nc -ll -p 1234 -e /bin/cat"endThe cookbooks run by test-kitchen make excellent usage examples.
Those recipes are found at test/cookbooks/docker_test.
- docker_service: composite resource that uses docker_installation and docker_service_manager
- docker_container: container operations
- docker_exec: execute commands inside running containers
- docker_image: image/repository operations
- docker_image_prune: remove unused docker images
- docker_installation_package: install Docker via package 'docker-ce'
- docker_installation_script: install Docker via curl | bash
- docker_installation_tarball: install Docker from a tarball
- docker_network: network operations
- docker_plugin: plugin operations
- docker_registry: registry operations
- docker_service_manager_execute: manage docker daemon with Chef
- docker_service_manager_systemd: manage docker daemon with systemd unit files
- docker_tag: image tagging operations
- docker_volume: volume operations
- docker_volume_prune: remove unused docker volumes
- docker_swarm_init: initialize a new Docker swarm cluster
- docker_swarm_join: join a node to a Docker swarm cluster
- docker_swarm_service: manage Docker swarm services
- docker_swarm_token: manage Docker swarm tokens
Here's a quick example of pulling the latest image and running a container with exposed ports.
# Pull latest imagedocker_image'nginx'dotag'latest'action:pullnotifies:redeploy,'docker_container[my_nginx]'end# Run container mapping containers port 80 to the host's port 80docker_container'my_nginx'dorepo'nginx'tag'latest'port['80:80']host_name'www'domain_name'computers.biz'env'FOO=bar'volumes['/some/local/files/:/etc/nginx/conf.d']endYou might run a private registry and multiple Docker hosts.
# Login to private registrydocker_registry'https://registry.computers.biz/'dousername'shipper'password'iloveshipping'email'[email protected]'end# Pull tagged imagedocker_image'registry.computers.biz:443/my_project/my_container'dotag'latest'action:pullhost'tcp://host-1.computers.biz:2376'end# Run containerdocker_container'crowsnest'dorepo'registry.computers.biz:443/my_project/my_container'tag'latest'host'tcp://host-2.computers.biz:2376'tls_verifytruetls_ca_cert"/path/to/ca.pem"tls_client_cert"/path/to/cert.pem"tls_client_key"/path/to/key.pem"action:runendYou can manipulate Docker volumes and networks
docker_network'my_network'dosubnet'10.9.8.0/24'gateway'10.9.8.1'enddocker_volume'my_volume'doaction:createenddocker_container'my_container'dorepo'alpine'tag'3.1'command"nc -ll -p 1234 -e /bin/cat"volumes'my_volume:/my_data'network_mode'my_network'action:runendSee full documentation for each resource and action below for more information.
The docker_installation resource auto-selects one of the below resources with the provider resolution system.
docker_installation'default'The docker_installation_tarball resource copies the precompiled Go binary tarball onto the disk. It should not be used in production, especially with devicemapper.
docker_installation_tarball'default'doversion'1.11.0'source'https://my.computers.biz/dist/docker.tgz'checksum'97a3f5924b0b831a310efa8bf0a4c91956cd6387c4a8667d27e2b2dd3da67e4d'action:createendversion- The desired version of docker to fetch.channel- The docker channel to fetch the tarball from. Default: stablesource- Path to network accessible Docker binary tarball. Ignores version when set.checksum- SHA-256 checksum of the tarball file.
The docker_installation_script resource runs the script hosted by Docker, Inc at http://get.docker.com. It configures package repositories and installs a dynamically compiled binary.
docker_installation_script'default'dorepo'main'script_url'https://my.computers.biz/dist/scripts/docker.sh'action:createendrepo- One of 'main', 'test', or 'experimental'. Used to calculate script_url in its absence. Defaults to 'main'script_url- 'URL of script to pipe into /bin/sh as root.
The docker_installation_package resource uses the system package manager to install Docker. It relies on the pre-configuration of the system's package repositories. The chef-yum-docker and chef-apt-docker Supermarket cookbooks can be used to use Docker's own repositories.
This is the recommended production installation method.
docker_installation_package'default'doversion'20.10.11'action:createpackage_options%q|--force-yes -o Dpkg::Options::='--force-confold' -o Dpkg::Options::='--force-all'|# if Ubuntu for exampleendversion- Used to calculate package_version string. This needs to be the complete version (19.03.8).package_version- Manually specify the package version stringpackage_name- Name of package to install. Defaults to 'docker-ce'package_options- Manually specify additional options, like apt-get directives for examplesetup_docker_repo- Setup the download.docker.com repo. If you would like to manage the repo yourself so you can use an internal repo then set this to false. default: true on all platforms except Amazon Linux.repo_channel- The channel of docker to setup from download.docker.com. Only used ifsetup_docker_repois true. default: 'stable'
The docker_service_manager resource auto-selects a strategy from the docker_service_manager_* group of resources based on platform and version. The docker_service family share a common set of properties.
docker_service_manager'default'doaction:startenddocker_service_manager_execute'default'doaction:startenddocker_service_manager_systemd'default'dohost['unix:///var/run/docker.sock','tcp://127.0.0.1:2376']tls_verifytruetls_ca_cert"/path/to/ca.pem"tls_server_cert"/path/to/server.pem"tls_server_key"/path/to/server-key.pem"tls_client_cert"/path/to/cert.pem"tls_client_key"/path/to/key.pem"systemd_opts["TasksMax=infinity","MountFlags=private"]systemd_socket_opts["Accept=yes"]action:startendThis project exists thanks to all the people who contribute.
Thank you to all our backers!
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.