Docker错误从密码数据库获取用户名

时间:2018-01-30 18:14:55

标签: python python-2.7 docker

我有一个python脚本,我想集装箱化

test_remote.py

import os
import pwd
try:
    userid = pwd.getpwuid(os.stat('.').st_uid).pw_name
except KeyError, err:
    raise Exception('NIS Problem: userid lookup failed: %s' % err)
print "Hi, I am %s" % userid

运行良好

[eugene@mymachine workdir]# python test_remote.py 
Hi, I am eugene

要在容器中运行此脚本,我编写了以下Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim

WORKDIR /data

# Copy the current directory contents into the container at /app
ADD . /data

# Install any needed packages specified in requirements.txt
RUN pip install -r /data/requirements.txt

CMD ["python", "/data/br-release/bin/test_remote.py"]

当我运行图片时,它无法进行查找。

[eugene@mymachine workdir]# docker run -v testremote
Traceback (most recent call last):
  File "/data/test_remote.py", line 27, in <module>
    raise Exception('NIS Problem: userid lookup failed: %s' % err)
Exception: NIS Problem: userid lookup failed: 'getpwuid(): uid not found: 52712'

我尝试创建用户并通过在Dockerfile中添加以下行来运行它

RUN useradd -ms /bin/bash eugene
USER eugene

但我仍然收到错误查找失败错误

有什么建议吗? 我怎么会得到&#34; eugene&#34;来自test_remote.py,如果我不查看密码数据库。我想一种方法是将USERNAME设置为env var并让脚本解析它。

1 个答案:

答案 0 :(得分:1)

这是你的问题:

userid lookup failed: 'getpwuid(): uid not found: 52712'

在Docker容器中,没有UID 52712的用户。您可以在构建映像时显式创建一个:

RUN useradd -u 52712 -ms /bin/bash eugene

或者您可以在运行主机时挂载/etc/passwd

docker run -v /etc/passwd:/etc/passwd ...