I need to create a few images for my application functionality (a few web applications in azure that work together) and I also want to mark the images with several tags like latest, minor, major, and full versions. I have read about extensions here
https://stackoverflow.com/a/59911532/4511281 and https://www.back2code.me/2020/01/multiple-image-tags-with-docker-compose/.
But it’s not so clear for me, is that will rebuild the same image 4 times or it will build only once and then tag them 4 times? Or should I just build images with the "latest" tag and then use some other commands to tag the images and push them to container storage?
3
Answers
There are actually a few ways to build images and add multiple tags.
slow version with docker-compose
rebuild images several times with different tags passed as environment variables. This version will use cashed images for the next build, but anyway it will take time to rebuild the image.
using docker-compose extensions
based on information on the website it is possible to define several tags with the same yaml file. Of course, they can be passed as environment variables. I am not sure if this method doesn't rebuild the images again, haven't tested it. https://www.back2code.me/2020/01/multiple-image-tags-with-docker-compose/#use-yaml-extension-to-define-multiple-tags
using docker buildx bake
I like this version more. It also uses extensions, but it allows you to define tags much easy. https://docs.docker.com/engine/reference/commandline/buildx_bake/#examples
Hope this review can help someone else.
Then, want you to make several services, tagged with several names? Maybe you should create a docker-compose.yaml file to define all of those images tag. Every Service has his own directory, in which there is the Dockerfile runned when by docker-compose.yaml when you launch
Have a look at this project, I think that you want something like that: https://github.com/Aragorn1992gb/multi-docker4/blob/master/docker-compose.yml
Using this approach, you will have n different images as n services you built. Docker compose will link all of those services to work all together. In the example you can see that there are’postgres’ and ‘redis’ service that load a pre-built image from docker-hub. Then there are also ‘nginx’, ‘api’, ‘client’ and ‘worker’ services, that follow the Dockerfile.dev inside their folders.
To be more clear, a service tag can be different from the folder name of the service. Have a look to ‘api’ service:
Hoping that this can help you
UPDATES: reading better your question, the approach is similar and the images are, of course, different.
Since Compose 2.6.0 you can define a list of
tags
in thebuild
section:That is not yet supported by
docker buildx bake
, in this case you have to puttags
insidex-bake
: