Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WORKDIR as VOLUME

In my dockerfile, I have my WORKDIR and I want to have it as a VOLUME, so that on the host I have a directory in /var/lib/docker/volumes/ where is the same content as in the WORKDIR. How do I use the VOLUME Dockerfile command for this?

like image 910
HelloGriffin Avatar asked Nov 01 '25 09:11

HelloGriffin


1 Answers

While you can mount a volume over the WORKDIR that you were using when building your image, the volume isn't available at build time. Volumes are only available for a container, not while building an image.

You can COPY files into the image to represent the content that will exist in the volume once a container is running, and use those temporary files to complete the building of the image. However, those exact files would be inaccessible once a volume is mounted in that location.

To have a directory from the host machine mounted inside a container, you would pass a -v parameter (you can do multiple -v params for different directories or for individual files) to the docker run command that starts the container:

docker run -v /var/lib/docker/volumes:/full/path/inside/container your_image_name
like image 190
Karl Wilbur Avatar answered Nov 02 '25 23:11

Karl Wilbur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!