A very common need is to quickly identify what version of the software a Docker image is running and optionally stick to it, or to be sure to always run a stable version. This is a perfect use for the Docker tags. We'll build a Terraform container, with both a stable and an unstable tag, so multiple versions can coexist—one for production and one for testing.
Here's a simple Dockerfile to create a Terraform container (Terraform was covered earlier in this book):
FROM alpine:latest ENV TERRAFORM_VERSION=0.7.12 VOLUME ["/data"] WORKDIR /data RUN apk --update --no-cache add ca-certificates openssl && wget -O terraform.zip "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && unzip terraform.zip -d /bin && rm -rf terraform.zip /var/cache/apk/* ENTRYPOINT ["/bin/terraform"] CMD [ "--help" ]
This is the current, stable, and latest version, and it's 0.7.12 as well. We'd like our users to be able to request one of the following:
terraform:latest
(for those of our users who always want the latest version available)terraform:stable
(for those of our users who always want the stable version, opposed to a beta version)terraform:0.7.12
(for those of our users who always want a very specific version, such as for compatibility issues)This is easily achievable by building directly with all these different tags:
$ docker build -t terraform:latest -t terraform:stable -t terraform:0.7.12 .
Now when requesting which images are available, we can see they all have the same image ID, but with different tags. This is what we wanted, since it's the same image that shares all those tags:
$ docker images terraform REPOSITORY TAG IMAGE ID CREATED SIZE terraform 0.7.12 9d53a0811d63 About a minute ago 83.61 MB terraform latest 9d53a0811d63 About a minute ago 83.61 MB terraform stable 9d53a0811d63 About a minute ago 83.61 MB
Some days later, we release a new version of the software as a Docker container for our team to test it out. This time it's an unstable, 0.8.0-rc1 version. We'd like our users to request this image as one of the following:
terraform:latest
(it's still the latest version available, even unstable)terraform:unstable
(it's a release candidate, not a stable version)terraform:0.8.0-rc1
(it's this specific version)Change the TERRAFORM_VERSION
variable in the Dockerfile
, and build the image with the following tags:
$ docker build -t terraform:latest -t terraform:unstable -t terraform:0.8.0-rc1 .
Now, if we look at the available Terraform images, we can confirm that it's the same image ID shared by the latest
, unstable
, and 0.8.0-rc1
tags, while our users preferring the stable version are not impacted by our changes:
$ docker images terraform REPOSITORY TAG IMAGE ID CREATED SIZE terraform 0.8.0-rc1 44609fa7c016 18 seconds ago 86.77 MB terraform latest 44609fa7c016 18 seconds ago 86.77 MB terraform unstable 44609fa7c016 18 seconds ago 86.77 MB terraform 0.7.12 9d53a0811d63 9 minutes ago 83.61 MB terraform stable 9d53a0811d63 9 minutes ago 83.61 MB
13.58.51.228