Skip to content

API Docs

This page demonstrates the usage of docker-compose for Node.js.

Commands

CommandDescription
upBuilds, (re)creates, starts, and attaches to containers
downStops and removes containers, networks, volumes, and images
stopStops running containers without removing them
restartRestart services
buildBuild or rebuild services
createCreate containers without starting them
pullPull service images
pushPush service images
configValidate and view configuration
psList containers
imagesList images
logsView container logs
execExecute a command in a running container
runRun a one-off command
rmRemove stopped containers
killForce stop containers
pausePause and unpause services
portPrint public port for a port binding
versionShow version information
statsDisplay container resource usage

Return Type

All commands return a Promise({object}) with stdout and stderr strings and an exit code:

typescript
{
  out: 'stdout contents',
  err: 'stderr contents',
  exitCode: 0, // !== 0 in case of an error
}

Progress Callback

Although the return type is a Promise, it is still possible to get the process progress before the Promise resolves, by passing a callback function to the optional callback parameter.

typescript
compose.upAll({
  cwd: path.join(__dirname),
  callback: (chunk: Buffer) => {
    console.log('job in progress: ', chunk.toString())
  }
}).then(
  () => { console.log('job done') },
  err => { console.log('something went wrong:', err.message) }
)

Options

docker-compose accepts these params:

OptionTypeDescription
cwdstringRequired. Folder path to the docker-compose.yml
executablePathstringPath to docker-compose executable if not in $PATH
configstring | string[]Custom yml file(s), relative to cwd
configAsStringstringConfiguration as string (ignores config if set)
logbooleanEnable console logging
composeOptionsstring[] | Array<string | string[]>Options for all commands (e.g., --verbose)
commandOptionsstring[] | Array<string | string[]>Options for specific command
callback(chunk: Buffer, sourceStream?: 'stdout' | 'stderr') => voidProgress callback

Example with options

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

compose.upAll({
  cwd: path.join(__dirname),
  config: 'docker-compose.prod.yml',
  log: true,
  composeOptions: ['--verbose'],
  commandOptions: ['--build', ['--timeout', '30']]
})