Skip to content

Instantly share code, notes, and snippets.

@drnic
Last active August 29, 2015 14:22
Show Gist options
  • Save drnic/78b92daeab0732ffb72b to your computer and use it in GitHub Desktop.
Save drnic/78b92daeab0732ffb72b to your computer and use it in GitHub Desktop.
Script to recreate docker containers to fix the host port to the current dynamically assigned port

Get the current running containers:

# docker --host unix:///var/vcap/sys/run/docker/docker.sock ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                  PORTS                                                                      NAMES
1795b5c8aa16        lnguyen/logstash:1.4   "/scripts/run.sh"   4 days ago          Up Less than a second   0.0.0.0:32768->514/tcp, 0.0.0.0:32769->9200/tcp, 0.0.0.0:32770->9300/tcp   cf-f9404974-4ff9-4658-96d1-4b165d8bf731   
7bd1c38e6c49        lnguyen/logstash:1.4   "/scripts/run.sh"   6 days ago          Up Less than a second   0.0.0.0:32771->514/tcp, 0.0.0.0:32772->9200/tcp, 0.0.0.0:32773->9300/tcp   cf-f99c81b2-efa7-4b90-a9d5-efa882e19875   

Install and run the fix script:

mkdir workspace
cd workspace
wget https://gist.github.com/drnic/78b92daeab0732ffb72b/download
mv download{,.tgz}
tar xfz download.tgz
cd gist78b92*
bundle
chmod +x fix_docker_host_ports.rb
DOCKER_URL=unix:///var/vcap/sys/run/docker/docker.sock bundle exec ./fix_docker_host_ports.rb
#!/usr/bin/env ruby
# DOCKER_URL=unix:///var/vcap/sys/run/docker/docker.sock bundle exec ./fix_docker_host_ports.rb
# curl http://containers:[email protected]/v2/service_instances/$RANDOM -d '{
# "service_id": "2fd814ac-d1f7-4d4a-a4f7-d386cd8fd8e3",
# "plan_id": "1a0efffc-eb45-4bf8-8ee3-a3c1a9a53151",
# "organization_guid": "org-guid-here",
# "space_guid": "space-guid-here"
# }' -X PUT -H "X-Broker-API-Version: 2.4" -H "Content-Type: application/json"
# docker --host unix:///var/vcap/sys/run/docker/docker.sock ps -a
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 767a17b0b3c1 frodenas/postgresql:latest "/scripts/run.sh " 2 seconds ago Up 1 seconds 0.0.0.0:32772->5432/tcp cf-foobar
# 38bd802ee1c9 frodenas/postgresql:latest "/scripts/run.sh " 18 seconds ago Up 17 seconds 0.0.0.0:32769->5432/tcp happy_bell
# a4c72c1c504e frodenas/postgresql:latest "/scripts/run.sh " 19 seconds ago Up 18 seconds 0.0.0.0:32770->5432/tcp dreamy_sinoussi
# ef75144ade79 frodenas/postgresql:latest "/scripts/run.sh " 19 seconds ago Up 18 seconds 0.0.0.0:32771->5432/tcp backstabbing_engelbart
# DOCKER_URL=unix:///var/vcap/sys/run/docker/docker.sock bundle exec ./fix_docker_host_ports.rb
# Removing container /cf-foobar
# Recreating container /cf-foobar
# Restarting container /cf-foobar with ports {"5432/tcp"=>[{"HostIp"=>"0.0.0.0", "HostPort"=>"32772"}]}
# Removing container /happy_bell
# Recreating container /happy_bell
# Restarting container /happy_bell with ports {"5432/tcp"=>[{"HostIp"=>"0.0.0.0", "HostPort"=>"32769"}]}
# Removing container /dreamy_sinoussi
# Recreating container /dreamy_sinoussi
# Restarting container /dreamy_sinoussi with ports {"5432/tcp"=>[{"HostIp"=>"0.0.0.0", "HostPort"=>"32770"}]}
# Removing container /backstabbing_engelbart
# Recreating container /backstabbing_engelbart
# Restarting container /backstabbing_engelbart with ports {"5432/tcp"=>[{"HostIp"=>"0.0.0.0", "HostPort"=>"32771"}]}
# docker --host unix:///var/vcap/sys/run/docker/docker.sock ps -a
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# a9e32efb1d29 frodenas/postgresql:latest "/scripts/run.sh " 4 seconds ago Up 3 seconds 0.0.0.0:32771->5432/tcp backstabbing_engelbart
# e86a680422c4 frodenas/postgresql:latest "/scripts/run.sh " 4 seconds ago Up 3 seconds 0.0.0.0:32770->5432/tcp dreamy_sinoussi
# 13a9b2989702 frodenas/postgresql:latest "/scripts/run.sh " 5 seconds ago Up 4 seconds 0.0.0.0:32769->5432/tcp happy_bell
# 017f9ce7362d frodenas/postgresql:latest "/scripts/run.sh " 5 seconds ago Up 2 seconds 0.0.0.0:32772->5432/tcp cf-foobar
require 'docker'
Docker::Container.all.each do |container|
config = container.json['Config']
host_config = container.json['HostConfig']
name = container.json['Name']
ports = container.json['NetworkSettings']['Ports']
volumes = container.json['Volumes']
new_container_options = config.merge({
'name' => name,
})
start_container_options = host_config.merge({
'PortBindings' => ports,
})
puts "Removing container #{name}"
container.remove(force: true)
puts "Recreating container #{name}"
new_container = Docker::Container.create(new_container_options)
puts "Restarting container #{name} with ports #{ports.inspect}"
new_container.start(start_container_options)
end
source "https://rubygems.org"
gem "docker-api"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment