如何在docker-py中绑定卷?

时间:2021-10-27 21:37:53

I think this used to work up to a few months ago. The regular commandline docker:

我认为这曾经是几个月前的工作。常规命令行docker:

>> docker run --name 'mycontainer' -d -v '/new' ubuntu /bin/bash -c 'touch /new/hello.txt'
>> docker run --volumes-from mycontainer ubuntu /bin/bash -c 'ls new'
>> hello.txt

works as expected but I cannot get this to work in docker-py:

按预期工作,但我不能让它在docker-py中工作:

from docker import Client #docker-py
import time

docker = Client(base_url='unix://var/run/docker.sock')
response1 = docker.create_container('ubuntu', detach=True, volumes=['/new'],
    command="/bin/bash -c 'touch /new/hello.txt'", name='mycontainer2')
docker.start(response1['Id'])
time.sleep(1)
response = docker.create_container('ubuntu', 
    command="/bin/bash -c 'ls new'", 
    volumes_from='mycontainer2')
docker.start(response['Id'])
time.sleep(1)
print(docker.logs(response['Id']))

..always tells me that new doesn't exist. How is volumes-from supposed to be done with docker-py?

..总是告诉我,新的不存在。如何使用docker-py来完成卷?

3 个解决方案

#1


3  

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'))

#2


3  

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

最初的答案已在api中弃用,不再有效。以下是使用create host config命令执行此操作的方法

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)

#3


2  

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

从docker api版本1.10开始,volume-from是start()而不是create()的参数

Available from docker-py release 0.3.2

可从docker-py版本0.3.2获得

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

拉引入更改的请求:https://github.com/dotcloud/docker-py/pull/200

#1


3  

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'))

#2


3  

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

最初的答案已在api中弃用,不再有效。以下是使用create host config命令执行此操作的方法

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)

#3


2  

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

从docker api版本1.10开始,volume-from是start()而不是create()的参数

Available from docker-py release 0.3.2

可从docker-py版本0.3.2获得

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

拉引入更改的请求:https://github.com/dotcloud/docker-py/pull/200