Home > Software engineering >  Adding a tag to all images in a docker-compose file?
Adding a tag to all images in a docker-compose file?

Time:12-19

For all my images I have a latest tag for production, and test for testing. I also have 2 docker-compose files that only differ in that one uses latest on all packages (except nginx), while the other uses test. Is there a way to set this via a CLI variable, so that I don't have to keep those two files in sync manually all the time?

CodePudding user response:

We can handle this with an environment variable.

docker-compose.yml:

services:
  ...
  my-service-one:
    image: my/service-one:$TAG
    ...
  my-service-two:
    image: my/service-two:$TAG
    ...

Then we can add an .env file (for production) with content

TAG=latest

and a test.env file (for testing) with content

TAG=test

When we run docker compose up, file .env will be used by default. If we want to start our test deployment, we can run docker compose --env-file test.env up.

CodePudding user response:

You can simply run these commands for each compose file

TAG=latest docker-compose -p latestapp -f compose-for-latest.yaml up -d

and

TAG=test docker-compose -p testapp -f compose-for-test.yaml up -d

of course, don't forget to specify where you want to use your Tag variable in the compose files using ${TAG} or $TAG (BOTH ARE VALID)

If you are a LINUX user this should work fine.

if you are a WINDOWS user and the commands didn't work you can use GIT BASH instead.

  • Related