docker compose run Command

Docker command reference


The docker compose run command is used to start a new container and run a specific service, without starting all services defined in the entire Compose file.

The docker compose run command allows you to 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 that depend on it.

Syntax

docker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...]
  • SERVICE:The service name defined in the Compose file.
  • COMMANDandARGSOptional parameter, specifying the command and its arguments to run inside the container.

OPTIONS:

  • --rm:Automatically delete the container after running.
  • -d:Run the container in detached mode.
  • -T:Disable pseudo-TTY.

Example

1. Command to run a specific service

docker compose run web python manage.py migrate

This command will execute the `python manage.py migrate` command in the container of the `web` service, without starting other services.

2. Automatically delete the container

docker compose run --rm web bash

This command will run the web service and start a Bash terminal, and will delete the container after the task is completed.

docker compose run is very suitable for executing one-off tasks in a service without affecting the operation of other services.


Docker command reference

other extensions