DEV Community

Tom
Tom

Posted on

"docker artifact" CLI Plugin

docker lego

Hey everyone, my name is Tom Shaw and I've been a Docker user since 2013, Docker Community Leader and Docker Captain since 2015. This is my first time posting to dev.to and I wanted to share a side project that I've been working on.

What does "docker artifact" do ?

The "docker artifact" CLI plugin is a simple little plugin which allows you to pull individual artifacts (files) from a Docker Image in Docker Hub without pulling the whole image. Feel free to use the code as it is or fork it. Pull requests welcome too.

Usage : docker artifact [command]

Command :

    ls    - List all files available to get 
    get   - Get a file (or multiple files) from an Image 
    label - Add a LABEL to file (or multiple files) inside an Image 

Examples : 

    docker artifact ls tomwillfixit/healthcheck:latest 
    docker artifact get helloworld.bin tom.jpg tomwillfixit/healthcheck:latest 
    docker artifact label helloworld.bin tom.jpg tomwillfixit/healthcheck:latest

How does this work ?

We use Docker Labels to point at the "sha256" value of the artifact or multiple artifacts that we want to make available for download. Then we use curl to download the single "blob" (tar bundle) from Docker Hub which contains the artifact we need.

Why would you do this ?

When building a Docker Image it's quite common to copy a file or directory from another Image. For example :

COPY --from=tomwillfixit/test:latest /tmp/shipitcon.jpg /tmp

This may be repeated a number of times within the Dockerfile, particularly for those using Multi Stage Builds.

This is a fine approach to take but it is quite wasteful. The "COPY --from" directive downloads the whole image in order to access the file or directory you specify. Using the "docker artifact" CLI we only download the artifact we need.

In a comparison between "COPY --from" and "docker artifact" we were able to reduce build time by 40% when copying a single file from a 60mb image on a freshly installed system (no build cache).

For more details on how to use the plugin and how to add a CLI plugin to Docker then checkout the GitHub repository here.

Disclaimer : This is just a side project so the code may be a little rough. Pull requests are welcome. There is also a Medium post with more details available here.

If you have zero interest in this plugin you can still use this approach to extend Docker by creating your own CLI plugins.

Thanks for reading.

Twitter : @tomwillfixit

Top comments (0)