(self.webpackChunkdocker_compose=self.webpackChunkdocker_compose||[]).push([[509],{422:(n,s,e)=>{"use strict";e.r(s),e.d(s,{data:()=>o});const o={key:"v-8daa1a0e",path:"/",title:"Manage Docker-Compose via Node.js",lang:"en-US",frontmatter:{title:"Manage Docker-Compose via Node.js",lang:"en-US",home:!1},excerpt:"",headers:[{level:2,title:"Installation",slug:"installation",children:[]},{level:2,title:"Usage",slug:"usage",children:[{level:3,title:"Example",slug:"example",children:[]},{level:3,title:"Options",slug:"options",children:[]}]},{level:2,title:"Running the tests",slug:"running-the-tests",children:[]},{level:2,title:"Want to help?",slug:"want-to-help",children:[{level:3,title:"Discussions",slug:"discussions",children:[]}]},{level:2,title:"License",slug:"license",children:[]}],filePathRelative:"README.md",git:{updatedTime:1638169024e3,contributors:[]}}},467:(n,s,e)=>{"use strict";e.r(s),e.d(s,{default:()=>j});var o=e(252);const a=(0,o.Wm)("h1",{id:"manage-docker-compose-via-node-js"},[(0,o.Wm)("a",{class:"header-anchor",href:"#manage-docker-compose-via-node-js"},"#"),(0,o.Uk)(" Manage Docker-Compose via Node.js")],-1),t=(0,o.Wm)("code",null,"docker-compose",-1),c=(0,o.Uk)(" is a small library that allows you to run "),i={href:"https://docs.docker.com/compose/",target:"_blank",rel:"noopener noreferrer"},p=(0,o.Uk)("docker-compose"),l=(0,o.Uk)(" (which is still required) via Node.js. This is useful to bootstrap test environments."),r=(0,o.uE)('
npm install --save-dev docker-compose\ndocker-compose current supports these commands:
buildAll(options) - Build or rebuild servicesbuildMany(services, options) - Build or rebuild servicesbuildOne(service, options) - Build or rebuild serviceconfig(options) - Validates configuration files and returns configuration yamlconfigServices(options) - Returns list of services defined in configuration filesconfigVolumes(options) - Returns list of volumes defined in configuration filesdown(options) - Stops containers and removes containers, networks, volumes, and images created by upexec(container, command, options) - Exec command inside container - uses -T to properly handle stdin & stdoutkill(options) - Force stop service containerslogs(services, options) - Show logs of service(s) - use options.follow true|false to turn on --follow flagpauseOne(service, options) - Pause the specified serviceport(service, containerPort, options) - Returns the public port of the given service and internal port.ps(options) - Lists containers informationpullAll(options) - Pull all service imagespullMany(services, options) - Pull service images specifiedpullOne(service, options) - Pull a service imagerestartAll(options) - Restart all servicesrestartMany(services, options) - Restart servicesrestartOne(service, options) - Restart servicerm(options, services) - Remove stopped service containers - always uses the -f flag due to non interactive mode - services can optionally be used to select the containers to removerun(service, command, options) - Run a one-off command on a service - uses -T to properly handle stdin & stdoutstop(options) - Stop running containers without removing themstopOne(service, options) - Stops one container without removing itunpauseOne(service, options) - Resume the specified serviceupAll(options) - Builds, (re)creates, starts, and attaches to containers for all services - always uses the -d flag due to non interactive modeupMany(services, options) - Builds, (re)creates, starts, and attaches to containers for the services specified in services - always uses the -d flag due to non interactive modeupOne(service, options) - Builds, (re)creates, starts, and attaches to containers for a service specified in service - always uses the -d flag due to non interactive modeversion(options) - Show docker-compose version stringsAll commands return a Promise({object}) with stdout and stderr strings and an exit code:
{\n out: 'stdout contents',\n err: 'stderr contents',\n exitCode: 0, // !== 0 in case of an error\n}\nAlthough the return type is a Promise, it is still possible to get the process progres before the Promise resolves, by passing a callback function to the optional callback parameter.
To start service containers based on the docker-compose.yml file in your current directory, just call compose.up like this:
compose.upAll({ cwd: path.join(__dirname), log: true })\n .then(\n () => { console.log('done')},\n err => { console.log('something went wrong:', err.message)}\n );\nTo get process progres
compose.upAll({\n cwd: path.join(__dirname),\n callback: (chunk: Buffer) => {\n console.log('job in progres: ', chunk.ToString())\n }\n })\n .then(\n () => { console.log('job done')},\n err => { console.log('something went wrong:', err.message)}\n );\nTo execute command inside a running container
compose.exec('node', 'npm install', { cwd: path.join(__dirname) })\ndocker-compose accepts these params:
cwd {string}: mandatory folder path to the docker-compose.ymlexecutablePath {string}: optional path to docker-compose executable in case it's not located in $PATH /path/to/docker-composeconfig {(string|string[])}: custom and/or multiple yml files can be specified (relative to cwd)configAsString {string}: configuration can be provided as is, instead of relying on a file. In case configAsString is provided config will be ignored.[log] {boolean}: optional setting to enable console logging (output of docker-compose stdout/stderr output)[composeOptions] string[]|Array<string|string[]: pass optional compose options like "--verbose" or [["--verbose"], ["--log-level", "DEBUG"]] or ["--verbose", ["--loglevel", "DEBUG"]] for all commands.[callback] (chunk: Buffer, sourceStream?: 'stdout' | 'stderr') => void: optional callback function, that provides infromation about the process while it is still runing.[commandOptions] string[]|Array<string|string[]: pass optional command options like "--build" or [["--build"], ["--timeout", "5"]] or ["--build", ["--timeout", "5"]] for the up command. Viable commandOptions depend on the command (up, down etc.) itselfWhile docker-compose runs on Node.js 6+, running the tests requires you to use Node.js 8 as they make use of async/await.
yarn test\nThis project is just getting off the ground and could use some help with cleaning things up and refactoring.
If you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.
If you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.
',25),u=(0,o.Uk)("Before sending a PR, please "),d={href:"https://github.com/PDMLab/docker-compose/issues/new",target:"_blank",rel:"noopener noreferrer"},m=(0,o.Uk)("create an issue"),k=(0,o.Uk)(" to introduce your idea and have a reference for your PR."),g=(0,o.Uk)("We're using "),h={href:"https://www.conventionalcommits.org",target:"_blank",rel:"noopener noreferrer"},f=(0,o.Uk)("conventional commits"),b=(0,o.Uk)(", so please use it for your commits as well."),v=(0,o.Wm)("p",null,[(0,o.Uk)("Also please add tests and make sure to run "),(0,o.Wm)("code",null,"yarn lint"),(0,o.Uk)(".")],-1),w=(0,o.Wm)("h3",{id:"discussions"},[(0,o.Wm)("a",{class:"header-anchor",href:"#discussions"},"#"),(0,o.Uk)(" Discussions")],-1),y=(0,o.Uk)("If you want to discuss an "),R=(0,o.Wm)("code",null,"docker-compose",-1),T=(0,o.Uk)(" issue or PR in more detail, feel free to "),O={href:"https://github.com/PDMLab/docker-compose/discussions",target:"_blank",rel:"noopener noreferrer"},A=(0,o.Uk)("start a discussion"),E=(0,o.Uk)("."),I=(0,o.Wm)("h2",{id:"license"},[(0,o.Wm)("a",{class:"header-anchor",href:"#license"},"#"),(0,o.Uk)(" License")],-1),W=(0,o.Wm)("p",null,"MIT License",-1),S=(0,o.Wm)("p",null,"Copyright (c) 2017 - 2021 PDMLab",-1),N=(0,o.Wm)("p",null,'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:',-1),U=(0,o.Wm)("p",null,"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.",-1),q=(0,o.Wm)("p",null,'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.',-1),j={render:function(n,s){const e=(0,o.up)("OutboundLink");return(0,o.wg)(),(0,o.j4)(o.HY,null,[a,(0,o.Wm)("p",null,[t,c,(0,o.Wm)("a",i,[p,(0,o.Wm)(e)]),l]),r,(0,o.Wm)("p",null,[u,(0,o.Wm)("a",d,[m,(0,o.Wm)(e)]),k]),(0,o.Wm)("p",null,[g,(0,o.Wm)("a",h,[f,(0,o.Wm)(e)]),b]),v,w,(0,o.Wm)("p",null,[y,R,T,(0,o.Wm)("a",O,[A,(0,o.Wm)(e)]),E]),I,W,S,N,U,q],64)}}}}]);