Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing docker nginx access logs inside a docker volume

Currently my docker container is printing the nginx access logs to /dev/stdout. How do I create a volume inside my docker container to store the access logs?

My Dockerfile:

FROM python:3.7

ENV APP_ROOT /src
ENV CONFIG_ROOT /config

RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get -y install unixodbc-dev
RUN apt-get -y install default-libmysqlclient-dev

RUN mkdir ${CONFIG_ROOT}
COPY /app/requirements.txt ${CONFIG_ROOT}/requirements.txt
RUN pip install -r ${CONFIG_ROOT}/requirements.txt

RUN mkdir ${APP_ROOT}
WORKDIR ${APP_ROOT}

ADD /app/ ${APP_ROOT}

My docker-compose.yml:

version: "3"

services:
  app:
    build: .
    container_name: django-gunicorn
    restart: always
    env_file:
      - django.env
    ports:
      - "8000:8000"
    command:
      "gunicorn --workers=2 --bind=0.0.0.0:8000 mysite.wsgi:application"

  nginx:
    image: nginx:1.14
    container_name: ngx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./app/static:/static
    depends_on:
      - app

My nginx/default.conf:

limit_req_zone "$binary_remote_addr$request_uri" zone=one:10m rate=60r/m;

server {
    listen 80;
    server_name example.org;
    server_tokens off;

    location  /static/ {
        autoindex on;
        alias /static/;
    }

    location / {
        proxy_pass http://app:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        limit_req zone=one nodelay burst=30;

    }
}

I am trying to add fail2ban and fluentd logging to this application but first I need to store the physical file (not /dev/stout) which can be used for other logging purposes.

Thanks you!

like image 990
wasabi_gardener Avatar asked Feb 27 '19 18:02

wasabi_gardener


People also ask

Where are logs stored inside Docker container?

You find these JSON log files in the /var/lib/docker/containers/ directory on a Linux Docker host.

How do I persist Docker container logs?

You can persist all container log files by creating a volume mount point to the Docker host machine or central log server. Since every container has its own unique log folder ( containerType _ containerId ), you can simply mount all container log directories (*/logs/) to the same path on your host machine.

Where are Docker nginx logs?

The NGINX image is configured to send the main NGINX access and error logs to the Docker log collector by default. This is done by linking them to stdout and stderr, which causes all messages from both logs to be stored in the file/var/lib/docker/containers/\/\-json.

Can I access the logs of the Docker container?

The docker logs command shows information logged by a running container. The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container's endpoint command.

How to store Nginx logs in a docker container?

By default the Nginx container forwards access logs to STDOUT and error logs to STDERR. You can see these lines in nginx Dockerfile: But if you want to store nginx logs in a docker volume (or a local mounted directory), first create your customized nginx docker image: And then your nginx service in docker-compose.yml would be like:

What are Docker volumes and how do I use them?

There are many software that log to files or pipes instead of their stdout, the place where Docker expects them. Fortunately, by using Docker volumes, you can share data among containers and syslog-ng can collect these logs as well.

How to check logs for Docker containers?

Logs for containers can be checked using the command docker logs <container-id> ... Our app is doing good and it is getting a lot of users every day. Something goes wrong and the container crashes. But that’s fine, we have docker by our side. So we can create a new container from the same image.

How do I forward Nginx error logs to dockerfile?

This is done by linking them to stdout and stderr 1. In the NGINX Dockerfile this is achieved by creating symbolic links to /dev/stdout and /dev/stderr: # forward request and error logs to docker log collector && ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log


2 Answers

By default the Nginx container forwards access logs to STDOUT and error logs to STDERR. You can see these lines in nginx Dockerfile:

# forward request and error logs to docker log collector
    && ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log \

So you can see nginx logs in container logs:

docker logs -f ngx

But if you want to store nginx logs in a docker volume (or a local mounted directory), first create your customized nginx docker image:

FROM nginx
RUN rm /var/log/nginx/*

And then your nginx service in docker-compose.yml would be like:

nginx:
    build: ./nginx/
    container_name: ngx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./app/static:/static
      - ./log/nginx:/var/log/nginx
    depends_on:
      - app
like image 200
Hamon Avatar answered Oct 06 '22 00:10

Hamon


Apart from the comments above to add the volume, you have to adjust main Nginx configuration (most probably /etc/nginx/nginx.conf) and/or configuration files for each vhost.

In most cases add these to your main configuration:

http {
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
}

Similar entries could be palces inside server or location

like image 34
Szczad Avatar answered Oct 06 '22 00:10

Szczad