From 01a5ba99d0648bbb54193d36878141fb6ab363c5 Mon Sep 17 00:00:00 2001 From: Connor Martin Date: Wed, 10 Jul 2019 13:34:20 -0500 Subject: [PATCH] Docker remove container before remove image (#928) * add docker container removal * remove container before deleting image * update readme * clean up and add assert not test * test not docker command * use shell.and_ correctly --- README.md | 1 + ...st_docker_image_being_used_by_container.py | 27 +++++++++++++++++++ .../docker_image_being_used_by_container.py | 20 ++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/rules/test_docker_image_being_used_by_container.py create mode 100644 thefuck/rules/docker_image_being_used_by_container.py diff --git a/README.md b/README.md index d9424df..8383007 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ following rules are enabled by default: * `django_south_merge` – adds `--merge` to inconsistent django south migration; * `docker_login` – executes a `docker login` and repeats the previous command; * `docker_not_command` – fixes wrong docker commands like `docker tags`; +* `docker_image_being_used_by_container` ‐ removes the container that is using the image before removing the image; * `dry` – fixes repetitions like `git git push`; * `fab_command_not_found` – fix misspelled fabric commands; * `fix_alt_space` – replaces Alt+Space with Space character; diff --git a/tests/rules/test_docker_image_being_used_by_container.py b/tests/rules/test_docker_image_being_used_by_container.py new file mode 100644 index 0000000..00d9e67 --- /dev/null +++ b/tests/rules/test_docker_image_being_used_by_container.py @@ -0,0 +1,27 @@ +from thefuck.rules.docker_image_being_used_by_container import match, get_new_command +from thefuck.types import Command + + +def test_match(): + err_response = """Error response from daemon: conflict: unable to delete cd809b04b6ff (cannot be forced) - image is being used by running container e5e2591040d1""" + assert match(Command('docker image rm -f cd809b04b6ff', err_response)) + + +def test_not_match(): + err_response = 'bash: docker: command not found' + assert not match(Command('docker image rm -f cd809b04b6ff', err_response)) + + +def test_not_docker_command(): + err_response = """Error response from daemon: conflict: unable to delete cd809b04b6ff (cannot be forced) - image is being used by running container e5e2591040d1""" + assert not match(Command('git image rm -f cd809b04b6ff', err_response)) + + +def test_get_new_command(): + err_response = """ + Error response from daemon: conflict: unable to delete cd809b04b6ff (cannot be forced) - image + is being used by running container e5e2591040d1 + """ + result = get_new_command(Command('docker image rm -f cd809b04b6ff', err_response)) + expected = 'docker container rm -f e5e2591040d1 && docker image rm -f cd809b04b6ff' + assert result == expected diff --git a/thefuck/rules/docker_image_being_used_by_container.py b/thefuck/rules/docker_image_being_used_by_container.py new file mode 100644 index 0000000..d68f919 --- /dev/null +++ b/thefuck/rules/docker_image_being_used_by_container.py @@ -0,0 +1,20 @@ +from thefuck.utils import for_app +from thefuck.shells import shell + + +@for_app('docker') +def match(command): + ''' + Matches a command's output with docker's output + warning you that you need to remove a container before removing an image. + ''' + return 'image is being used by running container' in command.output + + +def get_new_command(command): + ''' + Prepends docker container rm -f {container ID} to + the previous docker image rm {image ID} command + ''' + container_id = command.output.strip().split(' ') + return shell.and_('docker container rm -f {}', '{}').format(container_id[-1], command.script)