大网数据广告活动
服务器运维问答

docker-compose: option to automaticaly remove container after run in docker-compose.yml

· 52CE 我爱测

The docker-compose run reference states that it has the --rm option to Remove container after run. I want to make this a default run behavior for some of services I specify in docker-compose.yml . So, the questions are : Can it s…

问题原文

原帖链接 · Ivan Kolmychek · CC BY-SA 3.0

The docker-compose run reference states that it has the --rm option to

Remove container after run.

I want to make this a default run behavior for some of services I specify in docker-compose.yml.

So, the questions are:

  1. Can it somehow be specified in docker-compose.yml?
  2. If it can, how can I do that?

(INB4 "Use bash aliases, Luke!": Of course I can enforce this outside of docker-compose.yml by setting some bash alias like alias docker-compose-run='docker-compose run --rm' but I'm interested in how can I enforce that exactly through docker-compose.yml, not in some extrnal way.)

已采纳解答

原帖链接 · Maurice Müller · CC BY-SA 4.0

TLDR: It's still not possible 2018-11; use docker-compose down or docker-compose run --rm

I want to give an updated answer to this question because it's almost 3 years later. This will save others some searching.

I had the same question and here are the workarounds I found (including the one from the question itself):

docker-compose down

which does the following:

Stops containers and removes containers, networks, volumes, and images created by up.

By default, the only things removed are:

- Containers for services defined in the Compose file
- Networks defined in the networks section of the Compose file
- The default network, if one is used

Networks and volumes defined as external are never removed.

Although you cannot declare it in docker-compose.yml it will safe you some hassle; especially with volumes and networks.

docker-compose run --rm

--rm - Remove container after run. Ignored in detached mode.

Runs a one-time command against a service. For example, the following command starts the web service and runs bash as its command.

docker-compose run web bash

[...] the command passed by run overrides the command defined in the service configuration.

[...] the command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag

docker-compose rm -f

-f, --force Don't ask to confirm removal

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。