Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

Why are -d and --rm conflicting arguments in Docker?

$ docker run -d --rm image Conflicting options: --rm and -d 

I have a number of containers that run unit/functional/integration tests. The Docker containers start, run the tests, and then stop. I run them detached since I only care about the results, but I'd also like the containers to be removed after the container exits. What would be a good way to do this?

like image 269
jchysk Avatar asked Jul 15 '14 17:07

jchysk


People also ask

How do you run a container so that it automatically remove when it exits?

If you know when you're creating a container that you won't want to keep it around once you're done, you can run docker run --rm to automatically delete it when it exits: Run and Remove: docker run --rm image_name.

How do you run a container in detach mode?

To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the --rm option.

How do you stop a container automatically?

According to this answer, adding the -t flag will prevent the container from exiting when running in the background. You can then use docker exec -i -t <image> /bin/bash to get into a shell prompt.

What happens to the docker container when the process it is running exits?

When this happens, the program will stop, and the container will exit. The container has been stopped using docker stop : You can manually stop a container using the docker stop command. The Docker daemon has restarted, and it terminated and restarted the container: Docker can restart containers if you need it to.


2 Answers

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 
like image 173
Leonid Mirsky Avatar answered Oct 17 '22 01:10

Leonid Mirsky


These options no longer conflict as of Docker version 1.13.0

There was a pull request that moves the --rm option daemon-side and allows for running containers detached with the removal option: https://github.com/docker/docker/pull/20848

like image 20
jchysk Avatar answered Oct 17 '22 00:10

jchysk