diff --git a/README.md b/README.md index e15776a8f..2b40082e9 100755 --- a/README.md +++ b/README.md @@ -216,6 +216,15 @@ docker::image { 'ubuntu': Note: images will only install if an image of that name does not already exist. +You can also remove all the images versions except the single tag you need. This is useful for keeping the disk space usage low. + +```puppet +docker::image { 'ubuntu': + image_tag => 'precise', + image_prune => true, +} +``` + 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` ```puppet @@ -236,7 +245,7 @@ You can trigger a rebuild of the image by subscribing to external events like Do ```puppet docker::image { 'ubuntu': - docker_file => '/tmp/Dockerfile' + docker_file => '/tmp/Dockerfile', subscribe => File['/tmp/Dockerfile'], } diff --git a/manifests/image.pp b/manifests/image.pp index 35b2b97fd..adeb2819c 100644 --- a/manifests/image.pp +++ b/manifests/image.pp @@ -17,6 +17,9 @@ # [*image_digest*] # If you want a specific content digest of the image to be installed # +# [*image_prune*] +# If you want to remove all the images except the specified tag +# # [*docker_file*] # If you want to add a docker image from specific docker file # @@ -28,6 +31,7 @@ $image = $title, $image_tag = undef, $image_digest = undef, + $image_prune = false, $force = false, $docker_file = undef, $docker_dir = undef, @@ -37,6 +41,7 @@ $docker_command = $docker::params::docker_command validate_re($ensure, '^(present|absent|latest)$') validate_re($image, '^[\S]*$') + validate_bool($image_prune) validate_bool($force) # Wrapper used to ensure images are up to date @@ -50,6 +55,10 @@ } ) + if ($image_prune) and (!$image_tag) { + fail 'docker::image $image_prune requires $image_tag to be set' + } + if ($docker_file) and ($docker_dir) { fail 'docker::image must not have both $docker_file and $docker_dir set' } @@ -129,4 +138,17 @@ } } + if $image_tag { + # get images with same image name, but other tags than given + $images_to_prune = "${docker_command} images | egrep '^(docker.io/)?${image} ' | grep -v -P '${image}\\s+${image_tag}' | awk '{ print \$3 }'" + + $images_prune = "${docker_command} rmi -f $($images_to_prune)" + + exec { $images_prune: + environment => 'HOME=/root', + path => ['/bin', '/usr/bin'], + timeout => 0, + onlyif => "$images_to_prune | grep -P '.'", + } + } } diff --git a/spec/defines/image_spec.rb b/spec/defines/image_spec.rb index e5d190e77..bb0e3daf9 100644 --- a/spec/defines/image_spec.rb +++ b/spec/defines/image_spec.rb @@ -168,4 +168,19 @@ }.to raise_error(Puppet::Error) end end + + context 'with image_prune => true' do + let(:params) { { 'image_prune' => true } } + it do + expect { + should have_exec_resource_count(1) + }.to raise_error(Puppet::Error) + end + end + + context 'with image_prune => true and image_tag => precise' do + let(:params) { { 'image_prune' => true , 'image_tag' => 'precise'} } + it { should contain_exec('docker rmi -f $(docker images | egrep '^(docker.io/)?base ' | grep -v -P 'base\s+precise' | awk '{ print \$3 }')') } + end + end