providers/docker: remove built image on reload and destroy

This commit is contained in:
Mitchell Hashimoto 2014-04-18 16:29:56 -07:00
parent 81df70eee0
commit 16ae728b5f
4 changed files with 43 additions and 0 deletions

View File

@ -107,6 +107,7 @@ module VagrantPlugins
b2.use ConfigValidate b2.use ConfigValidate
b2.use action_halt b2.use action_halt
b2.use DestroyBuildImage
b2.use EnvSet, build_rebuild: true b2.use EnvSet, build_rebuild: true
b2.use action_start b2.use action_start
end end
@ -135,6 +136,7 @@ module VagrantPlugins
b3.use EnvSet, :force_halt => true b3.use EnvSet, :force_halt => true
b3.use action_halt b3.use action_halt
b3.use HostMachineSyncFoldersDisable b3.use HostMachineSyncFoldersDisable
b3.use DestroyBuildImage
b3.use Destroy b3.use Destroy
b3.use ProvisionerCleanup b3.use ProvisionerCleanup
else else
@ -250,6 +252,7 @@ module VagrantPlugins
autoload :CompareSyncedFolders, action_root.join("compare_synced_folders") autoload :CompareSyncedFolders, action_root.join("compare_synced_folders")
autoload :Create, action_root.join("create") autoload :Create, action_root.join("create")
autoload :Destroy, action_root.join("destroy") autoload :Destroy, action_root.join("destroy")
autoload :DestroyBuildImage, action_root.join("destroy_build_image")
autoload :HasSSH, action_root.join("has_ssh") autoload :HasSSH, action_root.join("has_ssh")
autoload :HostMachine, action_root.join("host_machine") autoload :HostMachine, action_root.join("host_machine")
autoload :HostMachineBuildDir, action_root.join("host_machine_build_dir") autoload :HostMachineBuildDir, action_root.join("host_machine_build_dir")

View File

@ -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

View File

@ -94,6 +94,12 @@ module VagrantPlugins
end end
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) def inspect_container(cid)
# DISCUSS: Is there a chance that this json will change after the container # DISCUSS: Is there a chance that this json will change after the container
# has been brought up? # has been brought up?

View File

@ -2,6 +2,8 @@ en:
docker_provider: docker_provider:
already_built: |- already_built: |-
Image is already built from the Dockerfile. `vagrant reload` to rebuild. Image is already built from the Dockerfile. `vagrant reload` to rebuild.
build_image_destroy: |-
Removing built image...
building: |- building: |-
Building the container from a Dockerfile... Building the container from a Dockerfile...
creating: |- creating: |-