Packer: Post-Processor: Delete docker image in local build machine after pushing to remote registry

Created on 20 Sep 2017  路  8Comments  路  Source: hashicorp/packer

I do not want docker images piling up in my jenkins-slave. Is there any way to not commit images to local build machine and able to push docker image to remote registry?

I tried with "export" in builder and "docker-import" in post-processor but its still keeps a copy.

Thanks,
Karthik

enhancement post-processodocker

Most helpful comment

I like to have this feature implemented in packer too. I could delete images using shell-local as shown in a above comment. However, it was bit tricky as I didn't pay attention to where shell-local should be. Just want to point out shell-local should be outside the list of docker-tag and docker-push. Hope it helps for someone.

"post-processors": [
    [
      {
        "type": "docker-tag",
        "repository": "{{user `docker_registry`}}/{{user `name`}}",
        "tag": "{{user `version`}}"
      },
      {
        "type": "docker-push",
        "ecr_login": true,
        "login_server": "https://{{user `docker_registry`}}"
      }
    ],
    {
            "type": "shell-local",
            "inline": ["docker rmi {{user `docker_registry`}}/{{user `name`}}:{{user `version`}}"]
    },
    {
            "type": "shell-local",
            "inline": ["docker system prune -af"]
    }
  ]

All 8 comments

I was able to do that with "shell-local", here's my post-processors block:

 "post-processors": [
       [
        {
            "type": "docker-import",
            "repository": "hostname/bakery",
            "tag": "{{user `version`}}"
        },
        {
            "type": "docker-push",
            "login_server": "https://hostname",
            "login": "true",
            "login_username": "TOKEN",
            "login_password": "PASSWORD"
        }
       ],
        {
            "type": "shell-local",
            "inline": ["docker rmi $(docker images | grep -E {{user `version`}} | grep -E repo/bakery | awk '{print $3}')"]
        },
        {
            "type": "shell-local",
            "inline": ["rm -f jdk7-base-{{user `version`}}.tar"]
        }
    ]

I knew that shell-local is deprecated in 1.2.0 , alternative is the "manifest" how come this can execute commands in host machine? is the documentation is incomplete?

Thanks,
Karthik

We could be add the docker system prune as a new post-processor, I'll think a lit more about this.

either a docker-cleanup post-processor (would need to make sure the artifact gets passed on to it) or a flag in the docker-push pp makes sense to me

Reading the code I found that the most easy way to fix this is return the input artifact in the docker-push, because in that way the destroy will be applied by the docker-import artifact used be the docker-push post-processor.

I tested this and works but @mwhooker correct me if I'm wrong

I like to have this feature implemented in packer too. I could delete images using shell-local as shown in a above comment. However, it was bit tricky as I didn't pay attention to where shell-local should be. Just want to point out shell-local should be outside the list of docker-tag and docker-push. Hope it helps for someone.

"post-processors": [
    [
      {
        "type": "docker-tag",
        "repository": "{{user `docker_registry`}}/{{user `name`}}",
        "tag": "{{user `version`}}"
      },
      {
        "type": "docker-push",
        "ecr_login": true,
        "login_server": "https://{{user `docker_registry`}}"
      }
    ],
    {
            "type": "shell-local",
            "inline": ["docker rmi {{user `docker_registry`}}/{{user `name`}}:{{user `version`}}"]
    },
    {
            "type": "shell-local",
            "inline": ["docker system prune -af"]
    }
  ]

Closing this because we ended up not deprecating shell-local.

Hello,

@SwampDragons, can this issue be reopened ? Because shell-local is much like a workaround.

In my use case, I'm pushing three docker images to three different Docker repositories in one build. When I'm building only one Docker image with the -only option, shell-local will fail because it will not find all Docker images to cleanup.

I didn't find the way to tell to packer: delete only the docker image you just pushed.

You can always write a script that handles the error cases appropriately.

I'll reopen but because this has a workaround, I am not going to be working on it; if someone from the community wants to add a delete_after_push option to the post-processor, I'd be happy to review/merge a pull request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielBo picture DanielBo  路  3Comments

mwhooker picture mwhooker  路  3Comments

Tensho picture Tensho  路  3Comments

shashanksinha89 picture shashanksinha89  路  3Comments

paulcdejean picture paulcdejean  路  3Comments