Home > Back-end >  How can change Nginx default log file location in docker setting
How can change Nginx default log file location in docker setting

Time:10-06

I'm very new to docker and trying build docker compose with multiple service/app, also set the log file place separately.

If I run docker compose up will cause the open() file error like

FPM-nginx  | 2022/10/06 01:40:54 [emerg] 1#1: open() "/var/www/FPM/log/nginx/error.log" failed (2: No such file or directory)
FPM-nginx  | nginx: [emerg] open() "/var/www/FPM/log/nginx/error.log" failed (2: No such file or directory)

According relative answer, I've try adding the new command in Dockerfile but still causing the error.

The answers tried Nginx access log file path

The answers tried 2 Nginx log location

Currently Dockerfile docker-compose.yml nginx.conf like below

Dockerfile

FROM php:8.0.2-fpm

RUN mkdir -p /var/www/FPM/log/nginx/ \           <<<<<<<<< This is new add
    touch /var/www/FPM/log/nginx/error.log \     <<<<<<<<< This is new add
    touch /var/www/FPM/log/nginx/access.log \    <<<<<<<<< This is new add
    apt-get update && apt-get install -y \
    git \
    curl \
    zip \
    unzip

WORKDIR /var/www/FPM

nginx.conf

server {
    listen 80;
    index index.php;
    root /var/www/FPM/public;
    error_log /var/www/FPM/log/nginx/error.log;
    access_log /var/www/FPM/log/nginx/access.log;
    error_page 404 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

docker-compose.yml

version: "3.8"

services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: FPM-app
    restart: always
    working_dir: /var/www/FPM
    volumes:
      - ../src:/var/www/FPM

  nginx:
    image: nginx:1.23.1-alpine
    container_name: FPM-nginx
    restart: always
    ports:
      - 8000:80
    volumes:
      - ../src:/var/www/FPM
      - ./nginx:/etc/nginx/conf.d

I've also tried place

    error_log /var/www/FPM/log/nginx/error.log;
    access_log /var/www/FPM/log/nginx/access.log;

under location / but still cause the error

location / {
    error_log /var/www/FPM/log/nginx/error.log;
    access_log /var/www/FPM/log/nginx/access.log;
}

CodePudding user response:

You've multiple issues with you docker and docker-compose files.

According to your docker-compose.yml, when you would be doing docker-compose up you'll have two containers running one brought up from the docker image built at the time and the other brought up from public nginx:1.23.1-alpine image. The first image built will have the /var/www/FPM/log/nginx/ folder along with the error.log and access.log files but docker-compose up will overwrite the content because of this line:

volumes:
  - ../src:/var/www/FPM

That being said, you don't even need that folder and those files in the first (app) container in the first place. You need them in the nginx container. So you can remove these lines from the Dockerfile:

mkdir -p /var/www/FPM/log/nginx/ \ 
    touch /var/www/FPM/log/nginx/error.log \
    touch /var/www/FPM/log/nginx/access.log \

Instead, create error.log and access.log inside your src directory or if you would choose at src/log/nginx/ location for brevity. And mount these files in your docker compose file.

version: "3.8"

services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: FPM-app
    restart: always
    working_dir: /var/www/FPM
    volumes:
      - ../src:/var/www/FPM

  nginx:
    image: nginx:1.23.1-alpine
    container_name: FPM-nginx
    restart: always
    ports:
      - 8000:80
    volumes:
      - ../src:/var/www/FPM
      - ../src/nginx/logs/error.log:/var/www/FPM/log/nginx/error.log;
      - ../src/nginx/logs/access.log:/var/www/FPM/log/nginx/access.log;
      - ./nginx:/etc/nginx/conf.d
  • Related