'`docker-compose run --rm` does not remove depends_on services

I have a docker-compose.yml file like this,

version: "3.9"

services:
  db-test:
    image: mariadb:10.6
    profiles:
      - test
  be:
    build:
      context: .
      dockerfile: test.Dockerfile
    depends_on:
      - db-test
    volumes:
      - gradle-cache:/home/gradle/.gradle
    profiles:
      - test

# volumes
volumes:
  gradle-cache:
    driver: local

This is for testing the image inside a specific environment. This is a one time task; so I use docker-compose run --profile test --rm be.

This runs perfectly fine.

My concern is that, after the run is over, the dependency service db-test is still running. be service is automatically deleted when using --rm option.

Is there a way to clean up everything? i.e. containers related to be and db-test services must be deleted after the run is complete. Like in case of docker-compose down.



Solution 1:[1]

use docker compose up -d and down instead of run

up/down is creating the full project and removes everything after that, even volumes if flag -v is used. run is more for quick&dirty starting a container.

See: https://docs.docker.com/compose/faq/#whats-the-difference-between-up-run-and-start

or

docker compose run be & docker compose rm -s -f

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1