Using docker compose for cicd workflow with auto stopping dependencies

I’d like to use docker compose in a gitlab cicd workflow – essentially I want to be able to do something like,

docker-compose run webapp manage test

The webapp service would have a dependency, say, on postgres. Postgres would automatically start in a container, tests would run, and then ideally, postgres container would shutdown as the run exits.

In other words, the service dependencies only run during the duration of the ‘run’ command.

Is this a workflow that is supported in some way? How do you support this kind of workflow? Is the only way around this to do a docker-compose down after the “run” completes?

I’m looking for a workflow that is least likely to leave orphan containers running…

down or stop. I don’t know other simple ways. If you run the containers in the foreground, docker-compose down will stop and remove containers. If you don’t want to remove them, you can use docker-compose stop instead.

If you need to run the containers in the background, you can use docker wait containername to wait until the main container stops and stop the other containers.