Search code examples
docker.net-coreazure-pipelinesubuntu-18.04sudo

Azure pipeline . Install sudo in ubuntu 18.04 docker container failed - Permission Denied


I am trying to run CI/CD in Azure pipeline .Because my app need some dependencies that only work ubuntu 18.04 however that VmImage version deprecated in Azure hosted agent. That is why I run it on ubuntu 18.04 docker container. It seem the docker container does not have sudo installed so I try to install it. Here is my pipeline code

  pool: 
    vmImage: ubuntu-latest
  container: ubuntu:18.04
  dependsOn: ImpactedProjects
  condition: eq(dependencies.ImpactedProjects.outputs['ImpactAnalysis.Project.Loyalty.QueryCachePopulator.AzureFunction.BddTests'], 'True')
  steps:
  - script: apt-get install sudo
    displayName: 'Install sudo'

However then I got this error

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
##[error]Bash exited with code '100'.
Finishing: Install sudo

Is there anyone know how to fix this please. Thanks


Solution

  • I can reproduce the same issue when using the ubuntu 18.04 in Container job.

    enter image description here

    The cause of this issue is that this image runs as non-root, so for root-access things you have to switch into root.

    In a Dockerfile, you can simply switch user identities with a USER directive; this generally defaults to running as root:

    USER root
    

    To resolve this issue, you need to custom your docker image based on the image: ubuntu18.04.

    For example:

    FROM ubuntu:18.04
    USER root
    RUN apt-get update && apt-get install -y 
    RUN apt-get install sudo
    USER 1001
    

    Then you can use the custom image in container job.

    For example:

    pool: 
      vmImage: ubuntu-latest
    container: 
     image: xxx/ubuntu18.04:23787
     endpoint: dockerserviceconnection
     options: --user root --privileged
    
    steps:
    - script: sudo apt-get install sudo
    

    For more detailed info, you can refer to this ticket and doc: Container Job Option

    Update

    We can use the following sample to install the sudo package to the existing Ubuntu 18.04 image.

    pool: 
      vmImage: ubuntu-20.04
    
    container: 
     image: ubuntu:18.04
     options: "--name sample -v /usr/bin/docker:/tmp/docker:ro"
    
    steps:
      - script: |
          /tmp/docker exec -t -u root sample mv /etc/sudoers /etc/sudoers.bak
          /tmp/docker exec -t -u root sample apt-get -qq update
          /tmp/docker exec -t -u root sample apt-get -qq install sudo
          /tmp/docker exec -t -u root sample mv /etc/sudoers.bak /etc/sudoers
      - script: sudo xxx
    

    Note: If you are using the Microsoft-hosted agent, we need to use the VMimage: ubuntu-20.04

    For more detailed info, you can refer to this ticket: Can't acquire root on common container distros