0

I am creating NGINX container. I want to write all logs into a mounted volume rather than the default volume. This I can achieve by updating nginx.conf file by pointing access_log and error_log to a folder in mounted volume. The twist is that I want each container to write to container specific folder within the mounted volume.

For eg: Container image name: mycontainerapp Mounted volume: /logdirectory Then I want: /var/log to point to /logdirectory/mycontainerapp/{containerID}/log

This way, I can have multiple containers log to the common mounted volume.

AFAIK, I can get container ID from /proc/1/cpuset I am not sure of any other way to get the container ID

Question is, how can I read that containerID and use it to create the mounted volume (with folder name) using DOCKERFILE?

Also, if there is a better approach to what I am trying to achieve, please do let me know as I am a newbie to docker.

AJs
  • 191
  • 1
  • 4
  • Do you want to read the container Id from the host or from a container? See ` docker ps` and ` docker inspect` at least – user2915097 Nov 13 '17 at 19:27

1 Answers1

0

Docker has a logging mechanism included which removes standard log files from the equation. All data sent to stdout and stderr will be captured by Dockers logging interface.

There are a number of logging drivers that can then ship logs from your Docker host to a central logging service (Graylog, Syslog, AWS CloudWatch, ETW, Fluentd, Google Cloud, Splunk). The json driver is the default which is locally stored on the Docker host. journald will also be stored and accessible locally.

In nginx config, or any container for that matter, send the access log stdout or /dev/fd/1 and send the error log to stderr or /dev/fd/2

daemon off;
error_log /dev/fd/2 info;

http {
  access_log /dev/fd/1;
  ...
}

Once you start applying this concept to all containers, any log management requirements are removed from the container/application level and pushed up to the host. Container meta data can be attached to logs. It becomes easier to move or change the logging mechanism. Moving to clustered setups like Swarm becomes less of a hassle. This all ties into the 1 process per container idea of the world that Docker pushes.

Matt
  • 68,711
  • 7
  • 155
  • 158