When and why tag Docker images to :latest ?

When I work on cyber-dojo I do quite a lot of testing. To state the obvious, when testing you want to test the software as it actually runs, as it's actually released, as it's actually deployed, etc. As obvious as this is, it can be surprising how often a distortion occurs.

cyber-dojo deploys each microservice in its own docker container. It would be faster to run tests locally, not inside a docker container, but that would be a distortion. So my development loop looks like this:

  1. edit the code and tests
  2. build a docker image
  3. run a docker container from the image
  4. wait till the container is ready
  5. run tests against the container

Note step 2 does not volume mount the code and tests. That would be quicker but would be a distortion.

Now, step 2 in a workflow such as this commonly tags the docker image to :latest and, if an image is actually deployed, retags it to something unique (eg the first 7 characters of the git commit sha) and publishes that image. This is another small distortion. The published image now has a different tag to the one used in step 5's testing.

A development loop such as this can quickly build up a lot of stale image layers. When you clean up all those stale images you'll bust your image layer cache.

Here's my actual development loop:

  1. edit the code and tests
  2. delete all images except :latest (a continuous approach to deleting stale image layers)
  3. build a docker image tagged directly from the git commit sha
  4. retag this image to :latest (to prevent step 2 from busting the cache next time around)
  5. run a docker container from step 3)
  6. wait till the container is ready
  7. run tests against this container
  8. if deploying, deploy the image from step 3)

I also need to publish the :latest image. People running their own cyber-dojo server need a way to upgrade. And an upgrade needs a consistent set of image tags. So step 3) builds an image not only tagged to the git commit sha, but which also includes the full 40 character git commit sha as an environment variable (SHA) inside the image. This allows:

IMAGE=cyberdojo/runner SHA=$(docker run --rm ${IMAGE}:latest bash -c 'echo ${SHA}') TAG=${SHA:0:7}

and the tagged image is ${IMAGE}:${TAG}

No comments:

Post a comment