Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto remove container with docker-compose.yml

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.

like image 644
Fan Cheung Avatar asked Nov 09 '17 17:11

Fan Cheung


People also ask

Does docker compose delete containers?

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

How do you automatically delete a container 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.

Does docker compose RM remove volumes?

Removes stopped service containers. By default, anonymous volumes attached to containers are not removed.

How to stop the application containers using Docker-Compose?

Stop the application containers using docker-compose stop: Remove the application containers using docker-compose rm -f: Note: If you don’t specify -f in the above command, it will prompt you for Y/N before removing it.

Is there a way to enforce Docker-Compose to run in yml?

It's not part of the Dockerfile or docker-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.

What happens when you run Docker Compose down?

When you’re ready to tear it all down, simply run docker-compose down or hit the trash can on the Docker Dashboard for the entire app. The containers will stop and the network will be removed. By default, named volumes in your compose file are NOT removed when running docker-compose down.

How do I remove a docker container?

# docker-compose rm -v You can also remove a specific container by specifying the container name. The following will remove only the data container. # docker-compose rm -f data Going to remove data Removing data... done


1 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:

  • docker-compose: option to automaticaly remove container after run in docker-compose.yml
  • What is the difference between docker-compose up and docker-compose start?
like image 135
tgogos Avatar answered Sep 23 '22 15:09

tgogos