3

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

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

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 

2 Comments

Hi @Prakash Krishna , how do we do that in a dockerfile ?
The use of —mount is discouraged. —volume should be used in all cases
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 

7 Comments

Thanks for your answer @mchawre. But how we do that in a dockerfile ?
VOLUME option in dockerfile might help, but there you can't provide source directory on host. Just try this hack mentioned in this answer. stackoverflow.com/questions/26050899/…
Updated my answer.
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
Can you try the command I mentioned in my answer. I'm not 100% sure.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.