Search code examples
dockerazure-devopsdocker-composeazure-pipelinesazure-pipelines-release-pipeline

How to rollback to older release in azure release pipeline which uses Docker-compose


Since I am building the image and pushing to the Azure container registry(so far it's ok.) the problem for me starting after this.

Let's say I build several images with the build tag. such as api-image:23, api-image:24, api-image:25 and so on.

Question comes here: I'm running with the tag 25(also latest) in production server then I want to rollback to api-image:23(Using azure release pipeline history). My docker-compose file also has this image: api-image value. It's going to get 'latest' image. How can I get those image tags as dynamically in my compose file? As you know, Azure Devops release pipelines has a release history. Let's say I want to rollback to ex-release. How my docker-compose file knows which version I want to rollback to? If I leave the tag empty its going to get a latest tag. But my ex-release builded with api-image:23.

Also, this image already in my azure registry so I don't need to rebuild the whole project again right? I should be using it without rebuilding the app?

ps. my hosts are debian 11 on-premise.

version: '3.3'

services:
  reverse-proxy:
    image: xx.azurecr.io/nginx
    container_name: mars_proxy
    build:
      context: .
      dockerfile: reverse-proxy/Dockerfile
    ports:
      - 80:80
    restart: always
  slider:
    image: xx.azurecr.io/mars-slider
    container_name: slider
    build:
      context: .
      dockerfile: Mars.Slider/Presentation/Dockerfile
    ports:
      - "8081:5100"
    restart: always

My Azure-pipelines.yml

trigger:
  - develop
 
steps:
  - task: DockerCompose@0
    displayName: "Container registry login"
    inputs: 
      containerregistrytype: "Azure Container Registry"
      #azureSubscription = Azure resource manager service connection name 
      azureSubscription: "subname"
      azureContainerRegistry: '{"loginServer":"xx.azurecr.io", "id" : ""}'
      dockerComposeFile: '**/docker-compose.yml'
      additionalImageTags: $(Build.BuildId)
      action: 'Build services'

  - task: DockerCompose@0
    inputs: 
      containerregistrytype: "Azure Container Registry"
      azureSubscription: "subname"
      azureContainerRegistry: '{"loginServer":"xx.azurecr.io", "id" : ""}'
      dockerComposeFile: '**/docker-compose.yml'
      additionalImageTags: $(Build.BuildId)
      action: 'Push services'

thanks.


Solution

  • You can use variable substitution to fill in environment-variable values in many places in a Compose file. This includes the image:. So if you're able to provide the image tag as an environment variable:

    version: '3.8'
    services:
      reverse-proxy:
        image: xx.azurecr.io/nginx:${IMAGE_TAG:-latest}  # <--
        ports:
          - 80:80
        restart: always
      slider:
        image: xx.azurecr.io/mars-slider:${IMAGE_TAG:-latest}  # <--
        ports:
          - "8081:5100"
        restart: always
    

    Then you can roll back just by providing the older value for the tag:

    IMAGE_TAG=23 docker-compose up -d
    

    You can similarly use this technique to upgrade to a known "current" version if other builds are ongoing, and to minimize the risk of the system having an incorrect "latest" version. If you're supplying the tag explicitly like this then you do not need to manually docker-compose pull the rebuilt images; Docker will fetch them automatically if they're not present.