如何运行容器分离,退出时会自动删除? [英] How can I run containers detached and have them automatically removed when they exit?

查看:368
本文介绍了如何运行容器分离,退出时会自动删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ docker run -d --rm image $为什么在Docker中有-d和-rm冲突参数? b $ b冲突选项:--rm和-d 

我有一些运行单元的容器/功能/集成测试。码头箱容器启动,运行,测试,然后停止。我把它们分开,因为我只关心结果,但是我也希望容器在容器退出后被移除。这将是一个很好的方法?

解决方案

目前(docker v1.1.1)不支持此功能。
- rm 功能的开发人员解释了在他的 PR#1589


目前仅当没有提供-d时才支持。自动删除通过docker run -d创建的容器是没有意义的。有两个原因为什么以这种方式实现:1)我们可能希望在删除容器之前检索某种退出状态或日志2)在当前架构中使服务器端运行困难。


好消息是有人已经打开了问题修复这个,所以你可以在那里讨论。



另外,一个解决方法并不是太复杂,你可以使用一个包装器脚本来运行你的容器如下:

  ID = $(docker run -d ubuntu sleep 3)
docker等待$ ID
docker rm $ ID


Why are -d and --rm conflicting arguments in docker?

$ docker run -d --rm image
Conflicting options: --rm and -d

I have a number of containers that run unit/functional/integration tests. The docker containers start, run, the tests, and then stop. I run them detached since I only care about the results, but I'd also like the containers to be removed after the container exits. What would be a good way to do this?

解决方案

Currently (docker v1.1.1) this functionality isn't supported. The developer of the --rm feature explains the reasons for that in his PR #1589:

It's currently supported only when -d isn't provided. It doesn't make sense to automatically remove a container created via docker run -d. There are two reasons why this is implemented this way: 1) we might want to retrieve some kind of exit status or logs before removing the container 2) making this run on the server side is difficult in the current architecture.

The good news are that someone already opened an issue to fix this, so you might follow the discussion there.

Also, a workaround isn't too complicated, you can run your containers using a wrapper script as follows:

ID=$(docker run -d ubuntu sleep 3)
docker wait $ID
docker rm $ID

这篇关于如何运行容器分离,退出时会自动删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆