1
votes

I am using self hosted agent to run my build pipeline. The agent machine has got the base docker image downloaded. The pipeline builds and pushes a new docker image (based on top of the base image) using the 1st task as shown in the following screenshot.

docker tasks

Once the image is built and pushed to the container registry, I wish to clean up the images (created as part of this pipeline) from the self hosted agent to avoid disk space issues in the future (the base image should remain). Is there a Docker task to cleanup/remove the built image? In the azure build pipeline, how to cleanup the docker image from self hosted agent once it is pushed?

1
docker rm <CONTAINER_ID>vitkarpov
Do you mean docker rm <IMAGE_ID>? Can't hardcode it!variable
You can remove all stopped containers like this docker rm $(docker ps -a -q). Probably you can get the ids you need using docker ps.vitkarpov
Your commands (docker ps) are related to docker container. But my question is related to docker image. Does deleting docker container automatically delete docker image?variable
stopping a container does not remove the image, but docker rmi <image id> does.Frank Nielsen

1 Answers

0
votes

This answer might arrive late but here it goes.

I use this sort of flow to clean the images (and more) from the agent:

  - job: DockerCleanBuildAndTest
    displayName: Docker Build and Test
    workspace:
      clean: all
    steps:
      - task: DockerCompose@0
        displayName: Clean
        inputs:
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: ${{ variables['container-registry-name'] }}
          dockerComposeFile: '**/docker-compose.yml'
          action: 'Run a Docker Compose command'
          dockerComposeCommand: down
          detached: false

      - task: DockerCompose@0
        displayName: Run Tests
        inputs:
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: ${{ variables['container-registry-name'] }}
          dockerComposeFile: '**/docker-compose.yml'
          action: 'Run a Docker Compose command'
          dockerComposeCommand: 'run all-tests'
          detached: false

Maybe in your case it would be something like:

- job: DockerCleanBuildAndTest
    displayName: Docker Build and Test
    workspace:
      clean: all
    steps:
      - task: DockerCompose@0
        displayName: Clean
        inputs:
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: ${{ variables['container-registry-name'] }}
          dockerComposeFile: '**/docker-compose.yml'
          action: 'Run a Docker Compose command'
          dockerComposeCommand: rm     #  <---
          detached: false

      - task: DockerCompose@0
        displayName: Run Tests
        inputs:
          containerregistrytype: 'Container Registry'
          dockerRegistryEndpoint: ${{ variables['container-registry-name'] }}
          dockerComposeFile: '**/docker-compose.yml'
          action: 'Run a Docker Compose command'
          dockerComposeCommand: 'run all-tests'
          detached: false

Reference: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/docker-compose?view=azure-devops