Search code examples
dockerubuntudocker-machinedocker-container

How to access files in host from a Docker Container?


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 ?


Solution

  • 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>