From d8c56be5105113d99a2c84e4ac944c22bc175f00 Mon Sep 17 00:00:00 2001 From: Luca Invernizzi Date: Tue, 24 Nov 2015 16:01:21 -0800 Subject: [PATCH 1/2] Restart docker container if newer image build is available. --- plugins/provisioners/docker/client.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/provisioners/docker/client.rb b/plugins/provisioners/docker/client.rb index cb1e577ea..ec42543a0 100644 --- a/plugins/provisioners/docker/client.rb +++ b/plugins/provisioners/docker/client.rb @@ -59,7 +59,8 @@ module VagrantPlugins id = "$(cat #{config[:cidfile]})" if container_exists?(id) - if container_args_changed?(config) + + if container_args_changed?(config) or container_img_changed?(config) @machine.ui.info(I18n.t("vagrant.docker_restarting_container", name: config[:name], )) @@ -94,6 +95,19 @@ module VagrantPlugins lookup_container(id) end + def container_img_changed?(config) + # Returns true if there is a container running with the given :name, + # and the container is not using the latest :image. + + # Here, "docker inspect " returns the id of the image + # that the container is using. We check that the latest image that + # has been built with that name (:image) matches the one that the + # container is running. + cmd = ("docker inspect --format='{{.Image}}' #{config[:name]} |" + + " grep $(docker images -q #{config[:image]})") + return !@machine.communicate.test(cmd) + end + def container_args_changed?(config) path = container_data_path(config) return true if !path.exist? From aaef5da41d9e26d1a34120f347b037abf10550a4 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 29 May 2016 14:37:37 -0400 Subject: [PATCH 2/2] Use a different info message when image changed --- plugins/provisioners/docker/client.rb | 13 +++++++++---- templates/locales/en.yml | 6 ++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/provisioners/docker/client.rb b/plugins/provisioners/docker/client.rb index ec42543a0..84129a5d4 100644 --- a/plugins/provisioners/docker/client.rb +++ b/plugins/provisioners/docker/client.rb @@ -59,9 +59,14 @@ module VagrantPlugins id = "$(cat #{config[:cidfile]})" if container_exists?(id) - - if container_args_changed?(config) or container_img_changed?(config) - @machine.ui.info(I18n.t("vagrant.docker_restarting_container", + if container_args_changed?(config) + @machine.ui.info(I18n.t("vagrant.docker_restarting_container_args", + name: config[:name], + )) + stop_container(id) + create_container(config) + elsif container_image_changed?(config) + @machine.ui.info(I18n.t("vagrant.docker_restarting_container_image", name: config[:name], )) stop_container(id) @@ -95,7 +100,7 @@ module VagrantPlugins lookup_container(id) end - def container_img_changed?(config) + def container_image_changed?(config) # Returns true if there is a container running with the given :name, # and the container is not using the latest :image. diff --git a/templates/locales/en.yml b/templates/locales/en.yml index eca3759e4..005c88fc5 100755 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -135,8 +135,10 @@ en: Building Docker images... docker_running: |- -- Container: %{name} - docker_restarting_container: |- - -- Detected changes to container '%{name}', restarting... + docker_restarting_container_args: |- + -- Detected changes to container '%{name}' args, restarting... + docker_restarting_container_image: |- + -- Detected newer image for container '%{name}', restarting... docker_starting_containers: |- Starting Docker containers... inserted_key: |-