Search code examples
dockerdocker-compose

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.


Solution

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