如何从 Docker 容器访问主机中的文件? [英] How to access files in host from a Docker Container?

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

问题描述

我在 Ubuntu 服务器主机上有一个 Docker Ubuntu 仿生容器.从容器中,我可以看到主机驱动器安装为/etc/hosts,它不是目录.尝试在不同的位置卸载和重新安装,但抛出 permission denied 错误,当我以 root 身份尝试时会发生这种情况.那么如何访问主机系统的内容?

I have a Docker Ubuntu bionic container on A Ubuntu server host. From the container I can see the host drive is mounted as /etc/hosts which is not a directory. Tried unmounting and remounting on a different location but throws permission denied error, this happens when I am trying as root. So How do you access the contents of your host system ?

推荐答案

首先,etc/hosts 是一个存在于所有 linux 系统上的网络文件,它与驱动器或 docker 无关.

Firstly, etc/hosts is a networking file present on all linux systems, it is not related to drives or docker.

其次,如果您想访问 Docker 容器内的部分主机文件系统,您需要使用 volumes.在 docker run 命令中使用 -v 标志,您可以指定主机上的目录以安装到容器中,格式为:

Secondly, if you want to access part of the host filesystem inside a Docker container you need to use volumes. Using the -v flag in a docker run command you can specify a directory on the host to mount into the container, in the format:

-v /path/on/host:/path/inside/container

例如:

docker run -v /path/on/host:/path/inside/container <image_name>

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

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