Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add force_remove option to r/image #104

Merged
merged 4 commits into from Dec 29, 2020
Merged

Conversation

suzuki-shunsuke
Copy link
Collaborator

@suzuki-shunsuke suzuki-shunsuke commented Dec 28, 2020

  • feat: add an option force_remove to docker_image
  • fix: remove a Docker image by not image id but image name

@suzuki-shunsuke
Copy link
Collaborator Author

suzuki-shunsuke commented Dec 28, 2020

Hmm...
The force option doesn't work as expected. 🤔
Maybe we have to update Docker client?

❯ terraform destroy
docker_image.alpine: Refreshing state... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # docker_image.alpine will be destroyed
  - resource "docker_image" "alpine" {
      - force_remove = true -> null
      - id           = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5" -> null
      - latest       = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72" -> null
      - name         = "alpine:3.11.5" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_image.alpine: Destroying... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

Error: Unable to remove Docker image: Error response from daemon: conflict: unable to delete a187dde48cd2 (cannot be forced) - image is being used by running container a80da3cb1dfb
resource "docker_image" "alpine" {
  name         = "alpine:3.11.5"
  force_remove = true
}

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "2.9.0"
    }
  }
}

provider "docker" {
}

On the other hand, docker rmi's force option works as expected.

@suzuki-shunsuke suzuki-shunsuke added enhancement New feature or request r/image Relates to the image resource labels Dec 28, 2020
@suzuki-shunsuke suzuki-shunsuke changed the title feat: add force_remove option to r/image [WIP] feat: add force_remove option to r/image Dec 28, 2020
@suzuki-shunsuke
Copy link
Collaborator Author

suzuki-shunsuke commented Dec 28, 2020

Oh, it's interesting.

❯ docker images alpine:3.11.5
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
alpine       3.11.5    a187dde48cd2   9 months ago   5.6MB

❯ docker rmi -f a187dde48cd2
Error response from daemon: conflict: unable to delete a187dde48cd2 (cannot be forced) - image is being used by running container a80da3cb1dfb

❯ docker rmi -f alpine:3.11.5
Untagged: alpine:3.11.5
Untagged: alpine@sha256:b276d875eeed9c7d3f1cfa7edb06b22ed22b14219a7d67c52c56612330348239

❯ docker images | grep alpine:3.11.5

❯ docker images | grep a187dde48cd2
<none>                                                                           <none>                                                             a187dde48cd2   9 months ago    5.6MB

When a Docker image is used by a container, it fails to remove the image
by image id even if "force" option is enabled.
By specifying image name, the image tag is removed successfully.

#104 (comment)
@suzuki-shunsuke suzuki-shunsuke changed the title [WIP] feat: add force_remove option to r/image feat: add force_remove option to r/image Dec 29, 2020
@suzuki-shunsuke
Copy link
Collaborator Author

Test

resource "docker_image" "test" {
  name = "alpine:3.11.5"
}
$ docker run --rm -d alpine:3.11.5 tail -f /dev/null
$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED              STATUS              PORTS                       NAMES
99aa86a838e2   alpine:3.11.5          "tail -f /dev/null"      About a minute ago   Up About a minute                               musing_hypatia

When force_remove: false, it failed to remove the Docker image

$ terraform destroy
docker_image.test: Refreshing state... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # docker_image.test will be destroyed
  - resource "docker_image" "test" {
      - id     = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5" -> null
      - latest = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72" -> null
      - name   = "alpine:3.11.5" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_image.test: Destroying... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

Error: Unable to remove Docker image: Error response from daemon: conflict: unable to remove repository reference "alpine:3.11.5" (must force) - container 99aa86a838e2 is using its referenced image a187dde48cd2

Then set force_remove: true.

resource "docker_image" "test" {
  name         = "alpine:3.11.5"
  force_remove = true
}
❯ terraform apply
docker_image.test: Refreshing state... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # docker_image.test will be updated in-place
  ~ resource "docker_image" "test" {
      + force_remove = true
        id           = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5"
        latest       = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72"
        name         = "alpine:3.11.5"
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

docker_image.test: Modifying... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]
docker_image.test: Modifications complete after 0s [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

When force_remove: true, it succeeds to destroy the resource.

$ terraform destroy
docker_image.test: Refreshing state... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # docker_image.test will be destroyed
  - resource "docker_image" "test" {
      - force_remove = true -> null
      - id           = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5" -> null
      - latest       = "sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72" -> null
      - name         = "alpine:3.11.5" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_image.test: Destroying... [id=sha256:a187dde48cd289ac374ad8539930628314bc581a481cdb41409c9289419ddb72alpine:3.11.5]
docker_image.test: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.

$ docker images alpine:3.11.5
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

Copy link
Contributor

@mavogel mavogel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mavogel mavogel merged commit 4b80bc3 into master Dec 29, 2020
@mavogel mavogel deleted the feat/add-force-remove branch December 29, 2020 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request r/image Relates to the image resource
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants