module Docktor class DockerClient CONTAINER_NAME_PREFIX = "docktor_" def container_exists?(container) `docker ps --all --quiet --filter name=#{container_name(container.name)}`.present? end def run(container) `docker run --name #{container_name(container.name)} #{parse_options(container.options)} #{container.image} #{container.command}` end def start(container) `docker start #{container_name(container.name)}` end private def parse_options(options) options.map do |name, value| case name when :detach then "--detach=#{value}" when :volumes then "--volume #{value.join(" ")}" when :ports then "--publish #{value.join(" ")}" when :links value.map { |v| "--link #{container_name(v)}" }.join(" ") when :environment "--env #{value.map { |k, v| %(#{k}="#{v}") }.join(" ")}" end end.join(" ").strip end def container_name(name) CONTAINER_NAME_PREFIX + name end end end