使用docker python API链接容器
我想使用docker python API(使用docker python API链接容器,python,dockerpy,Python,Dockerpy,我想使用docker python API(pip install docker py)创建一个容器,并将其链接到我使用docker compose创建的现有容器 使用命令行很容易: docker运行--链接现有容器:链接名称mycontainer:mytag 但是使用docker API我被卡住了。我想我必须使用docker.Client().create_container()方法,该方法采用-undocumented-parameterlinks=。(我强烈认为文件非常不完整…) 我试着
pip install docker py
)创建一个容器,并将其链接到我使用docker compose创建的现有容器
使用命令行很容易:
docker运行--链接现有容器:链接名称mycontainer:mytag
但是使用docker API我被卡住了。我想我必须使用docker.Client().create_container()
方法,该方法采用-undocumented-parameterlinks=
。(我强烈认为文件非常不完整…)
我试着阅读docker compose代码,这似乎使用了links=
参数,但我不知道怎么做
我最初的尝试没有成功:
client_obj.create_container(…,links=(('EXISTING_container','LINK_NAME'),)
。。。我认为docker compose代码就是这么做的
有人能帮我吗?
Docker远程API的Python库。它做所有的事情
docker命令可以,但在Python中–运行容器,管理
它们、拉/推图像等
:
但我在以下网站上找到了链接
:
所以我认为你应该:
from docker import Client
>>> cli = Client(base_url='tcp://127.0.0.1:2375')
>>> container = cli.create_container(
... image='busybox:latest',
... command='/bin/sleep 30')
>>> response = cli.start(container=container.get('Id'),links=[('EXISTING_CONTAINER', 'LINK_NAME')])
工作示例(DO)
我在DO上使用CoreOS:
/var/run/docker.sock
来自主机现有容器
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:12.04 bash
apt-get update;apt-get install python-pip -y;pip install docker-py
docker run -d --name EXISTING_CONTAINER busybox sh -c "while true; do sleep 1;done"
Python示例
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30')
response = cli.start(container=container.get('Id'),links=(('EXISTING_CONTAINER', 'LINK_NAME'))
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
# Note: 'bridge' is the default network
net_config = cli.create_networking_config(
{'bridge': self.docker_client.create_endpoint_config(
links=[('EXISTING_CONTAINER', 'LINK_NAME')]
)}
)
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30',
network_configuration=net_config
)
response = cli.start(container=container.get('Id'))
主机上的结果:
wp-coreos-512mb-ams2-01 ~ # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f58e661579d busybox "sh -c 'while true; d" 23 seconds ago Up 22 seconds EXISTING_CONTAINER
6f08dd3f5017 busybox:latest "/bin/sleep 30" 9 minutes ago Up 5 seconds condescending_brown
是的,docker py的网络文档严重缺乏——维护人员同意(举个全局别名的例子) 请注意,上面Valeriy的回答将创建一个遗留链接,如果您使用非默认网络(如docker compose创建的网络),则可能(在我的情况下)导致问题 在任何情况下,将参数添加到
Client.start
都是无效的
可以在UnitTest中找到执行此操作的新方法:
瓦莱里的例子如下所示:
Python示例
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30')
response = cli.start(container=container.get('Id'),links=(('EXISTING_CONTAINER', 'LINK_NAME'))
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
# Note: 'bridge' is the default network
net_config = cli.create_networking_config(
{'bridge': self.docker_client.create_endpoint_config(
links=[('EXISTING_CONTAINER', 'LINK_NAME')]
)}
)
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30',
network_configuration=net_config
)
response = cli.start(container=container.get('Id'))
我没有测试这个特定的代码,但这是我能够将新容器连接到现有容器的方式,而现有容器是由compose创建到网络“project_default”中的
您可能还需要查看更多信息和背景。以下是当前的工作方式
links=[('postgres-modeldb', 'modeldb'),('postgres-userdb', 'userdb'),('redis-workerdb', 'workerdb')]
host_config = client.create_host_config(
links=links
)
networking_config = client.create_networking_config({
'my-net': client.create_endpoint_config(
links=links
)
})
container = client.create_container(
image='josepainumkal/vwadaptor:jose_toolUI',
name=container_name,
host_config=host_config,
networking_config = networking_config
)
response = client.start(container=container.get('Id'))
不幸的是,我已经尝试过了,但它不起作用。你有没有试过这个,它对你有用?那么我可能又犯了一个错误。此解决方案已被弃用,python sdk不再允许在开始时进行配置..:该示例无法运行。如果你想让这个例子起作用,我很乐意投票支持,但遗憾的是,我不支持被破坏的伪代码
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
# Note: 'bridge' is the default network
net_config = cli.create_networking_config(
{'bridge': self.docker_client.create_endpoint_config(
links=[('EXISTING_CONTAINER', 'LINK_NAME')]
)}
)
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30',
network_configuration=net_config
)
response = cli.start(container=container.get('Id'))
links=[('postgres-modeldb', 'modeldb'),('postgres-userdb', 'userdb'),('redis-workerdb', 'workerdb')]
host_config = client.create_host_config(
links=links
)
networking_config = client.create_networking_config({
'my-net': client.create_endpoint_config(
links=links
)
})
container = client.create_container(
image='josepainumkal/vwadaptor:jose_toolUI',
name=container_name,
host_config=host_config,
networking_config = networking_config
)
response = client.start(container=container.get('Id'))