skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


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

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search