我有一个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上得到“尤金”呢?我认为一种方法是将USERNAME设置为env var,然后让脚本对其进行解析。
发布于 2018-01-30 18:26:10
这就是你的问题:
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 ...
https://stackoverflow.com/questions/48527958
复制相似问题