faradayio / cage

Develop and deploy complex Docker applications
http://cage.faraday.io
Apache License 2.0
307 stars 26 forks source link

Subcommand for `rm` #16

Closed emk closed 7 years ago

emk commented 7 years ago

We need some way to remove all containers (and possibly images) associated with a project. docker-compose rm has the following semantics:

$ docker-compose rm --help
Removes stopped service containers.

By default, anonymous volumes attached to containers will not be removed. You
can override this with `-v`. To list all volumes, use `docker volume ls`.

Any data which is not in a volume will be lost.

Usage: rm [options] [SERVICE...]

Options:
    -f, --force   Don't ask to confirm removal
    -v            Remove any anonymous volumes attached to containers
    -a, --all     Obsolete. Also remove one-off containers created by
                  docker-compose run

We could support this with a [<pods_or_services>...] argument, as described in #8. The --all flag is now included.

The semantics of this command are:

        if options.get('--all'):
            log.warn(
                '--all flag is obsolete. This is now the default behavior '
                'of `docker-compose rm`'
            )
        one_off = OneOffFilter.include

        all_containers = self.project.containers(
            service_names=options['SERVICE'], stopped=True, one_off=one_off
        )
        stopped_containers = [c for c in all_containers if not c.is_running]

So this is internally doing something like:

docker ps --all --latest \
             --filter label=com.docker.compose.project=$PROJECT \
             --format '{{.Names}}'

In other words, I think we can use the thin, obvious wrapper over docker-compose rm and get the right results, including reasonable semantics for a bare cage rm.