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?

  • Building the MongoDB Docker image
  • Docker: cant send data from logstash container to Kafka container
  • Default path to install Nexus Docker Container from AWS EC2
  • Kubernetes replication controller in CrashLoopBackOff state
  • Wrong order of running docker containers in convox/docker-compose
  • Docker run without setting WORKDIR doesn't work
  • docker nginx:latest image - content on docker host not changing
  • Docker machine is active, but shown as aborted in Virtual Box
  • What is /dev/mapper/vg_root-lv_root directory? Why my docker relies on this directory?
  • My Docker container will run a command from within the container, but not with ENTRYPOINT
  • Docker remote api don't restart after my computer restart
  • How many WordPress instances can i run on Google Compute engine in a single google cloud platform project.
  • 2 Solutions collect form web for “how to map a local folder as the volume the docker container or image?”

    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.

    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

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.