使用主机中的目录挂载docker映像目录中存在的文件 [英] mount files present in a directory in docker image with the directory in the host

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

问题描述

我有一个Docker映像,其中目录 / opt / app / config 中存在少量配置文件,现在我想将此目录挂载到主机目录,因此可以从主机访问配置文件。



我的docker compose如下

 网站:
图片:网站:v1
container_name:网站
卷:
-./config:/opt/app/config
命令:[ tail,-f, / dev / null]
端口:
-5000:5000

如果我使用 docker run -it web:v1 bash 运行单个Web图像并执行 ls / opt / app / config 我可以看到配置文件



,但是使用 docker-compose up 创建容器,但文件未反映在主机路径中。



当我执行 ls ./config目录为空。预期的行为是docker映像中 / opt / app / config 中存在的文件应大声反映在我的主机 ./ config 目录。

解决方案

将主机目录装载到容器中时,主机目录的内容会遮盖容器的内容。 / p>

如果要访问主机上容器中存在的文件,可以按以下方式安装命名卷。

 服务:
网站:
图片:web:v1
container_name:网站
卷:
-配置:/ opt / app / config
命令:[ tail,-f, / dev / null]
端口:
-5000:5000
数量:
配置:

然后您可以找到使用 docker检查配置的配置卷路径,并从容器中查看内容。



如果要在主机上指定路径,则可以在撰写文件中指定命名卷的路径,如下所示。

 音量:
配置:
driver_opts:
o:绑定
设备:/ tmp / 2 ------ >主机


上的路径

I have a docker image in which there are few configuration files present in the directory /opt/app/config now i wanted to mount this directory to a host directory so that config files can be accessed from host.

my docker compose is a follows

  web:
    image: web:v1
    container_name: web
    volumes:
      - ./config:/opt/app/config
    command: ["tail","-f","/dev/null"]
    ports:
      - 5000:5000

if i run the individual web image using docker run -it web:v1 bash and do ls /opt/app/config i can see the config files

but using docker-compose upthe containers are getting created but the files are not being reflected in the host path.

when i do ls ./config the directory is empty. The expected behaviour is the files present in docker image at /opt/app/config shloud be reflected in my host ./config directory.

解决方案

When you mount a host directory into container, the contents of host directory shadow the contents of container.

If you want to access files present in container on host, you can mount a named volume as follows.

services:
  web:
    image: web:v1
    container_name: web
    volumes:
      - config:/opt/app/config
    command: ["tail","-f","/dev/null"]
    ports:
      - 5000:5000
volumes:
  config:

Then you can find the path of config volume using docker inspect config and see the contents from the container.

If you want to specify the path on host machine, you can specify the path of named volume in compose file as follows.

volumes:
  config:
    driver_opts:
      o: bind
      device: /tmp/2    ------> Path on host machine

这篇关于使用主机中的目录挂载docker映像目录中存在的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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