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 hiding IP of calling container [closed]
  • Docker, CoreOS and fleet based deployments
  • Start and attach a docker container with X11 forwarding
  • Dockerize angular-cli 4 project fails while trying to run “ng build --aot --prod”
  • Why is /etc/hosts file empty in my docker container?
  • vagrant uses default and not that specified by provider
  • 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

  • docker run long multi-line commands on Windows Host
  • Google Container Engine: Accessing Cloud Storage
  • How to check if dotnet restore was ok in ps1?
  • ImportError: No module named ssl_match_hostname when importing the docker SDK for Python
  • $PWD is not set in ENV instruction in a Dockerfile
  • Labelling images in docker
  • 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.

    https://docs.docker.com/compose/compose-file/

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