0

If i have inside my localhost a log folder at:

/var
  /logs
     apache.logs
     elasticsearch.logs
     etc... 

And i want to mount /var/logs directory of my host, into a path inside a Docker container, like /usr/var/logs/ , how do i do that within a dockerfile ? So each time a log file is updated, it would be accessible within the container too. Thank you

YoussHark
  • 558
  • 1
  • 9
  • 26

3 Answers3

2

You can not mount a volumn in Dockerfile

Because:

Dockerfile will build an image, image is independent on each machine host.

Image should be run everywhere on the same platform for example on linux platform it can be running on fedora, centos, ubuntu, redhat...etc

So you just mount volumn in to the container only. because container will be run on specify machine host.

Hope you understand it. Sorry for my bad English.

Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53
0

Try -v option of docker run command.

docker run -itd -v /var/logs:/usr/var/logs image-name

This will mount /var/logs directory of host on to /usr/var/logs directory of container.

Hope this helps.

Update:

To mount directory with source and dest in dockerfile make use of this hack (Not 100% sure though).

RUN --mount=target=/usr/var/logs,type=bind,source=/var/logs
mchawre
  • 10,744
  • 4
  • 35
  • 57
  • Thanks for your answer @mchawre. But how we do that in a dockerfile ? – YoussHark Jul 05 '19 at 04:21
  • `VOLUME` option in dockerfile might help, but there you can't provide source directory on host. Just try this hack mentioned in this answer. https://stackoverflow.com/questions/26050899/how-to-mount-host-volumes-into-docker-containers-in-dockerfile-during-build – mchawre Jul 05 '19 at 04:26
  • Updated my answer. – mchawre Jul 05 '19 at 04:27
  • which hack are you talking about ? i've red all the post but there isn't a solution with dockerfile unfortunately. And in my case i have to do it with a dockerfile due to some constraints – YoussHark Jul 05 '19 at 04:35
  • Can you try the command I mentioned in my answer. I'm not 100% sure. – mchawre Jul 05 '19 at 04:37
  • Oh sorry @mchawre, i did not see. Yes i will try this one as soon as i have my laptop afront of me, definitely . I really hope it works that would be awesome ! – YoussHark Jul 05 '19 at 04:51
  • sorry but it does not work. Finally i understood that a dockerfile is not meant to have actions on the host part – YoussHark Jul 08 '19 at 02:33
0

You can achieve it in two ways - https://docs.docker.com/storage/bind-mounts/

--mount

$ docker run -d -it --name devtest --mount type=bind,source=/var/logs,target=/usr/var/logs image:tag

-v

$ docker run -d -it --name devtest -v /var/logs:/usr/var/logs image:tag
Prakash Krishna
  • 1,239
  • 6
  • 12