Gunicorn cannot open its log file in a Docker container
PermissionError: [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.
Fix it fast
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'2Match what you see, then jump to its fix
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.yml2Fix
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 20Confirmed by gunicorn.org
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/gunicorn2Fix
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 appuser3Verify
Rebuild, start, and check that the log file appears.
docker compose up -d --build
docker compose exec web ls -l /var/log/gunicornConfirmed by gunicorn.org
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
Servers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix