[go: up one dir, main page]

Skip to content

Giftora/dynamic-docker-compose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM package https://www.npmjs.com/package/dynamic-docker-compose

dynamic-docker-compose

Nodejs docker-compose class.
Loads one or more docker-compose files and parses the yaml contained within.
When more than one file is supplyed the contents are merged on like keys. Yaml parsing is done with js-yaml. Object merging preformed by deepmerge

Usage jsdocs

Create an instance of the class like so.

import DockerComposeFile from 'dynamic-docker-compose';
const composeFile = new DockerComposeFile(`${__dirname}/docker-compose.yml`);
// or 
const yamlString = 
`
networks:
  proxy:
    driver: bridge
services:
  vpn:
    cap_add:
      - NET_ADMIN
    container_name: vpn
    environment:
      - REGION
      - OPENVPN_USER
      - VPNSP
    image: vpn
    networks:
      - proxy
    restart: always
    volumes:
      - ./vpn/volumes/gluton/:/gluetun
      - ./data:/data
version: '3'
`;
const composeFile = new DockerComposeFile(yamlString);

After you have created an instance of DockerComposeFile you can modify the YAML object using js-yaml.

Multiple files can be passed to the constructor

import DockerComposeFile from 'dynamic-docker-compose';
const composeFile = new DockerComposeFile(
  `${__dirname}/docker-compose1.yml`, 
  `${__dirname}/docker-compose2.yml`
);

Merging

The contents are merged on like keys.

Yaml version file.

# version.docker-compose.yml
version: '3'

Yaml network file.

# network.docker-compose.yml
networks:
  proxy:
    driver: bridge

Vpn service yaml file.

# vpn.docker-compose.yml
services:
  vpn:
    cap_add:
      - NET_ADMIN
    container_name: vpn
    environment:
      - REGION
      - OPENVPN_USER
      - VPNSP
    image: vpn
    networks:
      - proxy
    restart: always
    volumes:
      - ./vpn/volumes/gluton/:/gluetun
      - ./data:/data

Nodejs service yaml file.

# node-server.docker-compose.yml
services:
  node-server:
    container_name: node-server
    image: node
    networks:
      - proxy
    restart: always
import DockerComposeFile from 'dynamic-docker-compose';
const composeFile = new DockerComposeFile(
  `version.docker-compose.yml`, 
  `network.docker-compose.yml`,
  `vpn.docker-compose.yml`,
  `node-server.docker-compose.yml`
);

Outputing

composeFile.yaml;

will result in the following merged yaml file.

# node-server.docker-compose.yml
networks:
  proxy:
    driver: bridge
services:
  node-server:
    container_name: node-server
    image: node
    networks:
      - proxy
    restart: always
  vpn:
    cap_add:
      - NET_ADMIN
    container_name: vpn
    environment:
      - REGION
      - OPENVPN_USER
      - VPNSP
    image: vpn
    networks:
      - proxy
    restart: always
    volumes:
      - ./vpn/volumes/gluton/:/gluetun
      - ./data:/data
version: '3'

Mapping

DockerComposeFile can be mapped When template strings exist in the yaml file.
Template strings are in ${TEMPLATE_STRING} format.

# vpn.docker-compose.yml
services:
  ${VPN_NAME}:
    cap_add:
      - NET_ADMIN
    container_name: ${VPN_NAME}
    environment:
      - REGION=${REGION}
      - OPENVPN_USER
      - VPNSP
    image: vpn
    networks:
      - proxy
    restart: always
    volumes:
      - ./vpn/volumes/gluton/:/gluetun
      - ./data:/data
const composeFileTemplate = new DockerComposeFile(`vpn.docker-compose.yml`);
const composeFiles = composeFileTemplate.mapTemplate(
  ['VPN_NAME', ['vpn-west', 'vpn-seattle']],
  ['REGION', ['US_West', 'US_Seattle']],
);

This would create two DockerComposeFile instances.

# vpn.docker-compose.yml
services:
  vpn-west:
    cap_add:
      - NET_ADMIN
    container_name: vpn-west
    environment:
      - REGION=US_West
      - OPENVPN_USER
      - VPNSP
    image: vpn
    networks:
      - proxy
    restart: always
    volumes:
      - ./vpn/volumes/gluton/:/gluetun
      - ./data:/data
# vpn.docker-compose.yml
services:
  vpn-seattle:
    cap_add:
      - NET_ADMIN
    container_name: vpn-seattle
    environment:
      - REGION=US_Seattle
      - OPENVPN_USER
      - VPNSP
    image: vpn
    networks:
      - proxy
    restart: always
    volumes:
      - ./vpn/volumes/gluton/:/gluetun
      - ./data:/data

Those files can be merged afterwards by passing them to a new DockerComposeFile.

const composeFileTemplate = new DockerComposeFile(`vpn.docker-compose.yml`);
const composeFiles = composeFileTemplate.mapTemplate(
  ['VPN_NAME', ['vpn-west', 'vpn-seattle']],
  ['REGION', ['US_West', 'US_Seattle']],
);
const mergedMap = new DockerComposeFile(...composeFiles);