Python 从密码数据库获取用户名时发生Docker错误

Python 从密码数据库获取用户名时发生Docker错误,python,python-2.7,docker,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

我有一个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"]
RUN useradd -ms /bin/bash eugene
USER eugene
当我运行图像时,它无法进行查找

[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'
但我仍然得到错误查找失败错误

有什么建议吗? 如果我不查找密码数据库,如何从test_remote.py获取“eugene”。我想一种方法是将USERNAME设置为env var,并让脚本解析它

这是你的问题:

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 ...

你运行了什么命令?问题是docker映像中没有uid的映射名称,这是导致问题的原因。有关组@TarunLalwani的类似问题,请参阅此线程。您是什么意思?我运行了什么命令?这是一个python代码“userid=pwd.getpwuid(os.stat('..).st_uid).pw_name”,好的,我来看看这个问题不,我是说你是如何运行docker容器的?啊,我把这个映像构建为“docker build-t testremote”,然后运行“docker run testremote”谢谢@larsks有没有办法让52712在Dockerfile中不被硬编码,这样容器就可以用于任何用户?我将尝试安装/etc/passwd。这可能适用于任何用户。对大多数情况,只需安装/etc/passwd即可。还有其他解决此问题的方法(例如,将用户id作为变量传入,并使用
ENYTRPOINT
脚本在运行时创建用户…),但它们不太方便。