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 somehow be specified in
docker-compose.yml
? - 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.)
TLDR: It's still not possible 2018-11; use
docker-compose down
ordocker-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:
Although you cannot declare it in
docker-compose.yml
it will safe you some hassle; especially with volumes and networks.docker-compose run --rm
docker-compose rm -f
It's not part of the
Dockerfile
ordocker-compose.yml
spec, it is only a cli option for the run command, so the answer is no. You will need to rely on something external for enforcing.If you got some build tool for your project it is usually best to wrap docker-compose tasks with that. For example our gradle projects provide docker related tasks that set some default options like
--rm
for run tasks.