[Python] PermissionError: [Errno 13] Permission denied: '/var/log/gunicorn.error.log'

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Django app and I want to deploy it in docker but I got the error PermissionError: [Errno 13] Permission denied: '/var/log/gunicorn.error.log'. I have no idea anymore where the problem is. could someone help me, please?

    My dockerfile:


    # BUILDER #
    ###########

    # pull official base image
    FROM python:3.9.6-slim as builder

    # set work directory
    WORKDIR /app

    # set environment variables
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1

    # install system dependencies
    RUN apt-get update \
    && apt-get install -y build-essential python3-dev python2.7-dev \
    libldap2-dev libsasl2-dev libssl-dev ldap-utils tox \
    lcov valgrind \
    && apt-get -y install gcc \
    && apt install -y netcat

    # install psycopg2 dependencies
    RUN apt-get install -y postgresql-server-dev-all musl-dev


    # lint
    RUN pip install --upgrade pip
    COPY . .
    # install python dependencies
    COPY requirements.txt .
    RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt



    # pull official base image
    FROM python:3.9.6-slim
    RUN apt-get update && apt install -y netcat
    # create directory for the app user
    RUN mkdir -p /home/app
    # create the app user
    RUN addgroup --system --disabled-password app && adduser --system --group --disabled-password app
    # RUN addgroup --system app && adduser --system --group app

    # RUN adduser --disabled-password --gecos '' app

    # create the appropriate directories
    ENV APP_HOME=/home/app
    WORKDIR $APP_HOME

    # install dependencies
    COPY --from=builder /usr/src/app/wheels /wheels
    COPY --from=builder /app/requirements.txt .
    RUN pip install --upgrade pip
    RUN pip install --no-cache /wheels/*



    # copy entrypoint.sh
    COPY ./entrypoint.sh $APP_HOME
    RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.sh
    RUN chmod +x $APP_HOME/entrypoint.sh


    # copy project
    COPY . $APP_HOME

    # chown all the files to the app user
    RUN chown -R app:app $APP_HOME

    # change to the app user
    USER app

    # run entrypoint.sh
    ENTRYPOINT ["/home/app/entrypoint.sh"]


    and the entrypoint file:

    #!/bin/sh

    if [ "$DATABASE" = "postgres" ]
    then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
    sleep 0.1
    done

    echo "postgres database has initialized successfully"
    fi

    echo"ls"
    ls
    # echo "Running Migrations"
    # python manage.py migrate --no-input
    # python manage.py collectstatic --no-inp
    # Start server
    echo "who am i"
    whoami
    echo "Server Started"
    gunicorn -c gunicorn.conf.py culture_crawler.wsgi:application
    # python manage.py runserver 0.0.0.0:8000


    exec "$@"




    the gunicorn.config.py file

    # gunicorn.conf.py
    # Non logging stuff
    bind = "0.0.0.0:8000"
    workers = 3
    # Access log - records incoming HTTP requests
    accesslog = "/var/log/gunicorn.access.log"
    # Error log - records Gunicorn server goings-on
    errorlog = "/var/log/gunicorn.error.log"
    # Whether to send Django output to the error log
    capture_output = True
    # How verbose the Gunicorn error logs should be
    loglevel = "info"



    and the whole error :

    web_1 | Server Started
    web_1 | Traceback (most recent call last):
    web_1 | File "/usr/local/bin/gunicorn", line 8, in <module>
    web_1 | sys.exit(run())
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in run
    web_1 | WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 228, in run
    web_1 | super().run()
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 72, in run
    web_1 | Arbiter(self).run()
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 58, in __init__
    web_1 | self.setup(app)
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 93, in setup
    web_1 | self.log = self.cfg.logger_class(app.cfg)
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/glogging.py", line 195, in __init__
    web_1 | self.setup(cfg)
    web_1 | File "/usr/local/lib/python3.9/site-packages/gunicorn/glogging.py", line 207, in setup
    web_1 | self.logfile = open(cfg.errorlog, 'a+')
    web_1 | PermissionError: [Errno 13] Permission denied: '/var/log/gunicorn.error.log'


    thanks in advance

    Continue reading...

Compartilhe esta Página