简体   繁体   English

访问Docker主机中存在的文件结构

[英]Access file structure present in host with Docker

If i want my docker image to directly take data from a folder which is present in my system, is that possible. 如果我想让我的Docker映像直接从系统中存在的文件夹中获取数据,那是可能的。

If yes then how can I do it. 如果是,那我该怎么办。

Thanks. 谢谢。

The usual practice is to use docker volume ( bind mount ) at runtime. 通常的做法是在运行时使用docker volumebind mount )。

体积

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 ) 这样,您的/app内的容器就可以访问您的主机路径(此处$pwd/target

In a docker-compose file, see docker compose volume synta x: docker -compose文件中,请参阅docker compose volume synta x:

This example shows: 此示例显示:

  • a named volume ( mydata ) being used by the web service, Web服务正在使用的命名卷( mydata ),
  • and a bind mount defined for a single service (first path under db service volumes) . 为单个服务( db服务卷下的第一个路径)定义绑定安装
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"

If you use docker-compose you can define volumes to share folders from your system. 如果使用docker-compose,则可以定义volumes以共享系统中的文件夹。 In example bellow the mysql will use ./data/db/mysql folder to store/read data (because as default it uses /var/lib/mysql in linux). 在下面的示例中,mysql将使用./data/db/mysql文件夹来存储/读取数据(因为默认情况下,它在Linux中使用/var/lib/mysql )。

Also you must to be sure that provided volume has correct permissions and docker have read/write access to it. 另外,您还必须确保所提供的卷具有正确的权限,并且Docker对其具有读/写访问权限。

version: "3"

services:    
    nginx:
        ......

    php:
        ........

    mysql:
        .........
        volumes:
            - "./data/db/mysql:/var/lib/mysql"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM