Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Gunicorn cannot open its log file in a Docker container

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

Short answer

Gunicorn is trying to open a log file the container user may not write. In Docker the best fix is to write logs to stdout and stderr with a dash instead of a file path, so docker logs shows them. If you need a file, create the directory and give it to the same user the container runs as.

  • Applies to Gunicorn (current docs), Docker Engine
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which user the container runs as and who owns the log folder. If they differ, the error is explained.

    docker compose run --rm web sh -c 'id && ls -ld /var/log'
  2. 2Match what you see, then jump to its fix

    1. 1The setting errorlog, accesslog or --error-logfile names a path under /var/logA file path for logs in a container where logs belong on stdoutFix
    2. 2id shows a user such as appuser, while ls -ld shows root as ownerThe log folder is owned by root but the app runs as a non-root userFix

What does the error look like?

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

Cause 1 of 2

A file path for logs in a container where logs belong on stdout

1Check

Search the Gunicorn config, the CMD and the compose file for log file settings.

grep -R -n -E 'errorlog|accesslog|error-logfile|access-logfile|log-file' gunicorn.conf.py Dockerfile docker-compose.yml

2Fix

Send both logs to the console. A dash means stderr for the error log and stdout for the access log. Docker collects them.

# gunicorn.conf.py
accesslog = "-"
errorlog = "-"

3Verify

Start the container and read its logs.

docker compose up -d && docker compose logs web --tail 20

Confirmed by gunicorn.org

Did this fix it?

Cause 2 of 2

The log folder is owned by root but the app runs as a non-root user

1Check

A Dockerfile that ends with USER appuser runs Gunicorn as that user. A folder made earlier, as root, stays root-owned.

docker compose run --rm web id
docker compose run --rm web ls -ld /var/log/gunicorn

2Fix

Create the log directory in the image and give it to the app user before the USER line.

RUN useradd --create-home appuser \
    && mkdir -p /var/log/gunicorn \
    && chown appuser:appuser /var/log/gunicorn
USER appuser

3Verify

Rebuild, start, and check that the log file appears.

docker compose up -d --build
docker compose exec web ls -l /var/log/gunicorn

Confirmed by gunicorn.org

Did this fix it?

How do I stop it coming back?

  • In containers, log to stdout and stderr and let Docker or your log shipper store them.
  • Run the app as a non-root user and create every writable folder for that user in the Dockerfile.
  • Do not mount a host folder over a log directory unless its owner matches the container user.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.