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

  • Pass parameter (like -Des.node.name=“TestNode”) in docker run from Ansible or Fig
  • Why do I get Error: Could not find or load main class .jar when I run docker image
  • docker image running a mac osx installation
  • Shutting down MySQL 4 correctly in Docker Container
  • Django with docker doesn't run custom app's migrations
  • How do I compile python for Alpine?
  • 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

  • SQL Server on Mac with Docker : Login failed for user 'sa'.
  • How to copy docker volume from one machine to another?
  • Docker compose builds image in one directory but not in another
  • Running Portainer in a Docker Container with Apache 2.4 mod_proxy and basic auth
  • why docker container still can communicate with outside when i shutdown the iptables
  • docker exec wrong with remote docker host in Jenkins
  • 2 Solutions collect form web for “Dynamically get a running container name created by docker-compose”

    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

    You can assign a name to a container using container_name option on docker-compose.yml file.

    container_name: container_name

    Then, you can easily run commands in that container using.

    docker exec container_name python manage.py test.

    For more information about docker-compose options, visit the official documentation.


    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.