Skip to content

exec

Execute a command in a running container.

Usage

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

const result = await compose.exec('container', 'npm install', {
  cwd: path.join(__dirname)
})
console.log(result.out)

INFO

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

Parameters

ParameterTypeDescription
containerstringThe container name to execute the command in
commandstringThe command to execute
optionsIDockerComposeOptionsOptional configuration

Examples

Run a shell command

typescript
const result = await compose.exec('node', 'ls -la', {
  cwd: path.join(__dirname)
})

Install dependencies

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

Run database migrations

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

Options

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

  • --user / -u - Run the command as this user
  • --workdir / -w - Working directory inside the container
  • --env / -e - Set environment variables
typescript
compose.exec('node', 'whoami', {
  cwd: path.join(__dirname),
  commandOptions: [['--user', 'root']]
})