Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit c81e386

Browse files
committed
Add support for pid_mode
Also adds a bit of coverage for network_mode.
1 parent e172b22 commit c81e386

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ In `host` and `container...` network modes, you may specify a
271271
for container health checks. The mapping itself, while still passed via the API,
272272
will be ignored by Docker.
273273

274+
### PID modes
275+
276+
You may specify the PID mode you would like a container to use via:
277+
278+
```ruby
279+
set :pid_mode, 'pidmode'
280+
```
281+
282+
Docker (and therefore Centurion) supports one of nothing (the default), `host`,
283+
and `container:<container-id>` for this argument.
284+
274285
### CGroup Resource Constraints
275286

276287
Limits on memory and CPU can be specified with the `memory` and `cpu_shares`

lib/centurion/service.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Centurion
55
class Service
66
extend ::Capistrano::DSL
77

8-
attr_accessor :command, :dns, :extra_hosts, :image, :name, :volumes, :port_bindings, :network_mode, :cap_adds, :cap_drops, :ipc_mode, :security_opt
8+
attr_accessor :command, :dns, :extra_hosts, :image, :name, :volumes, :port_bindings, :network_mode, :pid_mode, :cap_adds, :cap_drops, :ipc_mode, :security_opt
99
attr_reader :memory, :cpu_shares, :env_vars, :labels
1010

1111
def initialize(name)
@@ -35,6 +35,7 @@ def self.from_env
3535
s.volumes = fetch(:binds, [])
3636
s.port_bindings = fetch(:port_bindings, [])
3737
s.network_mode = fetch(:network_mode, 'bridge')
38+
s.pid_mode = fetch(:pid_mode, nil)
3839
s.command = fetch(:command, nil)
3940
s.memory = fetch(:memory, 0)
4041
s.cpu_shares = fetch(:cpu_shares, 0)
@@ -76,10 +77,6 @@ def cap_drops=(capabilites)
7677
@cap_drops = capabilites
7778
end
7879

79-
def network_mode=(mode)
80-
@network_mode = mode
81-
end
82-
8380
def memory=(bytes)
8481
if !bytes || !is_a_uint64?(bytes)
8582
raise ArgumentError, "invalid value for cgroup memory constraint: #{bytes}, value must be a between 0 and 18446744073709551615"
@@ -159,6 +156,9 @@ def build_host_config(restart_policy = nil)
159156
# Set the network mode
160157
host_config['NetworkMode'] = network_mode
161158

159+
# Set the pid mode if specified
160+
host_config['PidMode'] = pid_mode if pid_mode
161+
162162
# DNS if specified
163163
host_config['Dns'] = dns if dns
164164

lib/centurion/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Centurion
2-
VERSION = '1.9.2'
2+
VERSION = '1.10.0'
33
end

spec/service_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
set(:port_bindings, [ Centurion::Service::PortBinding.new(12340, 80, 'tcp') ])
1919
set(:labels, labels)
2020
set(:security_opt, ['seccomp=unconfined'])
21+
set(:network_mode, 'host')
22+
set(:pid_mode, 'host')
2123

2224
svc = Centurion::Service.from_env
2325
expect(svc.name).to eq('mycontainer')
@@ -29,6 +31,8 @@
2931
expect(svc.port_bindings.first.container_port).to eq(80)
3032
expect(svc.labels).to eq(labels)
3133
expect(svc.security_opt).to eq(['seccomp=unconfined'])
34+
expect(svc.network_mode).to eq('host')
35+
expect(svc.pid_mode).to eq('host')
3236
end
3337

3438
it 'starts with a command' do
@@ -64,6 +68,14 @@
6468
expect(service.dns).to eq('redis.example.com')
6569
end
6670

71+
it 'has a default network mode' do
72+
expect(service.network_mode).to eq('bridge')
73+
end
74+
75+
it 'has no default pid mode' do
76+
expect(service.pid_mode).to be(nil)
77+
end
78+
6779
it 'boots from a docker image' do
6880
service.image = 'registry.hub.docker.com/library/redis'
6981
expect(service.image).to eq('registry.hub.docker.com/library/redis')
@@ -309,4 +321,35 @@
309321
})
310322
end
311323

324+
it 'builds docker configuration for container-linked pids' do
325+
service.pid_mode = 'container:a2e8937b'
326+
expect(service.build_host_config(Centurion::Service::RestartPolicy.new('on-failure', 50))).to eq({
327+
'Binds' => [],
328+
'CapAdd' => [],
329+
'CapDrop' => [],
330+
'NetworkMode' => 'bridge',
331+
'PidMode' => 'container:a2e8937b',
332+
'PortBindings' => {},
333+
'RestartPolicy' => {
334+
'Name' => 'on-failure',
335+
'MaximumRetryCount' => 50
336+
}
337+
})
338+
end
339+
340+
it 'builds docker configuration for host pids' do
341+
service.pid_mode = 'host'
342+
expect(service.build_host_config(Centurion::Service::RestartPolicy.new('on-failure', 50))).to eq({
343+
'Binds' => [],
344+
'CapAdd' => [],
345+
'CapDrop' => [],
346+
'NetworkMode' => 'bridge',
347+
'PidMode' => 'host',
348+
'PortBindings' => {},
349+
'RestartPolicy' => {
350+
'Name' => 'on-failure',
351+
'MaximumRetryCount' => 50
352+
}
353+
})
354+
end
312355
end

0 commit comments

Comments
 (0)