API-Docker
view release on metacpan or search on metacpan
lib/API/Docker/API/Images.pm view on Meta::CPAN
my ($self, %opts) = @_;
croak "container required" unless $opts{container};
my %params;
$params{container} = $opts{container};
$params{repo} = $opts{repo} if defined $opts{repo};
$params{tag} = $opts{tag} if defined $opts{tag};
$params{comment} = $opts{comment} if defined $opts{comment};
$params{author} = $opts{author} if defined $opts{author};
$params{pause} = $opts{pause} ? 1 : 0 if defined $opts{pause};
# `changes` is a repeated query parameter on the wire, but the engine parses
# each value as a Dockerfile snippet and a snippet may span lines, so one
# newline-joined value carries a list just as well. Measured against Podman
# 5.4.2: changes=LABEL%20a%3Db%0AEXPOSE%208080 and two separate changes=
# pairs produce the same image. The joined form is used because it fits the
# transport's one-value-per-key params encoder.
if (defined $opts{changes}) {
$params{changes} = ref $opts{changes} eq 'ARRAY'
? join("\n", @{$opts{changes}})
: $opts{changes};
}
return $self->client->post('/commit', $opts{config},
params => \%params,
%{ $self->_request_options },
);
}
sub build_prune {
my ($self, %opts) = @_;
my %params;
# The engine spells this one with a hyphen, and an unquoted
# `keep-storage => $n` is not even valid Perl -- the fat comma quotes a
# bareword identifier, and keep-storage is a subtraction. So keep_storage is
# the documented spelling, the wire name is accepted beside it for anyone
# copying out of the Engine reference, and the hyphen is what goes on the
# wire. _uri_encode leaves `-` alone, so the key survives unmangled.
my $keep_storage = $opts{keep_storage} // $opts{'keep-storage'};
$params{'keep-storage'} = $keep_storage if defined $keep_storage;
$params{all} = $opts{all} ? 1 : 0 if defined $opts{all};
$params{filters} = $self->_normalise_filters($opts{filters})
if defined $opts{filters};
return $self->client->post('/build/prune', undef,
params => \%params,
%{ $self->_request_options },
);
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
API::Docker::API::Images - Docker Engine Images API
=head1 VERSION
version 0.006
=head1 SYNOPSIS
my $docker = API::Docker->new;
# Build an image from a tar context
use Path::Tiny;
my $tar = path('context.tar')->slurp_raw;
$docker->images->build(context => $tar, t => 'myapp:latest');
# Pull an image
$docker->images->pull(fromImage => 'nginx', tag => 'latest');
# List images
my $images = $docker->images->list;
for my $image (@$images) {
say $image->id;
say join ', ', @{$image->repo_tags};
}
# Inspect image details
my $image = $docker->images->inspect('nginx:latest');
# Tag and push
$docker->images->tag('nginx:latest', repo => 'myrepo/nginx', tag => 'v1');
$docker->images->push('myrepo/nginx', tag => 'v1');
# Remove image
$docker->images->remove('nginx:latest', force => 1);
# Snapshot a container into an image
my $new = $docker->images->commit(container => $id, repo => 'myapp', tag => 'snap');
# Air-gapped roundtrip: export here, carry the tar over, load there
my $export = $docker->images->get('myapp:snap'); # raw tar bytes
$docker->images->load($export);
# Reclaim the build cache (not the same thing as prune)
$docker->images->build_prune(all => 1);
=head1 DESCRIPTION
This module provides methods for managing Docker images including pulling,
listing, tagging, pushing to registries, and removal.
C<list> and C<inspect> return generated L<API::Docker::Type> objects carrying
the convenience methods of L<API::Docker::Role::Entity::Image>, so
C<< $image->tag >> and C<< $image->remove >> work on either. Which class each
returns, and where the two disagree, is below.
Accessed via C<< $docker->images >>, or through
L<API::Docker::Role::Using/using> for a run of calls that needs its own
lib/API/Docker/API/Images.pm view on Meta::CPAN
=back
=head2 build_prune
my $result = $images->build_prune(all => 1);
my $freed = $result->{SpaceReclaimed};
# Keep 5 GB of cache
$images->build_prune(keep_storage => 5 * 1024 * 1024 * 1024);
Clear the BuildKit build cache. B<This is not L</prune>>, and the two are not
interchangeable: L</prune> deletes unused I<images>, this deletes the
intermediate I<build cache> that L</build> writes. Neither touches the other's
storage, and on a machine that builds often the build cache is usually the
larger of the two.
Returns the raw daemon response, a HashRef with C<CachesDeleted> and
C<SpaceReclaimed>.
B<Podman does not implement this endpoint.> Measured against 5.4.2 (API 1.41):
C<POST /build/prune> answers C<404 Not Found> with a C<text/plain> body of
C<Not Found> -- not the JSON C<< {"message":...} >> shape its other errors use
-- at every version prefix tried, and there is no C<libpod> equivalent either.
The transport croaks with C<Docker API error (404): Not Found>, the plain body
verbatim, because it is not JSON to unwrap. A caller that must work on both
engines has to treat that 404 as "no build cache to clear here" rather than as
a transport fault.
Options:
=over
=item * C<keep_storage> - Bytes of cache to keep. Sent as the engine's
C<keep-storage>, which is also accepted as the option name; the underscore
form exists because the hyphenated one has to be quoted in a Perl hash
=item * C<all> - Remove all cache, not just the dangling entries
=item * C<filters> - HashRef of filters, e.g. C<< { until => ['24h'] } >>;
values are ArrayRefs of strings, shape-checked and normalised by
L<API::Docker::Role::Filters>, and passed to the transport unencoded because
it JSON-encodes a HashRef params value itself
=back
=head1 SEE ALSO
=over
=item * L<API::Docker> - Main Docker client
=item * L<API::Docker::Role::Entity::Image> - the convenience methods the
returned objects carry
=item * L<API::Docker::Type::ImageSummary> - the fields C<list> returns
=item * L<API::Docker::Type::ImageInspect> - the fields C<inspect> returns
=item * L<API::Docker::Role::RegistryAuth> - the C<X-Registry-Auth>
encoding C<push> uses, shared with the other registry-facing endpoints
=item * L<API::Docker::Error::Stream> - Raised by C<build>, C<pull>, C<push>
and C<load>
=back
=head1 SUPPORT
=head2 Issues
Please report bugs and feature requests at
L<https://github.com/Getty/p5-api-docker/issues>.
=head1 CONTRIBUTING
Contributions are welcome! Please fork the repository and submit a pull request.
=head1 AUTHOR
Torsten Raudssus <getty@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 0.408 second using v1.01-cache-2.11-cpan-39bc3f1cbff )