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?

  • Deploy a Dockerized Symfony2 application on AWS Elastic Beanstalk
  • Referencing services with Docker Compose
  • After pulling a Docker from the repository, how to change the images files?
  • GC overhead limit exceeded in Docker when trying to build project by sbt
  • Why does Docker need a Union File System
  • What are the benefits of using Docker for persistent data
  • Installation of system tables failed! boot2docker tutum/mysql mount file volume on Mac OS
  • INTEL DEEP LEARNING SDK INSTALLATION FAILING
  • Getting the manifest list (fat manifest) from docker registry
  • Why is 'fig build' returning: AttributeError: 'WrappedSocket' object has no attribute 'recvfrom'?
  • Can't push or pull to/from docker registry on localhost
  • Insecure Docker registry on Centos7 refuses to ingest images, even with flag 'insecure-registries'
  • 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.