dockertagsdocker-compose

How to use multiple image tags with docker-compose


According to this and this GitHub issues, currently there is no native way how to supply multiple tags for a service's image when using docker-compose to build one or multiple images.

My use case for this would be to build images defined in a docker-compose.yml file and tag them once with some customized tag (e.g. some build no. or date or similar) and once as latest.

While this can be easily achieved with plain docker using docker tag, docker-compose only allows to set one single tag in the image key. Using docker tag together with docker-compose is not an option for me since I want to keep all my docker-related definitions in the docker-compose.yml file and not copy them over into my build script.

What would be a decent work-around to achieve setting of multiple tags with docker-compose and without having to hardcode/copy the image names first?


Solution

  • You can also take the following approach:

    # build is your actual build spec
    build:
      image: myrepo/myimage
      build:
      ...
      ...
    # these extend from build and just add new tags statically or from environment variables or 
    version_tag:
      extends: build
      image: myrepo/myimage:v1.0
    some_other_tag:
      extends: build
      image: myrepo/myimage:${SOME_OTHER_TAG}
    

    You can then just run docker-compose build and docker-compose push and you will build and push the correct set of tagged imaged