'docker compose pull newest image

I have a few microservices. Jenkins builds these projects, creates docker images and publishes them to the artifactory. I have another project for automation testing which is using these docker images. We have a docker-compose file that has all the configuration of all microservice images.

Following is sample docker-compose

version: "1.0"
services:
  my-service:
    image: .../my-service:1.0.0-SNAPSHOT-1
    container_name: 'my-service'
    restart: always
    volumes:
      ...
    ports:
      ...
  ...

all these are working fine. Now to update the image then I have to manually change the image tag (1.0.0-SNAPSHOT-2) in docker-compose.

This is an issue because this involves human intervention. Is there any way to pull the newest docker image without any change in docker-compose?

NOTE - I cannot create images with the latest tag. Getting issue when publishing image with the same name in the artifactory (unauthorized: The client does not have permission for manifest: Not enough permissions to delete/overwrite artifact).



Solution 1:[1]

Well What actually you can do is, use environment variables substitutions in cli commands (envsubst). Let me explain an escenario as example.

First in the docker-compose.yaml you define an environmet variable, as a tag of the container

version: "3"
  services:
    my-service:
      image: .../my-service:$TAG
      container_name: 'my-service'
      restart: always
      volumes:
        ...
      ports:
        ...
    ...

Second, with cli command (or terminal) you define an environment variable, with you version. This part is important because here you add your version tag to the container (and you can execute bash commands to extract some id, or last git commit or what ever you want to execute as tag for the container, i give you some ideas)

export TAG=1.0.0-SNAPSHOT-1
export TAG="$(bash /path/to/script/tag.sh)"
export TAG="$(git log --format="%H" -n 1)"

And the third part and last one is for execute "envsubst" and then execute docker-compose.yaml to deploy your container. Note the pipe |, very important for execution.

envsubst < docker-compose.yaml | docker-compose up -d

link to envsubst

I use this format to deploy tagged containers in kubernetes, but the idea must be the same with docker compose.

envsubst < deployment.yaml | kubectl apply -f -

And change version to 3 in the docker-compose.yaml. Good luck

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Felipe Illanes