41

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 ?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Just Khaithang
  • 1,217
  • 1
  • 14
  • 24

4 Answers4

59

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>
JShorthouse
  • 1,450
  • 13
  • 28
  • 3
    It should be made clear here that once a container is created, you cannot go and change the files in a share (-v) directory. The container takes a snapshot of what's happening and you cannot rely on changing a file on the host will translate into the docker container. Docker containers are expected to RUN modify files, then exit. If you have a web server, for example and it used a volume share into a nginx docker, you must stop start the container to reliably update the files into the container. Yes I know. total Crap. – James Gardiner Mar 09 '21 at 00:20
  • 8
    @JamesGardiner this isn't true, docker access the files directly on the host. There is no separate copy or snapshotting. Any filesystem changes you make on the host will be immediately visible inside the container and vice versa. It sounds like you're running into a caching problem by trying to swap files out from under nginx while it has them loaded into memory, which would be an issue even on bare metal. I imagine simply restarting nginx inside the container would also solve the problem. – JShorthouse Oct 20 '21 at 17:44
  • This syntax uses [bind mounts](https://docs.docker.com/storage/bind-mounts/), not [volumes](https://docs.docker.com/storage/volumes/). – SenhorLucas Dec 20 '22 at 10:34
13

Example. container id: 32162f4ebeb0

#HOST BASH SHELL

docker cp 32162f4ebeb0:/dir_inside_container/image1.jpg /dir_inside_host/image1.jpg


docker cp /dir_inside_host/image1.jpg  32162f4ebeb0:/dir_inside_container/image1.jpg 


quine9997
  • 685
  • 7
  • 13
3

Docker directly manages the /etc/hosts files in containers. You can't bind-mount a file there.

Hand-maintaining mappings of host names to IP addresses in multiple places can be tricky to keep up to date. Consider running a DNS server such as BIND or dnsmasq, or using a hosted service like Amazon's Route 53, or a service-discovery system like Consul (which incidentally provides a DNS interface).

If you really need to add entries to a container's /etc/hosts file, the docker run --add-host option or Docker Compose extra_hosts: setting will do it.

As a general rule, a container can't access the host's filesystem, except to the extent that the docker run -v option maps specific directories into a container. Also as a general rule you can't directly change mount points in a container; stop, delete, and recreate it with different -v options.

David Maze
  • 130,717
  • 29
  • 175
  • 215
3

run this command for linking local folder to docker container

docker run -it -v "$(pwd)":/src  centos

pwd: present working directroy(we can use any directory) and

src: we linking pwd with src

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '22 at 09:27
  • Can confirm. It works. – earth2jason Mar 31 '23 at 07:10