skip to Main Content

I tried do run a docker in pipeline ,first i tried to use documentation : https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/docker-v2?view=azure-pipelines&viewFallbackFrom=azure-devops&tabs=yaml#troubleshooting but still got errors

Its looks like my pipeline properly download a docker file but when i try to exec any command in docker file i got errors

steps:
    - checkout: self
      clean: true
      fetchDepth: full

    - task: Docker@2
      displayName: Login to ACR
      inputs:
        containerRegistry: 'Docker'
        command: 'login'
    
    - script: |
        docker run -d docker.io/Docker/Sample-docker-image
      displayName: 'Run Docker'

    - task: Docker@2
      inputs:
        containerRegistry: 'Docker'
        command: 'start'
        container: 'Docker/Sample-docker-image'

when i run this i got

Error response from daemon: No such container: ***/Docker/Sample-docker-image
Error: failed to start containers: ***/Docker/Sample-docker-image
##[error]Error response from daemon: No such container: ***/Docker/Sample-docker-image
##[error]Error: failed to start containers: ***/Docker/Sample-docker-image
##[error]The process '/usr/bin/docker' failed with exit code 1

How to handle and properly use a docker ? Im doing something wrong in this yml ?

Edit:
i added

        container_id=$(docker run -d Docker/Sample-docker-image)
        echo "Container ID: $container_id"
        echo "##vso[task.setvariable variable=container_id]$container_id"
        container_id=$(echo $container_id)
       
        while [[ "$(docker inspect -f '{{.State.Status}}' $container_id)" != "running" ]]; do
          docker inspect $container_id
          docker logs $container_id
          sleep 10
          docker ps -a
        done

and it shows docker starts as exited status. i tried locally and every image works as expected

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for answers. I made mistake only thing which i missed is a letter -t

    docker run -t -d --name my-container 
    

    This solved a issue, container is running and im able to run docker exec


  2. Below are the steps to execute a command on the docker image.

    Step 1: Create a service connection to the container registry if it is private. For public registries, a service connection is not required.
    enter image description here

    Step 2: Specify the container in the resources section of the pipeline and add the service connection name as the endpoint as shown below.

    resources:
     containers:
       - container: acr-customimg
         image: cleveracr.azurecr.io/customimg:latest
         endpoint: ACR-endpoint
    

    Step 3: Add the container resource as the target to the steps you would like to execute on the docker container.

    - script: echo hostname
      displayName: 'run script in docker container'
      target: 
       container: acr-customimg
    

    Below is the full pipeline code.

    trigger: none
    
    pool:
      vmImage: ubuntu-latest
    
    resources:
     containers:
       - container: acr-customimg
         image: cleveracr.azurecr.io/customimg:latest
         endpoint: ACR-endpoint
    
    steps:
    - script: echo hostname
      displayName: 'run script in azure agent'
    
    - script: echo hostname
      displayName: 'run script in docker container'
      target: 
       container: acr-customimg
    

    When I run this pipeline, it first fetches the image from the container registry and then start the container. The first step will execute on the azure agent while the second step will execute on the docker container.

    References:

    1. Define container jobs | Microsoft doccumentation
    2. Step target | Microsoft doccumentation
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search