通过具有特定ulimit的python API运行docker容器

问题描述 投票:3回答:1

现在我想创建一个容器,在这个简单的文档/教程之后在图像中运行一个虚拟命令:https://docker-py.readthedocs.io/en/stable/containers.html#container-objects

import docker
client = docker.from_env()
client.containers.run(shm_size='1g', ulimits=[docker.types.Ulimit(name='memlock', hard=-1), docker.types.Ulimit(name='stack', hard=67108864)], image='ubuntu:16.04', auto_remove=True,  command='date')

结果如下:

-------------------------------------------------- ------------------------- ContainerError Traceback(最近一次调用last)in()----> 1 client.containers.run(shm_size = '1g',ulimits = [docker.types.Ulimit(name ='memlock',hard = -1),docker.types.Ulimit(name ='stack',hard = 67108864)],image ='ubuntu:16.04' ,auto_remove = True,command ='date')

〜/ anaconda3 / lib / python3.7 / site-packages / docker / models / containers.py运行中(self,image,command,stdout,stderr,remove,** kwargs)812如果exit_status!= 0:813引发ContainerError ( - > 814容器,exit_status,命令,图像,输出815)816

虽然以下命令完美地工作:

docker run --shm-size=1g  --ulimit memlock=-1  --ulimit stack=67108864 --rm -t ubuntu:16.04 "date"

我使用的选项组合有什么问题?

python-3.x docker
1个回答
2
投票

你的python和shell命令不相同:在shell命令中,你指定了soft限制,在python中你指定了hard限制。 --ulimit command flag参数的语法是:

<type>=<soft limit>[:<hard limit>]

文档解释说:

注意:如果未提供硬限制,则软限制将用于两个值。如果未设置ulimits,则它们将从守护程序上设置的缺省ulimits继承。

为了获得相同的行为,我会尝试将python ulimit声明更改为

docker.types.Ulimit(name='stack', soft=67108864, hard=67108864)]

这听起来像是python documentation的一个缺点,它只说softhard都是可选参数。

© www.soinside.com 2019 - 2024. All rights reserved.