dockerdocker-composedockerfile

Should I Set docker image version in docker-compose?


Imagine I have a docker-compose.yml for adding mongo as a container, is it a good thing to set version in front of image name or let it to be the latest by default?

version: '3.8'
services:
  mongo:
    image: mongo:4.0
    ports:
      - "27017:27017"

Actually what are the pros and cons for application in development and production releases?

image: mongo:4.0 VS image: mongo


Solution

  • Including a version number as you've done is good practice. I'd generally use a major-only image tag (mongo:4) or a major+minor tag (mongo:4.4) but not a super-specific version (mongo:4.4.10) unless you have automation to update it routinely.

    Generally the Docker Hub images get rebuilt fairly routinely; but, within a given patch line, only the most-recent versions get patches. Say the debian:focal base image gets a security update. As of this writing, the mongo image has 4, 4.4, and 4.4.10 tags, so all of those get rebuilt, but e.g. 4.4.9 won't. So using a too-specific version could mean you don't get important updates.

    Conversely, using latest means you just don't care what version you have. Your question mentions mongo:4.0 but mongo:latest is currently version 5.0.5; are there compatibility issues with that major-version upgrade?

    The key rules here are:

    1. If you already have some image:tag locally, launching a container will not pull it again, even if it's updated in the repository.
    2. Minor-version tags like mongo:4.4 will continue to get updates as long as they are supported, but you may need to docker-compose pull to get updates.
    3. Patch-version tags like mongo:4.4.9 will stop getting updates as soon as there's a newer patch version, even if you docker pull mongo:4.4.9.
    4. Using a floating tag like ...:latest or a minor-version tag could mean different systems get different builds of the image, depending on what they have locally. (Your coworker could have a different mongo:latest than you; this is a bigger problem in cluster environments like Kubernetes.)