diff --git a/plugins/provisioners/docker/client.rb b/plugins/provisioners/docker/client.rb index cb1e577ea..84129a5d4 100644 --- a/plugins/provisioners/docker/client.rb +++ b/plugins/provisioners/docker/client.rb @@ -60,7 +60,13 @@ module VagrantPlugins if container_exists?(id) if container_args_changed?(config) - @machine.ui.info(I18n.t("vagrant.docker_restarting_container", + @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) @@ -94,6 +100,19 @@ module VagrantPlugins lookup_container(id) end + 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. + + # 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? 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: |-