我有一个Dockerfile,使用了来自Nginx镜像的myuser,并想将日志挂载到指定位置。我使用docker-compose启动容器。我的要求是只能使用非root用户,且不能使用sudo。
我的Dockerfile使用myuser,创建的镜像标签为mynginx:v1
RUN addgroup mygroup
RUN adduser myuser --disabled-password
USER myuser
使用myuser镜像的mynginx Docker Compose无法工作
version: "2"
services:
nginx:
container_name: nginx
image: mynginx:v1
ports:
- "8888:80"
volumes:
- ./log/nginx:/var/log/nginx
尽管目录已经被挂载,但在主机上无法查看nginx日志文件access.log和error.log。
Docker日志如下:
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2021/04/09 12:46:08 [warn] 1#1: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021/04/09 12:46:08 [emerg] 1#1: mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
然而,如果我使用以root用户身份运行的官方nginx镜像执行相同的操作,则一切正常。
使用root用户的官方nginx镜像的工作docker compose
version: "2"
services:
nginx:
container_name: nginx
image: nginx
ports:
- "8888:80"
volumes:
- ./log/nginx:/var/log/nginx
尝试了各种选项,但到目前为止都没有成功。