Docker - 解决创建 nginx 容器尝试挂载 nginx.conf 文件时报错: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory:

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Docker - 解决创建 nginx 容器尝试挂载 nginx.conf 文件时报错: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory:

背景


在自己的服务器上想通过 nginx 镜像创建容器,并挂载镜像自带的 nginx.conf 文件

docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx

 

但是报错了

[root@poloyy ~]# docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx
e0e4b40446a64927603b85854c3a6472b2dfa5681fcbfa0e170c16b15e5c8fdd
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
[root@poloyy ~]# client_loop: send disconnect: Broken pipe


提取关键错误信息

mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)?

将“/root/nginx.conf”挂载到“/etc/nginx/nginx.conf”的rootfs导致:通过procfd挂载:不是目录:未知:您是否试图将目录挂载到文件上(反之亦然)

 

根因


  • 不支持直接挂载文件,只能挂载文件夹
  • 想要挂载文件,必须宿主机也要有对应的同名文件

 

解决方法


  • 可以先不挂载 nginx.conf
  • 先从容器中复制 nginx.conf 出来
  • 然后可以自行修改 nginx.conf,自定义配置项
  • 创建正式使用的 nginx 容器

 

从 test 容器中复制 nginx.conf 出来

当然也可以去网上随便找个 nginx.conf,最重要的是宿主机要有个 nginx.conf

docker run --name test -d nginx  

docker cp test:/etc/nginx/nginx.conf /data/

 

创建正式的 nginx 容器,挂载 nginx.conf 文件

可以赋予权限

docker run --privileged -it -p 80:80 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
-v /data/nginx/html:/usr/share/nginx/html:rw \
-v /data/nginx/logs:/var/log/nginx -d nginx
相关文章
|
4月前
|
应用服务中间件 nginx Docker
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷(1)
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷(1)
|
17天前
|
Ubuntu 应用服务中间件 nginx
Docker 解析:如何将 Nginx 容器化并用作代理
Docker 解析:如何将 Nginx 容器化并用作代理
29 0
|
4月前
|
应用服务中间件 nginx Docker
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷(3)
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷(3)
|
4月前
|
应用服务中间件 nginx Docker
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷(2)
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷(2)
|
4月前
|
应用服务中间件 nginx Docker
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷
Docker部署Nginx以及挂载数据卷(代码详细展示)_nginx 挂载大文件卷
|
4月前
|
前端开发 应用服务中间件 nginx
前端破圈使用Docker Nginx容器部署项目🏴‍☠️
前端破圈使用Docker Nginx容器部署项目🏴‍☠️
209 0
|
10月前
|
应用服务中间件 nginx Docker
nginx反向代理踩坑(容器方式)
nginx反向代理踩坑(容器方式)
252 0
|
4月前
|
Ubuntu Cloud Native Unix
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker 错误
在使用WSL(Ubuntu 18.04.6)时,初学者遇到运行Docker时的错误:“Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?”。解决问题的过程包括:首先尝试通过`sudo apt-get install daemon`安装daemon,然后使用`sudo service docker start`启动Docker。通过`sudo service docker status`确认Docker已启动,并成功运行`docker
|
10月前
|
Unix Docker 容器
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker
|
4月前
|
应用服务中间件 nginx Docker
使用Docker构建本地Nginx容器及配置
使用Docker构建本地Nginx容器及配置
99 2
下一篇
云函数