module Docktor class DockerClient CONTAINER_NAME_PREFIX = "docktor_" def container_exists?(container) `docker ps -aqf name=#{container_name(container.name)}` != "" end def run(container) puts "docker run -d --name #{container_name(container.name)} #{parse_options(container.options)} #{container.image} #{container.command}" `docker run -d --name #{container_name(container.name)} #{parse_options(container.options)} #{container.image} #{container.command}` end def start(container) `docker start #{container_name(container.name)}` end def parse_options(options) options.map do |name, value| case name when :volumes then "-v #{value.join(" ")}" when :ports then "-p #{value.join(" ")}" when :links then "--link #{value.join(" ")}" value.map { |v| "--link #{container_name(v)}" }.join(" ") when :environment "-e #{value.map { |k, v| %(#{k}="#{v}") }.join(" ")}" end end.join(" ").strip end def container_name(name) CONTAINER_NAME_PREFIX + name end end end