ealeon
ealeon

Reputation: 12462

Docker error getting username from password database

I have a python script which i would like to containerized

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

which runs fine

[eugene@mymachine workdir]# python test_remote.py 
Hi, I am eugene

To run this script in a container, I wrote the following 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"]

When I run the image, it's not able to do a lookup.

[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'

I've tried to create a user and run it as via adding the following lines in Dockerfile

RUN useradd -ms /bin/bash eugene
USER eugene

but i am still getting the error lookup failed error

Any suggestions? how would I get "eugene" from test_remote.py if I don't do a look up against password database. I suppose one way would be set USERNAME as an env var and have the script parse that.

Upvotes: 4

Views: 2797

Answers (1)

larsks
larsks

Reputation: 312370

This is your problem:

userid lookup failed: 'getpwuid(): uid not found: 52712'

Inside your Docker container, there is no user with UID 52712. You can create one explicitly when you build the image:

RUN useradd -u 52712 -ms /bin/bash eugene

Or you can mount /etc/passwd from your host when you run it:

docker run -v /etc/passwd:/etc/passwd ...

Upvotes: 5

Related Questions