运行Docker容器进程而不必担心死容器问题的脚本
rundocker的Python项目详细描述
Rundocker
运行Docker容器进程的脚本
我们在这里解决的问题
由于docker 0.6,您实际上可以像正常进程一样运行docker container
` docker run --rm foobar `
按ctrl+c时,docker client会将int信号代理给docker容器,这样容器就会终止。但是,在许多情况下,Docker容器将无法正确关闭,甚至Docker客户端进程也将停止。例如,当您试图用一个使用过的端口运行一个容器时,run命令将失败,但是不知何故,docker容器不会被删除,它会导致以下run命令失败
` 2014/07/27 03:24:45 Error response from daemon: Cannot start container 08b25a41e30be78410a2556ffe01e720ff0d7bd512a53e8a44d7bceb8d3cf83e: Bind for 172.17.42.1:5050 failed: port is already allocated 2014/07/27 03:24:46 Error response from daemon: Conflict, The name foobar is already assigned to 08b25a41e30b. You have to delete (or rename) that container to be able to assign foobar to a container again. 2014/07/27 03:24:48 Error response from daemon: Conflict, The name foobar is already assigned to 08b25a41e30b. You have to delete (or rename) that container to be able to assign foobar to a container again. .... `
有一个[issue 7245](https://github.com/docker/docker/issues/7245)描述了这个问题。如果增加了新功能,我想我们可以放弃这个项目。
安装
您可以通过
` pip install rundocker `
因为它只有[一个python脚本文件](https://github.com/victorlin/rundocker/blob/master/rundocker/__main__.py),所以实际上可以将脚本复制到一个文件并使其可执行。
用法
使用RunDocker运行Docker,并使用您通常使用的名称
` sudo rundocker --rm--name="foobar" eggs/spam `
在许多情况下,即使Docker运行进程也无法正确删除容器
已终止。当你试图运行同名的容器时,
它会一直告诉你已经有一个同名的容器了。
这很烦人,而且会破坏自动部署。
为了解决这个问题,您可能需要添加--force-rm
争论。它将强制删除具有相同名称的现有容器。
它在RunDocker运行新容器之前运行。例如:
` sudo rundocker --rm--force-rm--name="foobar" eggs/spam `
工作原理
如果存在死箱,脚本将查看现有的DOCKER容器。 用同样的名字,它会为你移除它。但是,如果存在 容器仍在运行,将引发错误。
当脚本接收到int信号时,它将停止容器。