How can I run containers detached and have them automatically removed when they exit?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)