docker compose run command
The docker compose run command starts a new container and runs a specific service, without starting all services defined in the entire Compose file.
The docker compose run command lets you perform tasks on a single service, such as running one-off commands or debugging.
The difference from docker compose up is that the run command only runs the specified service and does not start other services it depends on.
Syntax
docker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...]
- SERVICE: The name of a service defined in the Compose file.
- COMMAND and ARGS: Optional argument specifying the command and its arguments to run inside the container.
OPTIONS:
--rm: Automatically removes the container after the run.-d: Runs the container in detached mode.-T: Disables pseudo-TTY.
Example
1. Run a command for a specific service
docker compose run web python manage.py migrate
This command executes python manage.py migrate in the web service's container without starting other services.
** 2. Automatically remove the container**
docker compose run --rm web bash
This command runs the web service and starts a Bash terminal, then removes the container after the task completes.
docker compose run is ideal for performing one-off tasks in a service without affecting the operation of other services.
Docker Command Reference