diff --git a/plugins/providers/docker/action.rb b/plugins/providers/docker/action.rb index b8b61f48d..87a386c03 100644 --- a/plugins/providers/docker/action.rb +++ b/plugins/providers/docker/action.rb @@ -107,6 +107,7 @@ module VagrantPlugins b2.use ConfigValidate b2.use action_halt + b2.use DestroyBuildImage b2.use EnvSet, build_rebuild: true b2.use action_start end @@ -135,6 +136,7 @@ module VagrantPlugins b3.use EnvSet, :force_halt => true b3.use action_halt b3.use HostMachineSyncFoldersDisable + b3.use DestroyBuildImage b3.use Destroy b3.use ProvisionerCleanup else @@ -250,6 +252,7 @@ module VagrantPlugins autoload :CompareSyncedFolders, action_root.join("compare_synced_folders") autoload :Create, action_root.join("create") autoload :Destroy, action_root.join("destroy") + autoload :DestroyBuildImage, action_root.join("destroy_build_image") autoload :HasSSH, action_root.join("has_ssh") autoload :HostMachine, action_root.join("host_machine") autoload :HostMachineBuildDir, action_root.join("host_machine_build_dir") diff --git a/plugins/providers/docker/action/destroy_build_image.rb b/plugins/providers/docker/action/destroy_build_image.rb new file mode 100644 index 000000000..a9f739a9a --- /dev/null +++ b/plugins/providers/docker/action/destroy_build_image.rb @@ -0,0 +1,32 @@ +require "log4r" + +module VagrantPlugins + module DockerProvider + module Action + class DestroyBuildImage + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::docker::destroybuildimage") + end + + def call(env) + machine = env[:machine] + + # Try to read the image ID from the cache file if we've + # already built it. + image_file = machine.data_dir.join("docker_build_image") + image = nil + if image_file.file? + image = image_file.read.chomp + + machine.ui.output(I18n.t("docker_provider.build_image_destroy")) + machine.provider.driver.rmi(image) + image_file.delete + end + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/docker/driver.rb b/plugins/providers/docker/driver.rb index 8ef26d1dd..58c2c1f23 100644 --- a/plugins/providers/docker/driver.rb +++ b/plugins/providers/docker/driver.rb @@ -94,6 +94,12 @@ module VagrantPlugins end end + def rmi(id) + execute('docker', 'rmi', id) + rescue Exception => e + raise if !e.to_s.include?("No such image") + end + def inspect_container(cid) # DISCUSS: Is there a chance that this json will change after the container # has been brought up? diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index 966029001..ea565db98 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -2,6 +2,8 @@ en: docker_provider: already_built: |- Image is already built from the Dockerfile. `vagrant reload` to rebuild. + build_image_destroy: |- + Removing built image... building: |- Building the container from a Dockerfile... creating: |-