In modern DevOps practices, Docker images are primarily built either on a developer machine or a CI/CD pipeline. The images are stored in a container registry and then deployed to multiple staging environments and production machines. They might run Docker or a container orchestrator, such as Kubernetes, on top of them.

To efficiently use images, we must understand how to tag them.

Primarily, Docker pulls the image once when you do a Docker run. This means that once an image with a particular version is on the machine, Docker will not attempt to pull it on every run unless you explicitly pull it.

To pull the image explicitly, you can use the docker pull command:

$ docker pull nginx

Using default tag: latest

latest: Pulling from library/nginx

f03b40093957: Pull complete

eed12bbd6494: Pull complete

fa7eb8c8eee8: Pull complete

7ff3b2b12318: Pull complete

0f67c7de5f2c: Pull complete

831f51541d38: Pull complete

Digest: sha256:af296b18…

Status: Downloaded newer image for nginx:latest

docker.io/library/nginx:latest

Now, if we attempt to launch a container using this image, it will instantly launch the container without pulling the image:

$ docker run nginx

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform

configuration

/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

2023/06/10 08:09:07 [notice] 1#1: start worker processes

2023/06/10 08:09:07 [notice] 1#1: start worker process 29

2023/06/10 08:09:07 [notice] 1#1: start worker process 30

So, using the latest tag on an image is a bad idea, and the best practice is to use semantic versions as your tag. There are two primary reasons for this:

  • If you build the latest image every time, orchestrators such as Docker Compose and Kubernetes will assume the image is already on your machine and will not pull your image by default. Using an image pull policy such as Always on Kubernetes or a script to pull the image is a waste of network bandwidth. It is also important to note that Docker Hub limits the number of pulls you can make on open source images, so you must limit your pulls to only when necessary.
  • Docker tags allow you to roll out or roll back your container deployment quickly. If you always use the latest tag, the new build overrides the old one, so there is no way you can roll back a faulty container to the last known good version. Using versioned images in production is also a good idea to ensure your container’s stability. If, for some reason, you lose the local image and decide to rerun your container, you may not get the same version of the software you were already running, as the latest tag changes frequently. So, it’s best to use a particular container version in production for stability.

Leave a Reply

Your email address will not be published. Required fields are marked *