如何在docker-py中绑定卷?

Jas*_*sch 5 python docker dockerpy

我认为这在几个月前就可以解决了。常规命令行泊坞窗:

>> 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
Run Code Online (Sandbox Code Playgroud)

可以正常工作,但是我无法在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']))
Run Code Online (Sandbox Code Playgroud)

..总是告诉我,新的不存在。volumes-from应该如何使用docker-py完成?

tec*_*rch 5

原始答案已在 api 中弃用,不再有效。以下是使用创建主机配置命令的方法

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)
Run Code Online (Sandbox Code Playgroud)


jos*_*kal 5

以下是进行卷绑定的当前工作方式:

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'))
Run Code Online (Sandbox Code Playgroud)

  • 对于已经运行的命名卷,您将如何执行此操作? (2认同)