Skip to content

Commit a204679

Browse files
jacksgtdavejrt
authored andcommitted
docker::services: Fix using multiple published ports (#447)
While the class docker::services already allowed arrays to be specified for `publish` ports, it didn't handle this correctly since this array was simply converted to a string and passed onto Docker, like so: --publish '["80:80", "443:443"]' To correctly publish multiple ports, the `--publish` parameter has to be called specified for each port. --publish '80:80' --publish '443:443' This patch also introduces a new spec test to check this behavior
1 parent 01932d7 commit a204679

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ docker::services {'redis':
799799
}
800800
```
801801

802-
To base the service off an image, include the `image` parameter and include the `publish` parameter to expose the service ports. To set the amount of containers running in the service, include the `replicas` parameter. To attach one or multiple filesystems to the service, use the `mounts` parameter. For information regarding the `extra_params` parameter, see `docker service create --help`.
802+
To base the service off an image, include the `image` parameter and include the `publish` parameter to expose the service port (use an array to specify multiple published ports). To set the amount of containers running in the service, include the `replicas` parameter. To attach one or multiple filesystems to the service, use the `mounts` parameter. For information regarding the `extra_params` parameter, see `docker service create --help`.
803803

804804
To update the service, add the following code to the manifest file:
805805

lib/puppet/parser/functions/docker_service_flags.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ module Puppet::Parser::Functions
3636
end
3737
end
3838

39-
if opts['publish'] && opts['publish'].to_s != 'undef'
39+
if opts['publish'].is_a? Array
40+
opts['publish'].each do |port|
41+
flags << "--publish #{port}"
42+
end
43+
elsif opts['publish'].to_s != 'undef'
4044
flags << "--publish '#{opts['publish']}'"
4145
end
4246

manifests/services.pp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# Defaults to []
2727
#
2828
# [*publish*]
29-
# Publish a port as a node port.
29+
# Publish port(s) as node ports.
3030
# Defaults to undef
3131
#
3232
# [*replicas*]
@@ -66,6 +66,7 @@
6666
# [*registry_mirror*]
6767
# This will allow the service to set a registry mirror.
6868
# defaults to undef
69+
#
6970
# [*mounts*]
7071
# Allows attacking filesystem mounts to the service (specified as an array)
7172
# defaults to []

spec/defines/services_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@
4141
it { should contain_exec('test_service docker service create').with_command(/docker service create/) }
4242
it { should contain_exec('test_service_2 docker service create').with_command(/docker service create/) }
4343
end
44+
45+
context 'multiple publish ports' do
46+
let(:pre_condition) {
47+
"
48+
docker::services { 'test_service_3':
49+
service_name => 'foo_3',
50+
image => 'foo:bar',
51+
publish => ['80:8080', '9000:9000' ],
52+
}
53+
"
54+
}
55+
it { should contain_exec('test_service_3 docker service create').with_command(/--publish 80:8080/) }
56+
it { should contain_exec('test_service_3 docker service create').with_command(/--publish 9000:9000/) }
57+
end
4458
end
4559

4660
context 'with ensure => present and service update' do

0 commit comments

Comments
 (0)