Skip to content Skip to sidebar Skip to footer

How To Bind Volumes In Docker-py?

I think this used to work up to a few months ago. The regular commandline docker: >> docker run --name 'mycontainer' -d -v '/new' ubuntu /bin/bash -c 'touch /new/hello.txt' &

Solution 1:

Below is the current working way to do volume bindings:

volumes= ['/host_location']
volume_bindings = {
                    '/host_location': {
                        'bind': '/container_location',
                        'mode': 'rw',
                    },
}

host_config = client.create_host_config(
                    binds=volume_bindings
)

container = client.create_container(
                    image='josepainumkal/vwadaptor:jose_toolUI',
                    name=container_name,
                    volumes=volumes,
                    host_config=host_config,
) 
response = client.start(container=container.get('Id'))

Solution 2:

The original answer has been deprecated in the api and no longer works. Here is how you would do it by using the create host config commands

import docker

client = docker.from_env()

container = client.create_container(
    image='ubuntu',
    stdin_open=True,
    tty=True,
    command='/bin/sh',
    volumes=['/mnt/vol1', '/mnt/vol2'],

    host_config=client.create_host_config(binds={
        '/tmp': {
            'bind': '/mnt/vol2',
            'mode': 'rw',
        },
        '/etc': {
            'bind': '/mnt/vol1',
            'mode': 'ro',
        }
    })
)
client.start(container)

Solution 3:

Starting from docker api version 1.10 volumes-from is an argument to start() instead of create()

Available from docker-py release 0.3.2

Pull request which introduced the change: https://github.com/dotcloud/docker-py/pull/200

Post a Comment for "How To Bind Volumes In Docker-py?"