DockerComposeFile

DockerComposeFile

Docker Compose File class.
Represents the yaml object of a docker-compose file.
Allows for merging and mapping of compose files.

Constructor

# new DockerComposeFile(composeFileString) → {DockerComposeFile}

Creates a DockerComposeFile instance with provided docker-compose.yml file or string.

Parameters:
Name Type Description
composeFileString String | DockerComposeFile

a path to a docker-compose.yml file, a full yaml string or a DockerComposeFile instance.

Returns:

DockerComposeFile instance with all files or strings merged together.

Type
DockerComposeFile
Examples
const composeFile = new DockerComposeFile(`${__dirname}/docker-compose.yml`);
const composeFile = new DockerComposeFile(`${__dirname}/docker-compose.yml`, `${__dirname}/docker-compose.vpn.yml`);
const composeFile = new DockerComposeFile(`${__dirname}/docker-compose.yml, "version: '3'");

Members

# yaml :String

Merges the new yaml string to the current yaml also takes file paths or DockerComposeFiles.

Type:
  • String

Methods

# add(…composeFiles) → {String}

Merges the provided compose files into the current yaml class object.

Parameters:
Name Type Attributes Description
composeFiles String | DockerComposeFile <repeatable>

Any number of compose file paths or DockerComposeFile objects.

Returns:

Merged yaml string.

Type
String

# mapTemplate(…substitutions)

Takes a docker-compose file with template strings and generates an array of substituted docker compose files. Only operates with arrays of substitutions of the same length.

Parameters:
Name Type Attributes Description
substitutions Array.<String, Array.<String>> <repeatable>

Each set of substitutions an key and an array of values to map.

To Do:
  • allow for variable length substitutions.
Returns:

Array of substituted docker compose files.

Example
// docker-compose file containing template strings for ${REGION} and ${ID}.
const yamlString = 
`
services:
  vpn-${REGION}-${ID}:
    container_name: 'vpn-${REGION}-${ID}'
    environment:
      - REGION=${REGION}
`;
const composeFileTemplate = new DockerComposeFile(yamlString);
// Will return a array of 2 compose files for each substitution.
const composeFiles = composeFileTemplate.mapTemplate(
   ['REGION', ['US_West', 'US_Seattle']],
   ['ID', ['1', '2']]
);

# write(path)

Save the yaml file to disk.

Parameters:
Name Type Description
path

File path to save the yaml.