YoussHark
YoussHark

Reputation: 608

Dockerfile : How to mount host directory in container path?

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

Upvotes: 3

Views: 4554

Answers (3)

mchawre
mchawre

Reputation: 12258

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

Upvotes: 0

Thanh Nguyen Van
Thanh Nguyen Van

Reputation: 11772

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.

Upvotes: 6

Prakash Krishna
Prakash Krishna

Reputation: 1257

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

Upvotes: 0

Related Questions