docker-compose run
has a flag --rm
that auto removes the container after run. I am wondering if theres an equivalent config with docker-compose.yml for a specific service, as one of which services i got in yml is a one off build process which should just output the compile file and disappear itself.

- 23,218
- 20
- 96
- 128

- 10,745
- 3
- 17
- 39
-
16**still not possible in 2018-11** :( don't waste time searching like me (at least not in the next few months ;)) – Maurice Müller Nov 23 '18 at 09:10
-
4I'm using ````docker-compose up -d && docker-compose rm -f```` for a similar scenario. – rebornx Feb 26 '19 at 11:10
-
@MauriceMüller Don't you know if there're any updates? – St.Antario Sep 29 '20 at 17:54
5 Answers
I haven't found any option to help you define this behavior in the docker-compose.yml
file and I think the explanation is the that it will break how some of the docker-compose ...
commands are supposed to work.
More on this up
/down
, start
/stop
thing:
docker-compose up
builds, (re)creates, starts, and attaches to containers for a service.
Since your images
are built and the containers
of your service have started, you can then use docker-compose stop
and docker-compose start
to start/stop your service. This is different from docker-compose down
which:
Stops containers and removes containers, networks, volumes, and images created by
up
.
Problem with what you are trying to do:
If you docker-compose up
and one of your containers finishes its task and gets (auto)removed, then you can't docker-compose stop
and docker-compose start
again. The removed container will not be there to start
it again.
You might want to take a look at:

- 23,218
- 20
- 96
- 128
-
1Ya thats confirmed my research accepted as answer. It be nice if theres an option since one of the container just there to do one off webpack production build. Guess need to write bash for it – Fan Cheung Nov 10 '17 at 14:53
-
4Check if `docker-compose rm` fits your needs... [https://docs.docker.com/compose/reference/rm/](https://docs.docker.com/compose/reference/rm/) – tgogos Nov 10 '17 at 14:54
Simply run docker-compose up && docker-compose rm -fsv
https://docs.docker.com/compose/reference/rm
Removes stopped service containers
--force , -f Don't ask to confirm removal --stop , -s Stop the containers, if required, before removing --volumes , -v Remove any anonymous volumes attached to containers

- 10,835
- 4
- 58
- 69

- 3,659
- 3
- 17
- 15
-
51This helps but I still wish `docker-compose up` had a `--rm` flag like `docker run` has. – clemlatz Sep 04 '20 at 12:50
-
1
-
6With this approacth you can't remove a container if you stop the it using `SIGTERM` (CTRL+C) – James Bond Jun 02 '22 at 10:00
-
1I don't get it, when is "docker-compose up" supposed to exit with 0 code to run the "rm" part? I'll probably stick to NPM "concurrently" coupled with 2 Docker commands for my simple use case (running a MongoDB and a RedisDb during app development) – Eric Burel Sep 08 '22 at 12:21
-
1This only removes the container if the process ends successfully. – Bitcoin Cash - ADA enthusiast Jan 05 '23 at 23:18
It's been quite some time since this question was posted, but I thought it would be informative to share something that worked for my case, in 2022 :) But keep in mind that this solution still does not remove old containers, as the original author intended to achieve.
docker-compose up --force-recreate -V
In my case, I have a small Redis cluster where I want the data to be completely erased after I stop the servers.
Only using --force-recreate
didn't do the trick, because the anonymous volume is still reused. That's where -V
comes in.

- 30,962
- 25
- 85
- 135

- 356
- 2
- 4
My solution to this was to create a little bash script that automatically removes containers afterwards.
If you're on macOS, you can put this script in usr/local/bin
. Assuming it's named dco
, you can then run chmod +x usr/local/bin/dco
to make it executable. On Windows, I have no idea how to get this working, but on Linux it should be similar.
#! /bin/bash
# check for -d, --detached
DETACHED=false
for (( i=1; i <= "$#"; i++ )); do
ARG="${!i}"
case "$ARG" in
-d|--detach)
DETACHED=true
break
;;
esac
done
if [[ $1 == "run" ]] && [[ $DETACHED == false ]]; then
docker-compose run --rm "${@:2}"
elif [[ $1 == "up" ]] && [[ $DETACHED == false ]]; then
docker-compose up "${@:2}"; docker-compose down
else
docker-compose "${@:1}"
fi
Edit: Updated the script so that detached mode will work normally, added break
to the loop suggested by artu-hnrq

- 273
- 3
- 10
-
Nice solution! Yet your `for` stills looping after turn `DETACHED` on. A little improvement could be done by [using `break`](https://stackoverflow.com/a/18488730/2989289) – artu-hnrq Jul 01 '21 at 06:28
I'm not sure I understand, docker-compose run --user is an option, and the docker-compose.yml supports the user key (http://docs.docker.com/compose/yml/#working95dir-entrypoint-user-hostname-domainname-mem95limit-privileged-restart-stdin95open-tty-cpu95shares).

- 39
- 2
-
docker-compose -rm up is not supported as far as I researched. Wonder if there's rm key available in docker-compose.yml. I looked through the document can't see to find it. – Fan Cheung Nov 10 '17 at 01:54