Skip to content

run

Run a one-off command on a service.

Usage

typescript
import * as compose from 'docker-compose'
import * as path from 'path'

const result = await compose.run('service', 'npm test', {
  cwd: path.join(__dirname)
})
console.log(result.out)

INFO

The run command uses -T to properly handle stdin & stdout in non-interactive mode.

Parameters

ParameterTypeDescription
servicestringThe service name to run the command on
commandstringThe command to execute
optionsIDockerComposeOptionsOptional configuration

Examples

Run tests

typescript
const result = await compose.run('node', 'npm test', {
  cwd: path.join(__dirname)
})

Run a one-off script

typescript
const result = await compose.run('app', 'node scripts/seed.js', {
  cwd: path.join(__dirname)
})

Generate files

typescript
const result = await compose.run('app', 'npx prisma generate', {
  cwd: path.join(__dirname)
})

Options

In addition to the common options, run supports these command options:

  • --rm - Remove container after run
  • --user / -u - Run the command as this user
  • --workdir / -w - Working directory inside the container
  • --env / -e - Set environment variables
  • --no-deps - Don't start linked services
typescript
compose.run('node', 'npm test', {
  cwd: path.join(__dirname),
  commandOptions: ['--rm', '--no-deps']
})