How to access files in host from a Docker Container?

Just Khaithang picture Just Khaithang · Mar 11, 2019 · Viewed 15.3k times · Source

I have a Docker Ubuntu bionic container on A Ubuntu server host. From the container I can see the hsot 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 ?

Answer

JShorthouse picture JShorthouse · Mar 11, 2019

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

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

for example:

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