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
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.
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.
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-)
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With