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

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

问题描述

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

如果是,那我该怎么做.

谢谢.

解决方案

通常的做法是使用

docker run -d -它 --名称开发测试--mount type=bind,source="$(pwd)"/target,target=/app 
ginx:最新的

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

在 docker-compose 文件中,请参阅 docker compose volume syntax:

<块引用>

这个例子显示:

  • Web 服务正在使用的命名卷 (mydata),
  • 以及为单个服务定义的绑定挂载(db 服务卷下的第一个路径).

版本:3.2"服务:网络:图片:nginx:高山卷:- 类型:体积来源:我的数据目标:/数据体积:无复制:真- 类型:绑定来源:./静态目标:/opt/app/staticD b:图片:postgres:最新卷:-/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"- dbdata:/var/lib/postgresql/data"卷:我的数据:数据库数据:

在你的情况下,使用:

-/path/to/local/folder/on/host:/path/within/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天全站免登陆