【发布时间】:2018-01-31 11:55:09
【问题描述】:
我有一个想要容器化的 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
但我仍然收到错误查找失败错误
有什么建议吗? 如果我不查找密码数据库,我将如何从 test_remote.py 中获取“eugene”。我想一种方法是将 USERNAME 设置为 env var 并让脚本解析它。
【问题讨论】:
-
你运行了什么命令?问题是 docker 映像中没有 uid 的映射名称,这导致了问题。有关与组 stackoverflow.com/questions/46204643/… 相关的类似问题,请参阅此线程
-
@TarunLalwani 你是什么意思我运行了什么命令?这是一个python代码“userid = pwd.getpwuid(os.stat('.').st_uid).pw_name”,好吧,我看看那个问题
-
不,我的意思是你是如何运行你的 docker 容器的?
-
啊,我将映像构建为“docker build -t testremote”,然后运行“docker run testremote”
标签: python python-2.7 docker