docker - 如何持久化 alpine-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" failed (2: No such file or directory)
奇怪的是,error.log 文件是在主机上创建的,它包含抛出的错误。
好的,我现在发现这是一个 Windows 用户权限问题。如果我在 Linux 主机上运行容器,它就可以工作。宿主机上绑定的日志文件归root所有,nginx工作进程在nginx用户下运行。我觉得奇怪的是,我在 Windows 主机上使用的凭据用户是管理员组的一部分。去搞清楚。到目前为止,我自己的调查再次陷入困境。
关于如何解决这个问题的任何建议?
解决方案
我找不到解决 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
登录到提供对文件的交互式访问的 shell。
docker run -ti -v logs:/logs alpine sh -c 'cd /logs; exec "${SHELL:-sh}'
推荐阅读
- azure-blob-storage - 直接在 blob 上创建 Excel 文件
- c++ - 从文件中删除一行而不创建新文件
- angular - 无法从单独的组件更新材料表上的数据
- vue.js - Vue 强制所有子组件渲染
- grails - ipRestrictions 在 Grails 3.3.10 中不起作用?
- javascript - 如何将整个文件导出到打字稿中的命名空间?
- c++ - static_cast
给出奇怪的字符 - r - 努力理解 R 中 window<-.ts 函数的行为
- excel - VBA Excel 365 - 需要打印活动单元格的页码
- json - NiFi 无法解析转换记录中的数据