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
  truncated: { out: false, err: false },
}

truncated.out and truncated.err indicate whether any output was dropped from the corresponding stream, including when buffering is disabled. These flags are also present on results rejected for a nonzero exit code and on typed results with a data field. Commands that parse stdout (config, configServices, configVolumes, ps, images, port, version, and stats) reject with an error mentioning maxOutputLength if stdout was truncated. Truncating only stderr does not prevent parsing stdout.

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) }
)

Output buffering ​

Use maxOutputLength to limit how much output is retained independently for stdout and stderr. The limit counts UTF-16 code units, as measured by JavaScript's string.length, and defaults to Node.js's buffer.constants.MAX_STRING_LENGTH. Stdout retains the beginning of the output; stderr retains a trailing window so the most recent error messages remain available. Reaching the limit does not stop the command, and callback and log still receive all output.

Set maxOutputLength: 0 to disable buffering completely and process output only as it arrives. out and err remain empty strings, and each truncation flag becomes true if its stream emits nonempty output:

typescript
await compose.logs('web', {
  cwd: path.join(__dirname),
  follow: true,
  maxOutputLength: 0,
  callback: (chunk, streamSource) => {
    const stream = streamSource === 'stderr' ? process.stderr : process.stdout
    stream.write(chunk)
  }
})

Finite limits are rounded down and clamped between 0 and buffer.constants.MAX_STRING_LENGTH. Non-finite values use the default limit.

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)
composeComposeSpecificationTyped compose configuration object (converted to YAML internally)
logbooleanEnable console logging
composeOptionsstring[] | Array<string | string[]>Options for all commands (e.g., --verbose)
commandOptionsstring[] | Array<string | string[]>Options for specific command
maxOutputLengthnumberMaximum UTF-16 code units retained per stream; defaults to buffer.constants.MAX_STRING_LENGTH. 0 disables buffering.
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']]
})

Example with typed compose object ​

Instead of using a YAML file, you can pass a typed ComposeSpecification object directly. This gives you full TypeScript autocompletion and type checking for the Docker Compose configuration.

typescript
import { upAll, ComposeSpecification } from 'docker-compose'

const compose: ComposeSpecification = {
  services: {
    web: {
      image: 'nginx:latest',
      ports: ['8080:80']
    },
    db: {
      image: 'postgres:16',
      environment: {
        POSTGRES_PASSWORD: 'secret'
      }
    }
  }
}

await upAll({ compose })

The ComposeSpecification type is generated from the official Compose Specification JSON Schema, so it covers all valid compose file options.