File Coverage

blib/lib/WWW/Docker/Container.pm
Criterion Covered Total %
statement 12 34 35.2
branch 6 8 75.0
condition n/a
subroutine 3 14 21.4
pod 0 12 0.0
total 21 68 30.8


line stmt bran cond sub pod time code
1             package WWW::Docker::Container;
2             # ABSTRACT: Docker container entity
3              
4 10     10   62 use Moo;
  10         18  
  10         58  
5 10     10   3389 use namespace::clean;
  10         42  
  10         99  
6              
7             our $VERSION = '0.101';
8              
9             =head1 SYNOPSIS
10              
11             my $docker = WWW::Docker->new;
12              
13             # Get container from list or inspect
14             my $containers = $docker->containers->list;
15             my $container = $containers->[0];
16              
17             # Access container properties
18             say $container->Id;
19             say $container->Status;
20             say $container->Image;
21              
22             # Perform operations
23             $container->start;
24             $container->stop(timeout => 10);
25             my $logs = $container->logs(tail => 100);
26             $container->remove(force => 1);
27              
28             # Check state
29             if ($container->is_running) {
30             say "Container is running";
31             }
32              
33             =head1 DESCRIPTION
34              
35             This class represents a Docker container and provides convenient access to
36             container properties and operations. Instances are returned by
37             L methods like C and C.
38              
39             Each attribute corresponds to fields in the Docker API container representation.
40             Methods delegate to L for operations.
41              
42             =cut
43              
44             has client => (
45             is => 'ro',
46             weak_ref => 1,
47             );
48              
49             =attr client
50              
51             Reference to L client. Used for delegating operations.
52              
53             =cut
54              
55             has Id => (is => 'ro');
56              
57             =attr Id
58              
59             Container ID (64-character hex string).
60              
61             =cut
62              
63             has Names => (is => 'ro');
64              
65             =attr Names
66              
67             ArrayRef of container names (from C).
68              
69             =cut
70              
71             has Image => (is => 'ro');
72              
73             =attr Image
74              
75             Image name used to create the container.
76              
77             =cut
78              
79             has ImageID => (is => 'ro');
80             has Command => (is => 'ro');
81             has Created => (is => 'ro');
82              
83             =attr Created
84              
85             Container creation timestamp (Unix epoch).
86              
87             =cut
88              
89             has State => (is => 'ro');
90              
91             =attr State
92              
93             Container state. From C: string like C, C. From
94             C: hashref with C, C, C, etc.
95              
96             =cut
97              
98             has Status => (is => 'ro');
99              
100             =attr Status
101              
102             Human-readable status string (e.g., "Up 2 hours").
103              
104             =cut
105              
106             has Ports => (is => 'ro');
107             has Labels => (is => 'ro');
108             has SizeRw => (is => 'ro');
109             has SizeRootFs => (is => 'ro');
110             has HostConfig => (is => 'ro');
111             has NetworkSettings => (is => 'ro');
112             has Mounts => (is => 'ro');
113              
114             # Attributes from inspect response
115             has Name => (is => 'ro');
116              
117             =attr Name
118              
119             Container name (from C, includes leading C).
120              
121             =cut
122              
123             has RestartCount => (is => 'ro');
124             has Driver => (is => 'ro');
125             has Platform => (is => 'ro');
126             has Path => (is => 'ro');
127             has Args => (is => 'ro');
128             has Config => (is => 'ro');
129              
130             sub start {
131 0     0 0 0 my ($self) = @_;
132 0         0 return $self->client->containers->start($self->Id);
133             }
134              
135             =method start
136              
137             $container->start;
138              
139             Start the container. Delegates to L.
140              
141             =cut
142              
143             sub stop {
144 0     0 0 0 my ($self, %opts) = @_;
145 0         0 return $self->client->containers->stop($self->Id, %opts);
146             }
147              
148             =method stop
149              
150             $container->stop(timeout => 10);
151              
152             Stop the container. Delegates to L.
153              
154             =cut
155              
156             sub restart {
157 0     0 0 0 my ($self, %opts) = @_;
158 0         0 return $self->client->containers->restart($self->Id, %opts);
159             }
160              
161             =method restart
162              
163             $container->restart;
164              
165             Restart the container.
166              
167             =cut
168              
169             sub kill {
170 0     0 0 0 my ($self, %opts) = @_;
171 0         0 return $self->client->containers->kill($self->Id, %opts);
172             }
173              
174             =method kill
175              
176             $container->kill(signal => 'SIGTERM');
177              
178             Send a signal to the container.
179              
180             =cut
181              
182             sub remove {
183 0     0 0 0 my ($self, %opts) = @_;
184 0         0 return $self->client->containers->remove($self->Id, %opts);
185             }
186              
187             =method remove
188              
189             $container->remove(force => 1);
190              
191             Remove the container.
192              
193             =cut
194              
195             sub logs {
196 0     0 0 0 my ($self, %opts) = @_;
197 0         0 return $self->client->containers->logs($self->Id, %opts);
198             }
199              
200             =method logs
201              
202             my $logs = $container->logs(tail => 100);
203              
204             Get container logs.
205              
206             =cut
207              
208             sub inspect {
209 0     0 0 0 my ($self) = @_;
210 0         0 return $self->client->containers->inspect($self->Id);
211             }
212              
213             =method inspect
214              
215             my $updated = $container->inspect;
216              
217             Get fresh container information.
218              
219             =cut
220              
221             sub pause {
222 0     0 0 0 my ($self) = @_;
223 0         0 return $self->client->containers->pause($self->Id);
224             }
225              
226             =method pause
227              
228             $container->pause;
229              
230             Pause all processes in the container.
231              
232             =cut
233              
234             sub unpause {
235 0     0 0 0 my ($self) = @_;
236 0         0 return $self->client->containers->unpause($self->Id);
237             }
238              
239             =method unpause
240              
241             $container->unpause;
242              
243             Unpause the container.
244              
245             =cut
246              
247             sub top {
248 0     0 0 0 my ($self, %opts) = @_;
249 0         0 return $self->client->containers->top($self->Id, %opts);
250             }
251              
252             =method top
253              
254             my $processes = $container->top;
255              
256             List running processes in the container.
257              
258             =cut
259              
260             sub stats {
261 0     0 0 0 my ($self, %opts) = @_;
262 0         0 return $self->client->containers->stats($self->Id, %opts);
263             }
264              
265             =method stats
266              
267             my $stats = $container->stats;
268              
269             Get resource usage statistics.
270              
271             =cut
272              
273             sub is_running {
274 3     3 0 7779 my ($self) = @_;
275 3         9 my $state = $self->State;
276 3 50       11 return 0 unless defined $state;
277 3 100       14 if (ref $state eq 'HASH') {
278 1 50       28 return $state->{Running} ? 1 : 0;
279             }
280 2 100       17 return lc($state) eq 'running' ? 1 : 0;
281             }
282              
283             =method is_running
284              
285             if ($container->is_running) { ... }
286              
287             Returns true if container is running, false otherwise. Works with both C
288             and C response formats.
289              
290             =cut
291              
292             =seealso
293              
294             =over
295              
296             =item * L - Container API operations
297              
298             =item * L - Main Docker client
299              
300             =back
301              
302             =cut
303              
304             1;