Home > database >  Running a docker container pulled from Azure Container Registry in Azure DevOps pipeline
Running a docker container pulled from Azure Container Registry in Azure DevOps pipeline

Time:09-16

I would like to run a docker container, which i pull from Azure Container Registry. All of this i would like to do in Azure DevOps pipeline. Firstly i created sample Node.js app and Dockerized it with this tutorial: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ Then i did my Azure Pipeline which firstly do build&push and then pull and run. My pipeline:

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
    - task: Docker@2
      displayName: Docker pull
      inputs:
        command: pull
        containerRegistry: $(dockerRegistryServiceConnection)
        arguments: container01.azurecr.io/devopsnodejs:latest
    - task: Docker@2
      displayName: Login to ACR
      inputs:
        command: login
        containerRegistry: $(dockerRegistryServiceConnection)
    - script: |
        docker run -p 49160:8080 -d container01.azurecr.io/devopsnodejs:latest

The pipelines runs every step sucessfully, the last script with docker run prints this to Azure DevOps console

Generating script.
Script contents:
docker run -p 49160:8080 -d ***/devopsnodejs:latest
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/b117892d-e34c-484c-ad8c-f99cd0a97e18.sh
7c6c9d548c4be3e4568e56ffc87cca27e698fc53b5ec15a1595cd45fe72dd143

And now the problem is that, I cannot acces the app which should return simply get request saying 'Hello World' Im trying to go to localhost:49160, to curl -i localhost:49160 but there is only curl: (7) Failed to connect to localhost port 49160 after 2258 ms: Connection refused

Also, if i do it locally, not in azure pipelines, so I simply run docker pull container01.azurecr.io/devopsnodejs:latest and docker run -p 49160:8080 -d container01.azurecr.io/devopsnodejs:latest in powershell, the docker ps will show me this container, as well as the curl -i localhost:49160 will work. Am I able to access this locally, or if i run it in Azure Pipelines it will work only there?

CodePudding user response:

Have you seen this question already, which is I think your scenario?

Cannot conect to Docker container running in VSTS

CodePudding user response:

You could use a bash script with ssh to logon to the target machine and perform docker commands on the machine. The following is just an example. I have something like this in one of my pipelines.

- task: Bash@3
  displayName: "Deploy image $(imageName)"
  inputs:
    targetType: 'inline'
    failOnStderr: false
    script: |
        sshpass -p '$(localroot-userpassword)' ssh -o StrictHostKeyChecking=no $(localroot-username)@$(remoteHost) "{ sudo docker stop $(imageName) ; sudo docker rm $(imageName) ; sudo docker pull $(imageName) ; sudo docker run ...#add other commands separated by semicolon}"
  • Related