How to fix MongoDB Docker Can't initialize rotatable log file :: caused by :: Failed to open /var/log/mongodb/mongod.log
Problem
While start up your MongoDB Docker container, you see the following error:
{"t":{"$date":"2025-05-26T16:58:22.276Z"},"s":"F", "c":"CONTROL", "id":20574, "ctx":"main","msg":"Error during global initialization","attr":{"error":{"code":38,"codeName":"FileNotOpen","errmsg":"Can't initialize rotatable log file :: caused by :: Failed to open /var/log/mongodb/mongod.log"}}}
Solution
You mapped the MongoDB log directory to a host directory, but the user inside the container does not have permission to write to that directory.
Example mapping in your docker-compose.yml
:
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- "27017:27017"
volumes:
- ./mongodb_data:/data/db
- ./mongodb_log:/var/log/mongodb
You need to fix the permissions of the host directory ./mongodb_log
so that the user inside the container can write to it.
You can do this by running the following command on your host system:
sudo chown -R 999:999 ./mongodb_log ./mongodb_data
Setting the permissions for the data directory is not required to fix this specific error message but if you encouter the error for the log message, you might as well fix the data directory permissions at the same time.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow