Skip to content

Commit 5c97e9a

Browse files
Merge pull request #13146 from allisonlarson/fix-docker-port-collision-error
Ignore inactive docker containers when assigning ports
2 parents 773f942 + 10e45f1 commit 5c97e9a

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

plugins/providers/docker/driver.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ def read_used_ports
140140
all_containers.each do |c|
141141
container_info = inspect_container(c)
142142

143+
active = container_info["State"]["Running"]
144+
next unless active # Ignore used ports on inactive containers
145+
143146
if container_info["HostConfig"]["PortBindings"]
144147
port_bindings = container_info["HostConfig"]["PortBindings"]
145148
next if port_bindings.empty? # Nothing defined, but not nil either

test/unit/plugins/providers/docker/driver_test.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,33 @@
346346

347347
describe '#read_used_ports' do
348348
let(:all_containers) { ["container1\ncontainer2"] }
349-
let(:container_info) { {"Name"=>"/container", "HostConfig"=>{"PortBindings"=>{}}} }
349+
let(:container_info) { {"Name"=>"/container", "State"=>{"Running"=>true}, "HostConfig"=>{"PortBindings"=>{}}} }
350350
let(:empty_used_ports) { {} }
351351

352352
context "with existing port forwards" do
353-
let(:container_info) { {"Name"=>"/container", "HostConfig"=>{"PortBindings"=>{"22/tcp"=>[{"HostIp"=>"127.0.0.1","HostPort"=>"2222"}] }}} }
353+
let(:container_info) { {"Name"=>"/container","State"=>{"Running"=>true}, "HostConfig"=>{"PortBindings"=>{"22/tcp"=>[{"HostIp"=>"127.0.0.1","HostPort"=>"2222"}] }}} }
354354
let(:used_ports_set) { {"2222"=>Set["127.0.0.1"]} }
355355

356-
it 'should read all port bindings and return a hash of sets' do
357-
allow(subject).to receive(:all_containers).and_return(all_containers)
358-
allow(subject).to receive(:inspect_container).and_return(container_info)
356+
context "with active containers" do
357+
it 'should read all port bindings and return a hash of sets' do
358+
allow(subject).to receive(:all_containers).and_return(all_containers)
359+
allow(subject).to receive(:inspect_container).and_return(container_info)
359360

360-
used_ports = subject.read_used_ports
361-
expect(used_ports).to eq(used_ports_set)
361+
used_ports = subject.read_used_ports
362+
expect(used_ports).to eq(used_ports_set)
363+
end
364+
end
365+
366+
context "with inactive containers" do
367+
let(:container_info) { {"Name"=>"/container", "State"=>{"Running"=>false}, "HostConfig"=>{"PortBindings"=>{"22/tcp"=>[{"HostIp"=>"127.0.0.1","HostPort"=>"2222"}] }}} }
368+
369+
it 'returns empty' do
370+
allow(subject).to receive(:all_containers).and_return(all_containers)
371+
allow(subject).to receive(:inspect_container).and_return(container_info)
372+
373+
used_ports = subject.read_used_ports
374+
expect(used_ports).to eq(empty_used_ports)
375+
end
362376
end
363377
end
364378

0 commit comments

Comments
 (0)