0

In my Dockerfile these were the contents,

FROM php:7.0-apache
COPY src/ /var/www/html/public
VOLUME src/ /var/www/html/public
EXPOSE 80    

You see, I have mounted /src to /var/www/html/public now I went to the container to find out the what all mounts have been made inside the container more specifically finding out /src /var/ww/html/public.

I searched it and found the command /proc/mounts can help me out. So I tried, results are

...
/dev/vda1 /src ext4 rw,relatime 0 0
/dev/vda1 /etc/resolv.conf ext4 rw,relatime 0 0
/dev/vda1 /etc/hostname ext4 rw,relatime 0 0
/dev/vda1 /etc/hosts ext4 rw,relatime 0 0
/dev/vda1 /var/www/html/public ext4 rw,relatime 0 0
...

This part of the results does contains things that I want to know. As very first line contains /src and the 5th line contains /var/www/html/public But how do I interpret the results. In other words, by looking the file contents how do I know that host /src mounted to container directory /var/www/html/public?

What I thought I would get the results like this.

/src /var/www/html/public but instead /dev/vda1 was interfering, So How do I understand this.

janu agrawal
  • 85
  • 1
  • 11
  • `/dev/vda1` is a virtual disk refering to the real disk. I think you accidentially created another mount from `/src`. – dan1st Sep 05 '20 at 13:57
  • hi @dan1st, I have highlighted the question more specifically. If possible please add more details. – janu agrawal Sep 05 '20 at 13:59
  • https://stackoverflow.com/q/30133664/10871900 explains how to test what is mounted to what. – dan1st Sep 05 '20 at 14:02
  • But this can't be happen when you are inside the container, let me know if I am wrong. – janu agrawal Sep 05 '20 at 14:05
  • No, the container does not know where the mounts go to. A computer does not know the physical location of hard drives, too. You could locate them outside of it and connect a cable. – dan1st Sep 05 '20 at 14:08
  • I think found something, just want to share: This command: `mount | grep ^/dev/ | grep -v /etc | awk '{print $3}'` returns the host volumes. In this way: `/src /var/www/html/public ` – janu agrawal Sep 05 '20 at 14:14

1 Answers1

0

A Dockerfile cannot declare any specific host directories. Instead, what you've done is told Docker to create two anonymous volumes on the directories ./src and /var/www/html/public. There's almost no reason to do this, and you should just delete that VOLUME line.

Mounting a host directory over your application's source code isn't necessarily a best practice: it means you're not running the code built into the image, and so the image might not actually run reproducibly in a different environment. If you need to, then you need a docker run -v option when you start the container. There's nothing you need in the Dockerfile to make this possible, and you can't mandate it.

There's no easy way to tell what volumes are mounted from inside a container, and Docker by design hides details of the host system like this (hence multiple different things all apparently being mounted from the same device). You can test the specific effect of editing a file in the host/container and seeing the corresponding change in the container/host. A comment also cited How do you list volumes in docker containers? which lists out several ways to test this with docker commands, from the host.

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