加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

python-3.x-通过具有特定ulimit的python API运行docker容器

发布时间:2020-12-16 03:21:44 所属栏目:安全 来源:网络整理
导读:现在,我想按照以下简单的文档/教程创建一个容器,以在图像内运行虚拟命令: https://docker-py.readthedocs.io/en/stable/containers.html#container-objects import dockerclient = docker.from_env()client.containers.run(shm_size='1g',ulimits=[docker.t

现在,我想按照以下简单的文档/教程创建一个容器,以在图像内运行虚拟命令:
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 (most recent call
last) in ()
—-> 1 client.containers.run(shm_size=’1g’,ulimits=[docker.types.Ulimit(name=’memlock’,
docker.types.Ulimit(name=’stack’,
image=’ubuntu:16.04′,command=’date’)

~/anaconda3/lib/python3.7/site-packages/docker/models/containers.py in
run(self,image,command,stdout,stderr,remove,**kwargs)
812 if exit_status != 0:
813 raise ContainerError(
–> 814 container,exit_status,out
815 )
816

尽管以下命令可以完美运行:

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

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

最佳答案
您的python和shell命令并不相同:在shell命令中,您指定了软限制,而在python中,您指定了硬限制. --ulimit command flag的参数语法为:

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

并且文档说明:

Note: If you do not provide a hard limit,the soft limit will be used for both values. If no ulimits are set,they will be inherited from the default ulimits set on the daemon.

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

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

这听起来像python documentation的缺点,它仅表示软参数和硬参数都是可选参数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读