How to persist alpine-nginx log files?

Viewed 4639

I want to persist the log files of the Docker Alphine Nginx image, so that they remain available between restarts. I have the following Dockerfile.

# ---------- Build stage
FROM node:9.11.1-alpine as build-stage

# Make the '/usr/src/app' folder the current working directory
WORKDIR /usr/src/app

# Copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./

# Install project dependencies
RUN npm install

# Copy project files and folders to the current working directory (i.e. '/usr/src/app' folder)
COPY . .

# Build app for production with minification
RUN npm run build

# ---------- Production stage
FROM nginx:1.13.12-alpine as production-stage

# Copy the build files from the previous stage
COPY --from=build-stage /usr/src/app/dist /usr/share/nginx/html

# By default the Alphine image automatically streams Nginx logs (access and error logs) to stdout and stderr
# by creating a symbolic link from stdout to /var/log/nginx/access.log and stderr to /var/log/nginx/error.log
# Lets remove these symbolic links so that we can setup a volume and persist the logs so that they are available
# between restarts
RUN unlink /var/log/nginx/access.log && \
    unlink /var/log/nginx/error.log &&

# Make port 80 accesible outside the container
EXPOSE 80

# Start nginx NOT as daemon, so that the container runs in the foreground so that we can see the requests
CMD ["nginx", "-g", "daemon off;"]

And my run command is:

set CUR_DIR=%cd%
docker run -it --mount type=bind,src=%CUR_DIR%\logs,dst=/var/log/nginx -p 8080:80 --rm --name my-app my-company/my-app

The command executes and then the following error is thrown:

nginx: [emerg] open() "/var/log/nginx/error.log" failed (2: No such file or directory)

The strange thing is that the error.log file is created on the host and it contains the error that is thrown.

OKay, I have now figured out that it is a Windows user rights problem. If I run the container on a Linux host it works. The bound log files on the host are owned by root, nginix worker process runs under the nginx user. What I find strange is that the user who's credentials I use on the Windows host is part of the administrators' group. Go figure. So far my own investigation I'm stuck again.

Any suggestions on how to solve this?

1 Answers

I could not find a way to solve the user rights issue on Windows. So I switched from the bind to volume method and it works.

set CUR_DIR=%cd%
docker volume create test_vol
docker run -it --mount type=volume,src=test_vol,dst=/var/log/nginx -p 8080:80 --rm --name my-app my-company/my-app

To list the contents of the volume use the following command.

docker run -it --rm -v logs:/logs alpine ls -l /logs

To view the contents of a file use the following command.

docker run -it --rm -v logs:/logs alpine cat /logs/access.log

To log into a shell that provides interactive access to the files.

docker run -ti -v logs:/logs alpine sh -c 'cd /logs; exec "${SHELL:-sh}'
Related