I have a Yii application with docker-compose.yml:
version: '3'
services:
web:
image: nginx
restart: always
volumes:
- ./docker/conf/nginx/default.conf:/etc/nginx/conf.d/default.conf
- .:/var/www/html
ports:
- 80:80
depends_on:
- php
php:
image: yiisoftware/yii2-php:7.1-fpm
restart: always
volumes:
- ./docker/conf/php/php.ini:/usr/local/etc/php/conf.d/php.ini
- .:/var/www/html
All is good. Then some functions were added to save logs to folder /var/www/html/data/logs.
I do my stuff, connect to container and see
sudo docker container exec -it php_1 bash
root@16270e4c7275:/app# ls -la /var/www/html/data/logs
total 1
drwxrwxrwx 1 root root 0 Jan 12 02:03 . // write permissions to all users for /var/www/html/data/logs
drwxrwxrwx 1 root root 0 Dec 30 03:12 ..
-rwxrwxrwx 1 root root 104 Jan 12 02:03 12-01-2021.log // log is here
And then I want to save logs when container will be down. What should I use?
I try to make a volume.
File docker-compose.yml
php:
image: yiisoftware/yii2-php:7.1-fpm
restart: always
volumes:
- ./docker/conf/php/php.ini:/usr/local/etc/php/conf.d/php.ini
- .:/var/www/html
- useractionlogs:/var/www/html/data/logs
volumes:
pgdata:
useractionlogs:
But saving logs failed š
In the container I see:
sudo docker container exec -it php_1 bash
root@16270e4c7275:/app# ls -la /var/www/html/data/logs
total 1
drwxr-xr-x 1 root root 0 Jan 12 02:03 . // write permissions are not to all users for /var/www/html/data/logs
drwxrwxrwx 1 root root 0 Dec 30 03:12 ..
I can do this in the container:
chmod go+rw /var/www/html/data/logs
ls -la /var/www/html/data/logs
total 4
drwxrwxrwx 2 root root 4096 Jan 12 02:19 .
drwxrwxrwx 1 root root 0 Dec 30 03:12 ..
But I should do that after all recreation of the volume.
How can I make logs to be saved correctly? Is there may be another way?
2
Answers
A correct way to store logs is to send it to stdout. https://12factor.net/logs
https://serverfault.com/questions/599103/make-a-docker-application-write-to-stdout/634296#634296
and others
Then work with https://docs.docker.com/config/containers/logging/
Where is a log file with logs from a container?
To my knowledge, this has to be done through a custom dockerfile based of an nginx base image and by using that you can change the ownership of the content within the /var/www/html directory to the group (for example, Apache uses the www-data user and group) and change the file permissions too.
This way you can follow the security tradition of not using a root as a group permission and also overcome the fact that you have to change permissions manually each and every time.