mrutyunjay
mrutyunjay

Reputation: 8310

Check if local docker image latest

In my use case I always fetch the image tagged with "latest" tag. This "latest" tag gets updated regularly. So even if the latest tag image is updated on registry, the "docker run" command does not update it on local host. This is expected behavior as the "latest" image exists on local host. But I want to make sure that if the "latest" image on local host and registry are different then it should pull the latest image from registry. Is there any way to achieve this?

Upvotes: 0

Views: 1900

Answers (1)

David Maze
David Maze

Reputation: 159040

You can manually docker pull the image before you run it. This is fairly inexpensive, especially if the image hasn't changed. You can do it while the old container is still running to minimize downtime.

docker pull the-image
docker stop the-container
docker rm the-container
docker run -d ... --name the-container the-image

In an automated environment you might consider avoiding the latest tag and other similar fixed strings due to exactly this ambiguity. In Kubernetes, for example, the default behavior is to reuse a local image that has some name, which can result in different nodes running different latest images. If you label your images with a date stamp or source-control ID or something else such that every image has a unique tag, you can just use that tag.

Finding the tag value can be problematic outside the context of a continuous-deployment system; Docker doesn't have any built-in way to find the most recent tag for an image.

# docker pull the-image:20220704  # optional
docker stop the-container
docker rm the-container
docker run -d ... --name the-container the-image:20220704
docker rmi the-image:20220630

One notable advantage of this last approach is that it's very easy to go back to an earlier build if today's build happens to be broken; just switch the image tag back a build or two.

Upvotes: 1

Related Questions