Search code examples
dockerdocker-composecircleci

Dynamically get a running container name created by docker-compose


When I run my docker-compose, it creates a web container and postgres container.

I want to manually trigger my Django tests to run, via something like

docker-compose run web python manage.py test

the problem with this is it creates a new container (requiring new migrations to be applied, housekeeping work, etc.)

The option I'm leaning towards it doing something like

docker exec -i -t <containerid> python manage.py test

This introduces a new issue which is that I must run docker ps first to grab the container name. The whole point of this is to automatically run the tests for each build so it has to be automated, manually running docker ps is not a solution.

So is there a way to dynamically grab the container id or is there a better way to do this? This would not be an issue if you could assign container names in docker-compose


Solution

  • Just use docker-compose exec. It will execute in the already-running container instead of starting a new one.

    docker-compose exec web python manage.py test