Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'],
}

Expand Down
22 changes: 22 additions & 0 deletions manifests/image.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -28,6 +31,7 @@
$image = $title,
$image_tag = undef,
$image_digest = undef,
$image_prune = false,
$force = false,
$docker_file = undef,
$docker_dir = undef,
Expand All @@ -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
Expand All @@ -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'
}
Expand Down Expand Up @@ -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 '.'",
}
}
}
15 changes: 15 additions & 0 deletions spec/defines/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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