Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker error: 'useradd: cannot lock /etc/passwd'

I created a pipeline in Azure DevOps to run the Docker cirrus/flutter image. An error occurs when Azure tries to initialize the container (in the useradd command). Below is the final part of the execution log with the error:

##[command]/usr/bin/docker exec  5ae52fcbeefecc0df48056df5f9d673429fe5173e7a8e3d984d889ce5223c34c sh -c "command -v bash"
/bin/bash
##[command]whoami 
vsts
##[command]id -u vsts
1001
Try create an user with UID '1001' inside the container.
##[command]/usr/bin/docker exec  5ae52fcbeefecc0df48056df5f9d673429fe5173e7a8e3d984d889ce5223c34c bash -c "grep 1001 /etc/passwd | cut -f1 -d:"
##[command]/usr/bin/docker exec  5ae52fcbeefecc0df48056df5f9d673429fe5173e7a8e3d984d889ce5223c34c id -u bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
id: extra operand 'warning:'
Try 'id --help' for more information.
##[command]/usr/bin/docker exec  5ae52fcbeefecc0df48056df5f9d673429fe5173e7a8e3d984d889ce5223c34c useradd -m -u 1001 vsts_azpcontainer
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
##[error]Docker exec fail with exit code 1
##[section]Finishing: Initialize containers

This is my azure-pipelines.yml

jobs:
- job: Build
  pool:
    vmImage: 'ubuntu-16.04'
  container: cirrusci/flutter:latest
  steps:
  - bash: flutter doctor

How can I solve this? I thank everyone.

like image 625
Luciano Avatar asked Feb 01 '19 12:02

Luciano


2 Answers

Move the container to a resource section and pass the docker option --user 0:0 as options.

For example:

resources:
  containers:
  - container: flutter
    image: cirrusci/flutter:latest
    options: --user 0:0

jobs:
- job: Build
  pool:
    vmImage: 'ubuntu-16.04'
  container: flutter
  steps:
  - bash: flutter doctor
like image 111
big_gie Avatar answered Sep 17 '22 08:09

big_gie


You can't lock the passwd file because you're not running the useradd command as root. The cirrusci/flutter image runs as the cirrus user by default:

$ docker run -it  cirrusci/flutter id
uid=1000(cirrus) gid=999(cirrus) groups=999(cirrus),27(sudo)

You need to be root to modify /etc/passwd. You can run a command as root inside an existing container by using the -u option to docker exec. Compare this:

$ docker exec flutter useradd testuser
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
$

To this:

$ docker exec -u root flutter useradd testuser
$

In this specific case, it looks as if the cirrus user is able to run sudo, so you could also accomplish the same thing like this:

$ docker exec flutter sudo useradd testuser
$
like image 21
larsks Avatar answered Sep 21 '22 08:09

larsks