Skip to content

Commit

Permalink
Merge pull request #13146 from allisonlarson/fix-docker-port-collisio…
Browse files Browse the repository at this point in the history
…n-error

Ignore inactive docker containers when assigning ports
  • Loading branch information
allisonlarson authored May 10, 2023
2 parents 773f942 + 10e45f1 commit 5c97e9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions plugins/providers/docker/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def read_used_ports
all_containers.each do |c|
container_info = inspect_container(c)

active = container_info["State"]["Running"]
next unless active # Ignore used ports on inactive containers

if container_info["HostConfig"]["PortBindings"]
port_bindings = container_info["HostConfig"]["PortBindings"]
next if port_bindings.empty? # Nothing defined, but not nil either
Expand Down
28 changes: 21 additions & 7 deletions test/unit/plugins/providers/docker/driver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,33 @@

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

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

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

used_ports = subject.read_used_ports
expect(used_ports).to eq(used_ports_set)
used_ports = subject.read_used_ports
expect(used_ports).to eq(used_ports_set)
end
end

context "with inactive containers" do
let(:container_info) { {"Name"=>"/container", "State"=>{"Running"=>false}, "HostConfig"=>{"PortBindings"=>{"22/tcp"=>[{"HostIp"=>"127.0.0.1","HostPort"=>"2222"}] }}} }

it 'returns empty' do
allow(subject).to receive(:all_containers).and_return(all_containers)
allow(subject).to receive(:inspect_container).and_return(container_info)

used_ports = subject.read_used_ports
expect(used_ports).to eq(empty_used_ports)
end
end
end

Expand Down

0 comments on commit 5c97e9a

Please sign in to comment.