docker 文件卷,如何把本地的文件,映射到容器的某个文件夹中?

version: "3"
services:
  nginx:
    container_name: nginx
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./fastapi_example.conf:/etc/nginx/conf.d/
    ports:
      - 80:80

fastapi_example.conf 是我的本地文件

我想放到容器的 /etc/nginx/conf.d/ 路径下,但是报错了

╰─➤  docker-compose up -d      
Recreating nginx ... error

ERROR: for nginx  Cannot start service nginx: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/home/pon/opt/docker-compose-public/nginx/fastapi_example.conf" to rootfs at "/etc/nginx/conf.d": mount /home/pon/opt/docker-compose-public/nginx/fastapi_example.conf:/etc/nginx/conf.d (via /proc/self/fd/6), flags: 0x5000: 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

ERROR: for nginx  Cannot start service nginx: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/home/pon/opt/docker-compose-public/nginx/fastapi_example.conf" to rootfs at "/etc/nginx/conf.d": mount /home/pon/opt/docker-compose-public/nginx/fastapi_example.conf:/etc/nginx/conf.d (via /proc/self/fd/6), flags: 0x5000: 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
ERROR: Encountered errors while bringing up the project.
阅读 3.3k
3 个回答

我们挂载的时候都是具体到文件。比如用本地的global.js文件替换容器里面的global.js文件

 volumes:
    - /opt/devops/config/web/global.js:/usr/share/nginx/html/static/global.js

和你上面的nginx.conf一样,要写全,文件对文件,路径对路径

用这种方式:

...
volumes:
    - type: bind
      source: ./fastapi_example.conf
      target: /etc/nginx/conf.d/fastapi_example.conf
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题