bash 如何在最小的 linux 安装上“docker run”一个 shell 会话并立即拆除容器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32806227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:38:39  来源:igfitidea点击:

how to "docker run" a shell session on a minimal linux install and immediately tear down the container?

linuxbashshellprocessdocker

提问by Chris Bedford

I just started using Docker, and I like it very much, but I have a clunky workflow that I'd like to streamline. When I'm iterating on my Dockerfile script I will often test things out after a build by launching a bash session, running some commands, finding out that such and such package didn't get installed correctly, then going back and tweaking my Dockerfile.

我刚开始使用 Docker,我非常喜欢它,但我有一个笨拙的工作流程,我想简化它。当我在我的 Dockerfile 脚本上进行迭代时,我通常会在构建后通过启动 bash 会话、运行一些命令、发现某个包没有正确安装,然后返回并调整我的 Dockerfile 来测试内容。

Let's say I have built my image and tagged it as buildfoo, I'd run it like this:

假设我已经构建了我的图像并将其标记为 buildfoo,我会像这样运行它:

      $>  docker run -t -i  buildfoo 

              ... enter some bash commands.. then  ^D to exit

Then I will have a container running that I have to clean up. Usually I just nuke everything like this:

然后我将运行一个容器,我必须清理它。通常我只是像这样核对一切:

docker rm --force `docker ps -qa`

This works OK for me.. However, I'd rather not have to manually remove the container.

这对我来说没问题.. 但是,我宁愿不必手动移除容器。

Any tips gratefully accepted !

感激地接受任何提示!



Some Additional Minor Details:

一些额外的小细节:

Running minimal centos 7 image and using bash as my shell.

运行最小的 centos 7 映像并使用 bash 作为我的 shell。

回答by spectre007

Please use -rmflag of docker run command. --rm=trueor just --rm.

请使用-rmdocker run 命令的标志。--rm=true或者只是--rm.

It automatically remove the container when it exits (incompatible with -d). Example:

它在退出时自动移除容器(与 不兼容-d)。例子:

docker run -i -t --rm=true centos /bin/bash

or

或者

docker run -i -t --rm centos /bin/bash

回答by ruby_slinger

Even though the above still works, the command below makes use of Docker's newer syntax

即使以上仍然有效,下面的命令使用了 Docker 的新语法

docker container run -it --rm centos bash

回答by VonC

I use the alias dr

我使用别名dr

alias dr='docker run -it --rm'

That gives you:

这给了你:

dr myimage
ls
...
exit

No more container running.

不再运行容器。