Merge pull request #2579 from mitchellh/2579-fix-docker-container-running-check

provisioners/docker: Check for whether the container is running is not working properly
This commit is contained in:
Mitchell Hashimoto 2013-12-04 18:46:37 -08:00
commit 1b847b7829
1 changed files with 16 additions and 4 deletions

View File

@ -44,15 +44,15 @@ module VagrantPlugins
id = "$(cat #{config[:cidfile]})" id = "$(cat #{config[:cidfile]})"
if container_exist?(id) if container_exists?(id)
start_container(id) start_container(id)
else else
create_container(config) create_container(config)
end end
end end
def container_exist?(id) def container_exists?(id)
@machine.communicate.test("sudo docker ps -a -q | grep -q #{id}") lookup_container(id, true)
end end
def start_container(id) def start_container(id)
@ -62,7 +62,7 @@ module VagrantPlugins
end end
def container_running?(id) def container_running?(id)
@machine.communicate.test("sudo docker ps -q | grep #{id}") lookup_container(id)
end end
def create_container(config) def create_container(config)
@ -73,6 +73,18 @@ module VagrantPlugins
docker run #{args} #{config[:image]} #{config[:cmd]} docker run #{args} #{config[:image]} #{config[:cmd]}
] ]
end end
def lookup_container(id, list_all = false)
docker_ps = "sudo docker ps -q"
docker_ps << " -a" if list_all
@machine.communicate.tap do |comm|
# Docker < 0.7.0 stores container IDs using its short version while
# recent versions use the full container ID
# See https://github.com/dotcloud/docker/pull/2140 for more information
return comm.test("#{docker_ps} | grep -wFq #{id}") ||
comm.test("#{docker_ps} -notrunc | grep -wFq #{id}")
end
end
end end
end end
end end