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

2024-05-25

我有一个 python 脚本,我想将其容器化

测试远程.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

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

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


这是你的问题:

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 ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker 从密码数据库获取用户名时出错 的相关文章

随机推荐