Docker错误:'useradd:无法锁定/ etc / passwd'

时间:2019-02-01 12:36:08

标签: docker azure-devops

我在Azure DevOps中创建了一个管道来运行Docker cirrus / flutter映像。当Azure尝试初始化容器时(在useradd命令中),将发生错误。以下是执行日志的最后一部分,其中包含错误:

##[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

这是我的azure-pipelines.yml

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

我该如何解决? 我感谢大家。

2 个答案:

答案 0 :(得分:1)

您无法锁定passwd文件,因为您没有以root用户身份运行useradd命令。 cirrusci/flutter图像默认以cirrus用户身份运行:

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

您需要root才能修改/etc/passwd。您可以使用-u的{​​{1}}选项在现有容器内以root用户身份运行命令。比较一下:

docker exec

对此:

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

在这种特定情况下,看起来$ docker exec -u root flutter useradd testuser $ 用户能够运行cirrus,因此您也可以完成以下操作:

sudo

答案 1 :(得分:0)

container移动到resource部分,并将docker选项--user 0:0传递为options

例如:

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