Coder Social home page Coder Social logo

giftora / dynamic-docker-compose Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 2.0 344 KB

Nodejs docker compose class with yaml merging.

Home Page: https://giftora.github.io/dynamic-docker-compose

License: MIT License

JavaScript 100.00%
docker docker-compose yaml yaml-parser node nodejs js-yaml deepmerge javascript class

dynamic-docker-compose's Introduction

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

dynamic-docker-compose's People

Contributors

giftora avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.