docker-compose/index.html
2021-11-29 07:19:26 +00:00

35 lines
23 KiB
HTML

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Manage Docker-Compose via Node.js | </title><meta name="description" content=""><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="generator" content="VuePress 2.0.0-beta.7">
<link rel="preload" href="/docker-compose/assets/js/runtime~app.1cb1fcf0.js" as="script"><link rel="preload" href="/docker-compose/assets/css/styles.c29781f7.css" as="style"><link rel="preload" href="/docker-compose/assets/js/693.a7e8b7d1.js" as="script"><link rel="preload" href="/docker-compose/assets/js/app.b9941a80.js" as="script">
<link rel="stylesheet" href="/docker-compose/assets/css/styles.c29781f7.css">
</head>
<body>
<div id="app"><!--[--><div class="theme-container no-sidebar"><header class="navbar"><div class="toggle-sidebar-button"><svg class="icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z" class=""></path></svg></div><span><a aria-current="page" href="/docker-compose/" class="router-link-active router-link-exact-active"><!----><!----></a></span><div class="navbar-links-wrapper" style=""><!--[--><!--]--><nav class="navbar-links can-hide"><!--[--><div class="navbar-links-item"><a class="nav-link external" href="https://github.com/PDMLab/docker-compose" rel="noopener noreferrer" target="_blank" aria-label="GitHub"><!--[--><!--]--> GitHub <span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span><!--[--><!--]--></a></div><div class="navbar-links-item"><a class="nav-link external" href="https://npmjs.com/docker-compose" rel="noopener noreferrer" target="_blank" aria-label="npm"><!--[--><!--]--> npm <span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><!----></div></header><div class="sidebar-mask"></div><aside class="sidebar"><nav class="navbar-links"><!--[--><div class="navbar-links-item"><a class="nav-link external" href="https://github.com/PDMLab/docker-compose" rel="noopener noreferrer" target="_blank" aria-label="GitHub"><!--[--><!--]--> GitHub <span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span><!--[--><!--]--></a></div><div class="navbar-links-item"><a class="nav-link external" href="https://npmjs.com/docker-compose" rel="noopener noreferrer" target="_blank" aria-label="npm"><!--[--><!--]--> npm <span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-links"><!--[--><!--]--></ul><!--[--><!--]--></aside><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><h1 id="manage-docker-compose-via-node-js"><a class="header-anchor" href="#manage-docker-compose-via-node-js">#</a> Manage Docker-Compose via Node.js</h1><p><code>docker-compose</code> is a small library that allows you to run <a href="https://docs.docker.com/compose/" target="_blank" rel="noopener noreferrer">docker-compose<span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span></a> (which is still required) via Node.js. This is useful to bootstrap test environments.</p><h2 id="installation"><a class="header-anchor" href="#installation">#</a> Installation</h2><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>npm install --save-dev docker-compose
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><h2 id="usage"><a class="header-anchor" href="#usage">#</a> Usage</h2><p><code>docker-compose</code> current supports these commands:</p><ul><li><code>buildAll(options)</code> - Build or rebuild services</li><li><code>buildMany(services, options)</code> - Build or rebuild services</li><li><code>buildOne(service, options)</code> - Build or rebuild service</li><li><code>config(options)</code> - Validates configuration files and returns configuration yaml</li><li><code>configServices(options)</code> - Returns list of services defined in configuration files</li><li><code>configVolumes(options)</code> - Returns list of volumes defined in configuration files</li><li><code>down(options)</code> - Stops containers and removes containers, networks, volumes, and images created by <code>up</code></li><li><code>exec(container, command, options)</code> - Exec <code>command</code> inside <code>container</code> - uses <code>-T</code> to properly handle stdin &amp; stdout</li><li><code>kill(options)</code> - Force stop service containers</li><li><code>logs(services, options)</code> - Show logs of service(s) - use <code>options.follow</code> <code>true|false</code> to turn on <code>--follow</code> flag</li><li><code>pauseOne(service, options)</code> - Pause the specified service</li><li><code>port(service, containerPort, options)</code> - Returns the public port of the given service and internal port.</li><li><code>ps(options)</code> - Lists containers information</li><li><code>pullAll(options)</code> - Pull all service images</li><li><code>pullMany(services, options)</code> - Pull service images specified</li><li><code>pullOne(service, options)</code> - Pull a service image</li><li><code>restartAll(options)</code> - Restart all services</li><li><code>restartMany(services, options)</code> - Restart services</li><li><code>restartOne(service, options)</code> - Restart service</li><li><code>rm(options, services)</code> - Remove stopped service containers - always uses the <code>-f</code> flag due to non interactive mode - <code>services</code> can optionally be used to select the containers to remove</li><li><code>run(service, command, options)</code> - Run a one-off <code>command</code> on a service - uses <code>-T</code> to properly handle stdin &amp; stdout</li><li><code>stop(options)</code> - Stop running containers without removing them</li><li><code>stopOne(service, options)</code> - Stops one container without removing it</li><li><code>unpauseOne(service, options)</code> - Resume the specified service</li><li><code>upAll(options)</code> - Builds, (re)creates, starts, and attaches to containers for all services - always uses the <code>-d</code> flag due to non interactive mode</li><li><code>upMany(services, options)</code> - Builds, (re)creates, starts, and attaches to containers for the services specified in <code>services</code> - always uses the <code>-d</code> flag due to non interactive mode</li><li><code>upOne(service, options)</code> - Builds, (re)creates, starts, and attaches to containers for a service specified in <code>service</code> - always uses the <code>-d</code> flag due to non interactive mode</li><li><code>version(options)</code> - Show <code>docker-compose</code> version strings</li></ul><p>All commands return a <code>Promise({object})</code> with stdout and stderr strings and an exit code:</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token punctuation">{</span>
out<span class="token operator">:</span> <span class="token string">&#39;stdout contents&#39;</span><span class="token punctuation">,</span>
err<span class="token operator">:</span> <span class="token string">&#39;stderr contents&#39;</span><span class="token punctuation">,</span>
exitCode<span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token comment">// !== 0 in case of an error</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>Although the return type is a <code>Promise</code>, it is still possible to get the process progres before the <code>Promise</code> resolves, by passing a callback function to the optional <code>callback</code> parameter.</p><h3 id="example"><a class="header-anchor" href="#example">#</a> Example</h3><p>To start service containers based on the <code>docker-compose.yml</code> file in your current directory, just call <code>compose.up</code> like this:</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code>compose<span class="token punctuation">.</span><span class="token function">upAll</span><span class="token punctuation">(</span><span class="token punctuation">{</span> cwd<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span><span class="token punctuation">,</span> log<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&#39;done&#39;</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token parameter">err</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&#39;something went wrong:&#39;</span><span class="token punctuation">,</span> err<span class="token punctuation">.</span>message<span class="token punctuation">)</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>To get process progres</p><div class="language-typescript ext-ts line-numbers-mode"><pre class="language-typescript"><code>compose<span class="token punctuation">.</span><span class="token function">upAll</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
cwd<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token function-variable function">callback</span><span class="token operator">:</span> <span class="token punctuation">(</span>chunk<span class="token operator">:</span> Buffer<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
<span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&#39;job in progres: &#39;</span><span class="token punctuation">,</span> chunk<span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&#39;job done&#39;</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
err <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&#39;something went wrong:&#39;</span><span class="token punctuation">,</span> err<span class="token punctuation">.</span>message<span class="token punctuation">)</span><span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>To execute command inside a running container</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code>compose<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">&#39;node&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;npm install&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> cwd<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><h3 id="options"><a class="header-anchor" href="#options">#</a> Options</h3><p><code>docker-compose</code> accepts these params:</p><ul><li><code>cwd {string}</code>: mandatory folder path to the <code>docker-compose.yml</code></li><li><code>executablePath {string}</code>: optional path to docker-compose executable in case it&#39;s not located in $PATH <code>/path/to/docker-compose</code></li><li><code>config {(string|string[])}</code>: custom and/or multiple yml files can be specified (relative to <code>cwd</code>)</li><li><code>configAsString {string}</code>: configuration can be provided as is, instead of relying on a file. In case <code>configAsString</code> is provided <code>config</code> will be ignored.</li><li><code>[log] {boolean}</code>: optional setting to enable console logging (output of <code>docker-compose</code> <code>stdout</code>/<code>stderr</code> output)</li><li><code>[composeOptions] string[]|Array&lt;string|string[]</code>: pass optional compose options like <code>&quot;--verbose&quot;</code> or <code>[[&quot;--verbose&quot;], [&quot;--log-level&quot;, &quot;DEBUG&quot;]]</code> or <code>[&quot;--verbose&quot;, [&quot;--loglevel&quot;, &quot;DEBUG&quot;]]</code> for <em>all</em> commands.</li><li><code>[callback] (chunk: Buffer, sourceStream?: &#39;stdout&#39; | &#39;stderr&#39;) =&gt; void</code>: optional callback function, that provides infromation about the process while it is still runing.</li><li><code>[commandOptions] string[]|Array&lt;string|string[]</code>: pass optional command options like <code>&quot;--build&quot;</code> or <code>[[&quot;--build&quot;], [&quot;--timeout&quot;, &quot;5&quot;]]</code> or <code>[&quot;--build&quot;, [&quot;--timeout&quot;, &quot;5&quot;]]</code> for the <code>up</code> command. Viable <code>commandOptions</code> depend on the command (<code>up</code>, <code>down</code> etc.) itself</li></ul><h2 id="running-the-tests"><a class="header-anchor" href="#running-the-tests">#</a> Running the tests</h2><p>While <code>docker-compose</code> runs on Node.js 6+, running the tests requires you to use Node.js 8 as they make use of <code>async/await</code>.</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>yarn test
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><h2 id="want-to-help"><a class="header-anchor" href="#want-to-help">#</a> Want to help?</h2><p>This project is just getting off the ground and could use some help with cleaning things up and refactoring.</p><p>If you want to contribute - we&#39;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.</p><p>If you see a bug, please be so kind as to show how it&#39;s failing, and we&#39;ll do our best to get it fixed quickly.</p><p>Before sending a PR, please <a href="https://github.com/PDMLab/docker-compose/issues/new" target="_blank" rel="noopener noreferrer">create an issue<span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span></a> to introduce your idea and have a reference for your PR.</p><p>We&#39;re using <a href="https://www.conventionalcommits.org" target="_blank" rel="noopener noreferrer">conventional commits<span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span></a>, so please use it for your commits as well.</p><p>Also please add tests and make sure to run <code>yarn lint</code>.</p><h3 id="discussions"><a class="header-anchor" href="#discussions">#</a> Discussions</h3><p>If you want to discuss an <code>docker-compose</code> issue or PR in more detail, feel free to <a href="https://github.com/PDMLab/docker-compose/discussions" target="_blank" rel="noopener noreferrer">start a discussion<span><svg class="icon outbound" xmlns="http://www.w3.org/2000/svg" ariahidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><!--[--><span class="sr-only">open in new window</span><!--]--></span></a>.</p><h2 id="license"><a class="header-anchor" href="#license">#</a> License</h2><p>MIT License</p><p>Copyright (c) 2017 - 2021 PDMLab</p><p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the &quot;Software&quot;), 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:</p><p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p><p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, 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.</p><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">Last Updated: </span><span class="meta-item-info">11/29/2021, 6:57:04 AM</span></div><!----></footer><!----><!--[--><!--]--></main></div><!----><!--]--></div>
<script src="/docker-compose/assets/js/runtime~app.1cb1fcf0.js" defer></script><script src="/docker-compose/assets/js/693.a7e8b7d1.js" defer></script><script src="/docker-compose/assets/js/app.b9941a80.js" defer></script>
</body>
</html>