访问Docker主机中存在的文件结构 [英] Access file structure present in host with Docker

查看:63
本文介绍了访问Docker主机中存在的文件结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我希望我的docker映像直接从系统中存在的文件夹中获取数据,那是可能的。



如果是,那我该怎么办。



谢谢。

解决方案

通常的做法是使用

  docker run -d \ 
-it \
--name devtest \
--mount type = bind,source = $(pwd) / target,target = / app \
nginx:latest

这样,您的容器位于 / app 内,可以访问您的主机路径(此处 $ pwd / target



在docker-com中姿势文件,请参阅 docker构成音量合成器 x:


此示例显示:




  • 命名卷( mydata )被Web服务使用

  • 和为单个服务定义的 bind挂载(<$ c $下的第一个路径) c> db 服务卷)




 版本: 3.2 
服务:
网址:
图片:nginx:alpine
量:
-类型:量
来源:mydata
目标:/ data
量:
nocopy:真
-类型:绑定
源:./static
目标:/ opt / app / static

db:
图片:postgres:最新的
卷:
- /var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock
- dbdata:/ var / lib / postgresql / data

卷:
mydata:
dbdata:

在您的情况下,使用:

 - / path / to / local / folder / on / host:/ path / internal / your / container 


If i want my docker image to directly take data from a folder which is present in my system, is that possible.

If yes then how can I do it.

Thanks.

解决方案

The usual practice is to use docker volume (bind mount) at runtime.

docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  nginx:latest

That way, your container, inside /app, has access to your host path (here $pwd/target)

In a docker-compose file, see docker compose volume syntax:

This example shows:

  • a named volume (mydata) being used by the web service,
  • and a bind mount defined for a single service (first path under db service volumes).

version: "3.2"
services:
  web:
    image: nginx:alpine
    volumes:
      - type: volume
        source: mydata
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: ./static
        target: /opt/app/static

  db:
    image: postgres:latest
    volumes:
      - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
      - "dbdata:/var/lib/postgresql/data"

volumes:
  mydata:
  dbdata:

In your case, use:

- "/path/to/local/folder/on/host:/path/within/your/container"

这篇关于访问Docker主机中存在的文件结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆