Lubor
Lubor

Reputation: 999

how to map a local folder as the volume the docker container or image?

I am wondering if I can map the volume in the docker to another folder in my linux host. The reason why I want to do this is, if I don't misunderstand, the default mapping folder is in /var/lib/docker/... and I don't have the access to this folder. So I am thinking about changing that to a host's folder I have access (for example /tmp/) when I create the image. I'm now able to modify Dockerfile if this can be done before creating the image. Or must this be done after creating the image or after creating the container?

Upvotes: 0

Views: 10973

Answers (2)

Lubor
Lubor

Reputation: 999

I found this article which helps me to use a local directory as the volume in docker. https://docs.docker.com/engine/userguide/containers/dockervolumes/

Command I use while creating a new container:

docker run -d -P -name randomname -v /tmp/localfolder:/volumepath imageName

Upvotes: 3

BMitch
BMitch

Reputation: 263746

Docker doesn't have any tools I know of to map named or container volumes back to the host, though they are just sub directories under /var/lib/docker so writing your own tool wouldn't be impossible, but you'd need root access to run it. Note that with access to docker on the host, there are likely a lot of ways to access root privileges on the host. Creating a hard link to the target folder should be all that's needed if both source and target are on the same file system.

The docker way to access the named volume would be to create a disposable container to access your files. You can even create an additional host volume to export the data. E.g.

docker run -it --rm \
  -v test:/source -v `pwd`/data:/target \
  busybox /bin/sh -c "tar -cC /source . | tar -xC /target"'

Where "test" is the named volume you want to export/copy. You may need to also run a chown -R $uid /target in a container to change everything to your uid on the host.

Upvotes: 0

Related Questions