0

I am running a docker container remotely using pydocker library. I need to mount a local directory in the running container. The documentation does mention the way to create a volume using APIs. However, I am unable to figure out how to use the volume while creating the container.

I am following the documentation at https://docker-py.readthedocs.io/en/stable/api.html#module-docker.api.container

Pankaj Garg
  • 893
  • 2
  • 8
  • 15
  • https://docs.docker.com/engine/admin/volumes/volumes/#initial-set-up This page explains that it is possible using docker cli. – Pankaj Garg Sep 30 '17 at 20:46

2 Answers2

0

The simple answer is you can't. Docker will not mount directory from your system to a remote server. You need to have the data available on the machine.

The only other way around is to put this into the image itself. Then you can initiate the build locally and setting the build context to your local directory. This will send the complete directory context to the remote docker server and you can then copy it to the image inside the Dockerfile using COPY/ADD

This works for VMs as such because a shared folder is inserted inside the VM from the host and it seems as the host folders work with docker remote volume mounts in VM

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • I see there is an option to use volume-drivers which can help mounting the remote directory using sshfs. I am trying to understand the same. However its taking me some time. (Other drivers: https://docs.docker.com/engine/extend/legacy_plugins/#volume-plugins) – Pankaj Garg Sep 30 '17 at 20:35
  • These plugins needs to be installed on server first and you can't do anything directly from client – Tarun Lalwani Oct 01 '17 at 04:44
0

Following worked for me:

volume = cli.create_volume(name='<volume-name>',
                          driver='vieux/sshfs',
                          driver_opts={'sshcmd': '<username@hostname>:<path>', 'password':'<password>'},
                          labels={})

container = cli.create_container(
            container_url,  
            '<run-cmd>',   
            detach=True,    
            volumes=['<mnt_dir>'],
            host_config=self.dclient.create_host_config(
                            binds={ \       
                                '<volume-name>': {\
                                    'bind': '<mnt_dir>', \ 
                                    'mode': 'ro', \ 
                                    'src': '<volume>', \ 
                                    'volume-driver': 'vieux/sshfs', \
                                    'target' :'/app', \
                                    'volume-opt': { \
                                        'sshcmd': 'username@hostname', \
                                        'password': <passwd> \
                                    } \             
                                } \             
                            },              
                            port_bindings=port_map))
Pankaj Garg
  • 893
  • 2
  • 8
  • 15