skip to Main Content

I’m running a Dockerfile as non-root user. I have created a new directory and installed pipenv and gunicorn as root user. Once set user and group ownership to new user, it cannot run last CMD due to a permission denied.

My dockerfile

FROM python:3.10

ENV PYTHONUNBUFFERED 1

RUN mkdir /app
WORKDIR /app

RUN pip install pipenv

RUN pipenv install gunicorn --skip-lock

COPY Pipfile.lock ./
RUN pipenv sync

RUN adduser -g "appuser" -u 1111 --disabled-password appuser
USER 1111
WORKDIR /app
COPY --chown=appuser:appuser ./app ./app

CMD ["pipenv" , "run" , "gunicorn", ......]

I’m getting following error

Traceback (most recent call last):
  File "/usr/local/bin/pipenv", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pipenv/cli/options.py", line 58, in main
    return super().main(*args, **kwargs, windows_expand_args=False)
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pipenv/vendor/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pipenv/cli/command.py", line 418, in run
    do_run(
  File "/usr/local/lib/python3.10/site-packages/pipenv/core.py", line 2735, in do_run
    ensure_project(
  File "/usr/local/lib/python3.10/site-packages/pipenv/core.py", line 520, in ensure_project
    if project.s.PIPENV_USE_SYSTEM or project.virtualenv_exists:
  File "/usr/local/lib/python3.10/site-packages/pipenv/project.py", line 252, in virtualenv_exists
    if os.path.exists(self.virtualenv_location):
  File "/usr/local/lib/python3.10/site-packages/pipenv/project.py", line 449, in virtualenv_location
    self._virtualenv_location = self.get_location_for_virtualenv()
  File "/usr/local/lib/python3.10/site-packages/pipenv/project.py", line 274, in get_location_for_virtualenv
    return str(get_workon_home().joinpath(self.virtualenv_name))
  File "/usr/local/lib/python3.10/site-packages/pipenv/utils/shell.py", line 202, in get_workon_home
    os.makedirs(expanded_path, exist_ok=True)
  File "/usr/local/lib/python3.10/os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/usr/local/lib/python3.10/os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/usr/local/lib/python3.10/os.py", line 215, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/usr/local/lib/python3.10/os.py", line 225, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/home/appuser'

2

Answers


  1. Chosen as BEST ANSWER

    This issue was resolved by running following step after switching to new user

    RUN pipenv install gunicorn --skip-lock
    

  2. Change

    RUN adduser -g "appuser" -u 1111 --disabled-password appuser
    

    to

    RUN adduser -m -g "appuser" -u 1111 --disabled-password appuser
    

    Adding -m tells adduser to create a home directory for the appuser account.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search