ScanSkill

docker compose run

This docker compose run or docker-compose run command is used to run a one-time command against the service.

Syntax

$ docker compose run [options] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

Here,

OPTIONS:

  • --detach , -d → Detached mode: Run command in the background.
  • --env , -e → Set environment variables
  • --name → Assign a name to the container.
  • --interactive , -i → Keep STDIN open even if not attached.
  • --no-TTY , -T → Disable pseudo-TTY allocation. By default docker-compose, exec allocates a TTY.
  • --volume, -v → Bind mount a volume.
  • --tty , -t → Allocate a pseudo-TTY.
  • --user , -u → Run the command as this user.
  • --workdir , -w → Path to the working directory for this command.
  • --no-deps → Don’t start linked services.
  • --entrypooint → Override the entrypoint of the image

When you run commands with run command, It starts a new container with the configuration defined by that of the service, including volumes, links, and all other details. Main two important points:

  • Command defined in the service configuration is overridden by the command passed through run command. Example
  • docker-compose doesn’t create any ports specified in the service configuration. You have to explicitly specify using --service-ports flag.

Examples

  • Run one-time command against the service
$ docker-compose run cloudyfoxweb bas

This starts the web service and runs bash as its command.

  • Override the command specified in the configuration
$ docker-compose run cloudyfoxweb python app.py

Here, if the cloudyfoxweb is started with bash, then it overrides it with python [app.py](<http://app.py>) command.

  • Create service ports specified in the service configuration
$ docker-compose run --service-ports cloudyfoxweb python manage.py shell

Alternatively,

$ docker-compose run --publish 8080:8000 -p 2022:22 -p 127.0.0.1:2023:23 cloudyfoxweb python manage.py shell
  • For linked services, the run command first checks to see if the linked service is running and starts if stopped. For eg.
$ docker-compose run db psql -h db -U docker

Here, it starts an interactive shell of PostgreSQL for the linked db container service.

  • Run command run without starting linked containers(specify --no-deps flag)
$ docker-compose run --no-deps cloudyfoxweb python manage.py shell