Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 1.45 KB

File metadata and controls

72 lines (53 loc) · 1.45 KB
category Commands

exec

Execute a command in a running container.

Usage

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

Parameter Type Description
container string The container name to execute the command in
command string The command to execute
options IDockerComposeOptions Optional configuration

Examples

Run a shell command

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

Install dependencies

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

Run database migrations

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
compose.exec('node', 'whoami', {
  cwd: path.join(__dirname),
  commandOptions: [['--user', 'root']]
})