How to Version Your Docker Images

How to Version Your Docker ImagesThere are a thousand ways to version Docker images — here is a simple oneTravis ReederBlockedUnblockFollowFollowingOct 3, 2016While there are probably a thousand ways to version your Docker images, I am going to show you a very simple way, using methods that have become quite common.

It will ensure your image’s versions match your Git version tags, so you know exactly which code is inside the image.

This tutorial assumes you already have a Dockerfile that builds your image.

Version FileFirst, create a VERSION file with nothing in it but the version numbers:0.

0.

1You want to store this VERSION file in the image for completeness (and because your program can use it to display the version), so add the following to your Dockerfile:ADD VERSION .

That will add the VERSION file to your WORKDIR.

Build scriptIf you don’t already have a build script, create one and call it build.

sh.

The simplest form of this is just to run your docker build:set -ex# SET THE FOLLOWING VARIABLES# docker hub usernameUSERNAME=treeder# image nameIMAGE=helloworlddocker build -t $USERNAME/$IMAGE:latest .

You may require some extra build steps there, so go ahead and add them.

Or you may have your own build script, in which case you can ignore this script and just change the line in the script below to run your build.

Be sure to chmod a+x .

/build.

sh so you can run it.

Release scriptNow, let’s create a simple release script called release.

sh that will bump the version, run the build script, set git tags, push tags to GitHub, build the image, and finally push the image to Docker Hub.

You can copy and paste the script below — just make sure to change the values at the top to match your Docker Hub information:set -ex# SET THE FOLLOWING VARIABLES# docker hub usernameUSERNAME=treeder# image nameIMAGE=helloworld# ensure we're up to dategit pull# bump versiondocker run –rm -v "$PWD":/app treeder/bump patchversion=`cat VERSION`echo "version: $version"# run build.

/build.

sh# tag itgit add -Agit commit -m "version $version"git tag -a "$version" -m "version $version"git pushgit push –tagsdocker tag $USERNAME/$IMAGE:latest $USERNAME/$IMAGE:$version# push itdocker push $USERNAME/$IMAGE:latestdocker push $USERNAME/$IMAGE:$versionMake it executable with chmod a+x .

/release.

sh then run it with .

/release.

sh .

There you have it.

Every time you do a release, simply run .

/release.

sh and you will have nicely versioned images that match up with your source code!.. More details

Leave a Reply