简体   繁体   中英

Using pytz as non-root in Docker with Alpine Linux causes “IOError: [Errno 13] Permission denied”

I have the following Dockerfile (it's a bit stripped down, the core elements are displayed here)

FROM alpine:latest

RUN adduser -D some_user_name &&\
    apk add --update alpine-sdk &&\
    apk add --update python-dev &&\
    apk add --update py-dateutil &&\
    apk add --update py-tz

USER some_user_name

CMD ["/bin/sh"]

I then run this image the following way

docker run -d\
           -w /home/some_user_name/python/\
           -v ~/dockerfiles/mount_this/python:/home/some_user_name/python\
           --hostname docker-test --name dock-test\
           alpine/01\
           python executor.py settings:docker-lean

The container exits immediately, docker logs shows the following error

  File "/home/some_user_name/python/executor_handler.py", line 23, in <module>
    import pytz
  File "/usr/lib/python2.7/site-packages/pytz/__init__.py", line 29, in <module>
    from pkg_resources import resource_stream
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3141, in <module>
    @_call_aside
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3127, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3154, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 631, in _build_master
    ws = cls()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 624, in __init__
    self.add_entry(entry)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 680, in add_entry
    for dist in find_distributions(entry, True):
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2122, in find_on_path
    path_item, entry, metadata, precedence=DEVELOP_DIST
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2521, in from_location
    py_version=py_version, platform=platform, **kw
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2838, in _reload_version
    md_version = _version_from_file(self._get_metadata(self.PKG_INFO))
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2486, in _version_from_file
    line = next(iter(version_lines), '')
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2654, in _get_metadata
    for line in self.get_metadata_lines(name):
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1623, in get_metadata_lines
    return yield_lines(self.get_metadata(name))
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1615, in get_metadata
    return self._get(self._fn(self.egg_info, name))
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1726, in _get
    with open(path, 'rb') as stream:
IOError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg-info/PKG-INFO'

When I run the image as root by adding the -u root option to the docker run command, then the python script executes successfully, but I really don't want to run the container as root.

What can I do to solve this issue?

Ok, I added

RUN python -c "import pytz;" &&\
    chmod 644 /usr/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg-info/PKG-INFO

to the Dockerfile so that it ended up like this:

FROM alpine:latest

RUN adduser -D some_user_name &&\
    apk add --update alpine-sdk &&\
    apk add --update python-dev &&\
    apk add --update py-dateutil &&\
    apk add --update py-tz

RUN python -c "import pytz;" &&\
    chmod 644 /usr/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg-info/PKG-INFO

USER some_user_name

CMD ["/bin/sh"]

While this solved the issue, this might be something that needs to get fixed in pytz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM