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?
In Docker, we can also assign multiple tags to an image. Here, we'll use the docker build command to assign multiple tags to an image in a single command.
A tag must point to a single Docker image but a single Docker image can have many Tags. So let's rebuild the previous image with version:latest tag. In the above case, we built a new image (without modifying the contents) with the name version:latest though we left out :latest part so that Docker can do its magic.
Use the -a (or --all-tags ) option to push all tags of a local image. The following example creates multiple tags for an image, and pushes all those tags to Docker Hub.
You can pull a Docker Image using the pull sub-command. You can specify the tag of the Image that you want to pull. Note that if you don't specify a tag, it will automatically pull the latest version of the Image by appending the “latest” tag to the Image.
I have some nice and clean solution using environment variables (bash syntax for default variable value, in my case it is latest
but you can use anything ), this is my compose:
version: '3' services: app: build: . image: myapp-name:${version:-latest}
build and push (if you need to push to the registry) with the default tag, change the version using environment variable and build and push again:
docker-compose build docker-compose push export version=0.0.1 docker-compose build docker-compose push
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With