【问题标题】:/var/log/nginx not present in docker container and directory fail to be created by Dockerfile/var/log/nginx 不存在于 Docker 容器中,并且 Dockerfile 无法创建目录
【发布时间】:2020-07-10 10:40:21
【问题描述】:

在 Dockerfile 中,RUN mkdir -p /var/log/nginx 紧跟在 FROM 之后,但由于某种原因,它没有在容器中创建,如下所示

root@c6d43f610a51:/etc/nginx# cd /var/log
root@c6d43f610a51:/var/log# ls

这是容器上的nginx 配置。它明确指出nginx 日志将出现在/var/log/nginx 中,但这并没有发生。

root@c6d43f610a51:/var/log# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes 1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}
daemon off;

这是我完整的 Dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.5
RUN mkdir -p /var/log/nginx
RUN pip install pipenv python-dotenv requests jira  \
        && ln -sf /dev/stdout /var/log/nginx/access.log \
        && ln -sf /dev/stderr /var/log/nginx/error.log
EXPOSE 80
WORKDIR /app
CMD [ "pipenv", "run","flask", "run"]

仅供参考:nginx 网络服务器正在按预期工作,因此应该会填充日志。

【问题讨论】:

    标签: linux docker nginx


    【解决方案1】:

    NGINX 服务器进程以用户nginx 运行(参见nginx.conf 中的第一行)。

    您以 root 身份运行 mkdir,这将成为 /var/log/nginx 的所有者。

    您需要授予用户 nginx 对文件夹 /var/log/nginx 的写访问权限。以最简单的形式,您可以为所有用户授予写入权限:

    RUN chmod -R a+w /var/log/nginx
    

    【讨论】:

    • 好点,但是如果目录已经在容器中创建,为什么我做ls /var/log时它不显示?实际上,当我这样做时,docker-build 说 RUN not found 因为它在容器中看不到/var/log/nginx。
    • 对我来说它确实显示了它。我的第一个猜测是您正在运行旧图像。确保使用相同的镜像名称来构建和运行容器。
    • 是的,现在它已经创建好了,但是即使网络服务器正在运行,它里面也没有 nginx 日志
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2015-10-21
    • 2013-11-06
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多