2

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?

2
  • If you run docker build (or its equivalent) multiple times on the same source tree, you will get the same image; for example, docker images should show you the same image ID with multiple tags. Is that what you're asking about?
    – David Maze
    Commented Mar 14, 2022 at 10:09
  • yes, but anyway it takes much more time than just adding more tags to the same image. And even if in between 2 build events some dependency will be changed, I guess the final image would be also changed. And is there any possibility to add some information in the docker-compose file to mention which tags I want to add to the specific image? Commented Mar 14, 2022 at 10:56

3 Answers 3

2

Since Compose 2.6.0 you can define a list of tags in the build section:

services:
  foo:
    image: foo:latest # If omitted will default to 'project-service:latest'.
    build:
      context: .
      tags:
      - foo:1.2
      - foo:1.2.3

That is not yet supported by docker buildx bake, in this case you have to put tags inside x-bake:

services:
  foo:
    image: foo:latest # Will be ignored if you use tags below.
    build:
      context: .
      x-bake:
        output: type=image
        tags:
        - foo:latest
        - foo:1.2
        - foo:1.2.3
1

There are actually a few ways to build images and add multiple tags.

  1. 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.

  2. 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

  3. 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.

0

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

docker-compose run

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:

  • it depends on postgres (this is the relationship)
  • his image is built by Dockerfile.dev file (you use .dev only in development environment). It is stored on "server" folder, then copied inside the container at app folder (./server:app)
  • it has some volumes in some directories (that means when you change something in those folders, the application will takes the updates without re-generate the image)
  • environment specify some env variables

Hoping that this can help you

UPDATES: reading better your question, the approach is similar and the images are, of course, different.

2
  • it's not actually what I am asking for. I already have a docker-compose.yml file with a list of services that I need to be built. But because I want to mark them with a few tags (each service) I have to run the docker-compose several times with passing tag each time and it's starts to build the images again (using cach of course). But I want to marke images with tags and not build them several times. Commented Mar 14, 2022 at 8:53
  • but the approach is similar, you have to create images for each version and tag them into docker-compose Commented Mar 14, 2022 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.