如何持久保存alpine-nginx日志文件?

时间:2018-11-07 17:08:16

标签: docker nginx

我想保留Docker Alphine Nginx映像的日志文件,以便它们在重启之间保持可用。我有以下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;"]

我的运行命令是:

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

执行该命令,然后引发以下错误:

nginx:[emerg] open()“ /var/log/nginx/error.log”失败(2:无此类文件或目录)

奇怪的是,error.log文件是在主机上创建的,它包含引发的错误。

好的,我现在发现这是Windows用户权限问题。如果我在Linux主机上运行容器,则它可以工作。主机上的绑定日志文件由root拥有,nginix worker进程在nginx用户下运行。我感到奇怪的是,我在Windows主机上使用的凭据用户属于管理员组。去搞清楚。到目前为止,我自己的调查再次陷入困境。

关于如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:0)

我找不到解决Windows上用户权限问题的方法。因此,我从“绑定”切换为“体积”方法,并且可以使用。

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

要列出该卷的内容,请使用以下命令。

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

要查看文件的内容,请使用以下命令。

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

登录到提供对文件的交互式访问的外壳。

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