Featured image of post Exporting all layers of docker images with one command

Exporting all layers of docker images with one command

Creation of Docker images from a file/folder

It seemed to be an easy task to just use Docker Images as file archives, since you could simply create an image with a command like

sudo tar cv buildcache_persist |docker import - "${CICACHETAG}" && docker push "${CICACHETAG}"

( in our examples we assume that we want to save/restore the folder buildcache_persist )

Extraction ( and the trouble )

But the extraction was a bit more complicated, dealing with tar archives should be not too complicated , but it did’t instantly work on all machines..

( Do not use below code , it worked only on 1 out of 3 systems)

    echo "REMOVING AND GETTING ${CICACHETAG} AGAIN ( MERGE )"
docker rmi ${CICACHETAG}
docker pull ${CICACHETAG} &&             (
cd /tmp/;docker save ${CICACHETAG} > /tmp/.importCI ;
tar xvf /tmp/.importCI --to-stdout   $(tar tf /tmp/.importCI|grep layer.tar) |tar xv
rm /tmp/.importCI
)
echo "SAVING ${CICACHETAG}"

In fact Murphys Law kicked in “again”, since everything went well in “real machines” but CI-Runners failed with “famous” error:

tar: invalid tar header checksum

The internetz , or better Stack Overflow a.k.a. Crap Overload had some half-knowledge + false-positive answer again

Once more the Stackoverflow thread was merely helpful ,

since the answer closest to the simplest solution is lowest-voted, AND STILL FULL OF FAILS ( that could be easily checked on a command line instead of sh*tloading half-knowledge)

But just for the lulz ( and not being downvoted on swag overload ) ,

here’s the wipe-*ss part for that answer line-by-line:

StackOverflow answer of Konstantin P. Truth
id=$(docker create image:tag) does not work without command set for e.g. FROM scratch containers
docker export $id -o image.tar docker export accepts only ONE argument (…)
docker rm $id better quote "$id" or curly-bracket ${id}
UNMENTIONED The suggested solution extracts EVERYTHING including /etc/ /dev/ and so on ..

The one-line Solution : extraction of specific files of a docker container without single layer extraction

To narrow it down :

  • only extraction of specific files is possible ( here only buildcache_persist)
  • there is no need to bloat up the filesystem with temporary tar files

Here we go :

 docker export $(docker create --name cicache docker.io/my-org/my-cicache-image:example-tag /bin/false ) |tar xv buildcache_persist ;docker rm cicache

This command wil extract only the folder buildcache_persist into the current directory


Happy Coding…

      ...