Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 883
david Avatar asked Jan 12 '17 21:01

david


People also ask

How do I name my docker container when running?

How to Name a Docker Container. You can assign memorable names to your docker containers when you run them, using the --name flag as follows. The -d flag tells docker to run a container in detached mode, in the background and print the new container ID.

Does docker compose create containers?

docker-compose run : This is similar to the docker run command. It will create containers from images built for the services mentioned in the compose file. * Debugger is active! docker-compose up : This command does the work of the docker-compose build and docker-compose run commands.


4 Answers

While an accepted answer was provided, the answer itself is not really related to the title of this question:

Dynamically get a running container name created by docker-compose

To dynamically retrieve the name of a container run by docker-compose you can execute the following command:

$(docker inspect -f '{{.Name}}' $(docker-compose ps -q web) | cut -c2-)
like image 64
Toni Van de Voorde Avatar answered Oct 19 '22 10:10

Toni Van de Voorde


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
like image 29
Dan Lowe Avatar answered Oct 19 '22 09:10

Dan Lowe


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/

like image 34
kstromeiraos Avatar answered Oct 19 '22 08:10

kstromeiraos


Use docker-compose ps -q to find the ID of the container and run the command in that:

docker exec -it $(docker-compose ps -q) sh

PS: This will NOT work if you have multiple containers in the docker-compose file.

like image 2
Pylinux Avatar answered Oct 19 '22 10:10

Pylinux