How to mount directories from a docker-machine to my local host

0 votes
How can i mount a directory from a docker-machine to my local host machine?
Jul 9, 2019 in Docker by Ruby
2,239 views

1 answer to this question.

0 votes

You could do this using the docker-machine mount command.

Using this command,you can mount directories from a machine to your local host, using sshfs.

The notation is machinename:/path/to/dir for the argument; you can also supply an alternative mount point (default is the same dir path).

consider an example:

$ mkdir foo
$ docker-machine ssh dev mkdir foo
$ docker-machine mount dev:/home/docker/foo foo
$ touch foo/bar
$ docker-machine ssh dev ls foo
bar

Now you can use the directory on the machine, for mounting into containers. Any changes done in the local directory, is reflected in the machine too.

$ eval $(docker-machine env dev)
$ docker run -v /home/docker/foo:/tmp/foo busybox ls /tmp/foo
bar
$ touch foo/baz
$ docker run -v /home/docker/foo:/tmp/foo busybox ls /tmp/foo
bar
baz

The files are actually being transferred using sftp (over an ssh connection).

answered Jul 9, 2019 by Sirajul
• 59,030 points
But, when we stop the docker machine with the command "docker-machine stop <machine-name>" why it erases information from the mount points. You cannot see the information even from the HOST level.

Please update how will i do this using docker compose file? below is my docker-compose file. i wanted to mount /tmp folder of docker container to C:/<user>/hostFolder of my windows host machine so that what ever file written to /tmp folder will be available to hostFolder of host machine automatically.

version: '3.3'

networks:
   oracle_net:
    external:
      name: oracle_net
      
volumes:
   oravolume10:
    external:
      name: oravolume10
   
      
services:
    oracle_xe:
        image: redbdocker5_configured
        build:
            context: '.\oracle docker'
        hostname: oraclehost
        networks:
            oracle_net:
                aliases:
                    - oraclexe
        ports:
            - "1521:1521"
        volumes:
            - type: volume
              source: oravolume10
              target: /opt/oracle
              
    ruleeditor:
        depends_on:
            - oracle_xe
        image: redocker7_configured
        hostname: ruleeditor6
        container_name: ruleEditor3
        build:
            context: '.\REDocker'
        networks:
            oracle_net:
                aliases:
                    - oraclexe
        ports:
             - "9080:9080"
             - "9450:9450"
        links:
            - oracle_xe 

below is my docker compose file. I wanted to mount "/tmp" folder of docker container to "C:/<user>/hostfolder" of my windows host machine so that any file written to /tmp folder will be automatically available to "hostfolder" of my host machine.

version: '3.3'

networks:
   oracle_net:
    external:
      name: oracle_net
      
volumes:
   oravolume10:
    external:
      name: oravolume10
   
      
services:
    oracle_xe:
        image: redbdocker5_configured
        build:
            context: '.\oracle docker'
        hostname: oraclehost
        networks:
            oracle_net:
                aliases:
                    - oraclexe
        ports:
            - "1521:1521"
        volumes:
            - type: volume
              source: oravolume10
              target: /opt/oracle
              
    ruleeditor:
        depends_on:
            - oracle_xe
        image: redocker7_configured
        hostname: ruleeditor6
        container_name: ruleEditor3
        build:
            context: '.\REDocker'
        networks:
            oracle_net:
                aliases:
                    - oraclexe
        ports:
             - "9080:9080"
             - "9450:9450"
        links:
            - oracle_xe

Hi@prassu,

You have written the code properly. In volume, give the source and target folder properly. Also, check the indentation of your code. If you get any error, then paste the error here.

Hi, I have changed the volume to the docker compose file as follows.

volumes:
            - type: volume
              source: revolume
              target: /tmp

Now when I inspect revolume it shows below

"CreatedAt": "2020-10-27T03:46:37Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/revolume/_data",
        "Name": "revolume",
        "Options": {},
        "Scope": "local

Now how will I go and check the contents of "/var/lib/docker/volumes/revolume/_data". moreover, I want the files to be copied from /tmp to revolume folder so that files from /tmp folder will be copied to volume.

Hi@prassu,

You have already mounted your folder. So check inside the _data folder. You can see the same data as /tmp.

Related Questions In Docker

+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,220 points

edited Oct 16, 2018 by lina 21,098 views
+1 vote
2 answers

How do I copy a file from docker container to host?

Here is a way which you can ...READ MORE

answered Aug 27, 2018 in Docker by Damon Salvatore
• 5,990 points
24,397 views
+1 vote
1 answer

How to mount a host directory in a Docker container?

Hi@akhtar, You can use the -v option in ...READ MORE

answered Dec 23, 2020 in Docker by MD
• 95,140 points
178 views
0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 10,550 points
1,826 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,100 points
1,103 views
0 votes
2 answers
+1 vote
2 answers