我有一个 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 设置为环境变量并让脚本解析它。