How can you run a one off command using Docker Compose?
Experience Level: Senior
Tags: Docker Compose
Answer
Use docker compose run.
First, the command passed by run overrides the command defined in the service configuration. For example, if the web service configuration is started with bash, then docker compose run web python app.py overrides it with python app.py.
The second difference is that the docker compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports
Related Docker Compose job interview questions
What are links in Docker Compose?
Docker Compose SeniorWhat are Docker Compose profiles good for?
Docker Compose SeniorHow can you define what labels will be used for an image built using Docker Compose?
Docker Compose SeniorHow can you override a command that will be executed in a container of a specific service in Docker Compose?
Docker Compose SeniorHow do you map ports to a specific service container in Docker Compose?
Docker Compose Senior