mirror of
https://github.com/danbulant/docker-compose
synced 2026-06-14 04:01:21 +00:00
commit
219a93d245
5 changed files with 105 additions and 26 deletions
59
index.js
59
index.js
|
|
@ -1,20 +1,16 @@
|
|||
'use strict';
|
||||
|
||||
const promisify = require('es6-promisify');
|
||||
const exec = promisify(require('child_process').exec, { multiArgs: true });
|
||||
const execute = promisify(require('child_process').exec, { multiArgs: true });
|
||||
const logger = require('./lib/log');
|
||||
|
||||
const logStandards = function (standards) {
|
||||
const stdout = standards.stdout || standards[0];
|
||||
|
||||
if (stdout && stdout.length > 0) {
|
||||
logger.info(stdout);
|
||||
const logStandards = function (std) {
|
||||
if (std.out && std.out.length > 0) {
|
||||
logger.info(std.out);
|
||||
}
|
||||
|
||||
const stderr = standards.stderr || standards[1];
|
||||
|
||||
if (stderr) {
|
||||
logger.warn(stderr);
|
||||
if (std.err && std.err.length > 0) {
|
||||
logger.warn(std.err);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -46,13 +42,18 @@ const execCompose = (command, options) => new Promise((resolve, reject) => {
|
|||
const cmd = `docker-compose ${configToArgs(options.config)} ${command}`;
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec(cmd, { cwd }).then(
|
||||
execute(cmd, { cwd }).then(
|
||||
standards => {
|
||||
const std = {
|
||||
out: standards[0],
|
||||
err: standards[1]
|
||||
};
|
||||
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
logStandards(std);
|
||||
}
|
||||
|
||||
resolve();
|
||||
resolve(std);
|
||||
},
|
||||
error => {
|
||||
logger.error(error.message);
|
||||
|
|
@ -112,4 +113,34 @@ const rm = function (options) {
|
|||
return execCompose('rm -f', options);
|
||||
};
|
||||
|
||||
module.exports = { up, kill, down, stop, rm };
|
||||
/**
|
||||
* Execute command in a running container
|
||||
* @param {string} contaier container name
|
||||
* @param {string} command command to execute
|
||||
* @param {object} options
|
||||
* @param {string} options.cwd
|
||||
* @param {boolean} [options.log]
|
||||
* @param {?(string|string[])} [options.config]
|
||||
*
|
||||
* @return {object} std.out / std.err
|
||||
*/
|
||||
const exec = function (container, command, options) {
|
||||
return execCompose(`exec -T ${container} ${command}`, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Run command
|
||||
* @param {string} contaier container name
|
||||
* @param {string} command command to execute
|
||||
* @param {object} options
|
||||
* @param {string} options.cwd
|
||||
* @param {boolean} [options.log]
|
||||
* @param {?(string|string[])} [options.config]
|
||||
*
|
||||
* @return {object} std.out / std.err
|
||||
*/
|
||||
const run = function (container, command, options) {
|
||||
return execCompose(`run -T ${container} ${command}`, options);
|
||||
};
|
||||
|
||||
module.exports = { up, kill, down, stop, rm, exec, run };
|
||||
|
|
|
|||
25
readme.md
25
readme.md
|
|
@ -12,13 +12,21 @@ npm install --save-dev docker-compose
|
|||
|
||||
`docker-compose` current supports these commands:
|
||||
|
||||
* up - create and start containers - always uses the `-d` flag due to non interactive mode
|
||||
* down - Stop and remove containers, networks, images, and volumes
|
||||
* kill - Kill containers
|
||||
* stop - Stop services
|
||||
* rm - Remove stopped containers - always uses the `-f` flag due to non interactive mode
|
||||
* `up(options)` - create and start containers - always uses the `-d` flag due to non interactive mode
|
||||
* `down(options)` - Stop and remove containers, networks, images, and volumes
|
||||
* `kill(options)` - Kill containers
|
||||
* `stop(options)` - Stop services
|
||||
* `rm(options)` - Remove stopped containers - always uses the `-f` flag due to non interactive mode
|
||||
* `exec(container, command, options)` - Exec `command` inside `container`, uses `-T` to properly handle stdin & stdout
|
||||
* `run(container, command, options)` - Run `command` inside `container`, uses `-T` to properly handle stdin & stdout
|
||||
|
||||
All commands return a Promise.
|
||||
All commands return a `Promise({object})` with an stdout and stderr strings
|
||||
```javascript
|
||||
{
|
||||
out: 'stdout contents'
|
||||
err: 'stderr contents'
|
||||
}
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
|
|
@ -32,6 +40,11 @@ compose.up({ cwd: path.join(__dirname), log: true })
|
|||
);
|
||||
```
|
||||
|
||||
To execute command inside a running container
|
||||
```javascript
|
||||
compose.exec('node', 'npm install', { cwd: path.join(__dirname) })
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
`docker-compose` accepts these params:
|
||||
|
|
|
|||
|
|
@ -2,5 +2,5 @@ version: '2'
|
|||
|
||||
services:
|
||||
db:
|
||||
image: alpine:3.7
|
||||
container_name: compose_test_alpine
|
||||
image: mongo:3.4
|
||||
container_name: compose_test_mongodb_2
|
||||
|
|
@ -3,4 +3,7 @@ version: '2'
|
|||
services:
|
||||
db:
|
||||
image: mongo:3.4
|
||||
container_name: compose_test_mongodb
|
||||
container_name: compose_test_mongodb
|
||||
alpine:
|
||||
image: alpine:3.7
|
||||
container_name: compose_test_apline
|
||||
|
|
@ -57,11 +57,43 @@ test('ensure custom ymls are working', async assert => {
|
|||
const log = true;
|
||||
|
||||
await compose.up({ cwd, log, config });
|
||||
assert.true(await isContainerRunning('/compose_test_alpine'));
|
||||
assert.true(await isContainerRunning('/compose_test_mongodb_2'));
|
||||
|
||||
// config & [config] is the same thing, ensures that multiple configs are handled properly
|
||||
// config & [config] are the same thing, ensures that multiple configs are handled properly
|
||||
await compose.kill({ cwd, log, config: [ config ]});
|
||||
assert.false(await isContainerRunning('/compose_test_alpine'));
|
||||
assert.false(await isContainerRunning('/compose_test_mongodb_2'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure run and exec are working', async assert => {
|
||||
const checkOSID = (out, id) => {
|
||||
// parse /etc/os-release contents
|
||||
const re = /([\w,_]+)=(.*)/g;
|
||||
let match = null;
|
||||
const os = {};
|
||||
|
||||
while ((match = re.exec(out)) !== null) { // eslint-disable-line no-cond-assign
|
||||
os[match[1]] = match[2];
|
||||
}
|
||||
|
||||
assert.equals(os.ID, id);
|
||||
};
|
||||
|
||||
const opts = { cwd: path.join(__dirname), log: false };
|
||||
|
||||
await compose.up(opts);
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_mongodb'));
|
||||
|
||||
let std = await compose.exec('db', 'cat /etc/os-release', opts);
|
||||
|
||||
assert.false(std.err);
|
||||
checkOSID(std.out, 'debian');
|
||||
|
||||
std = await compose.run('alpine', 'cat /etc/os-release', opts);
|
||||
assert.false(std.err);
|
||||
checkOSID(std.out, 'alpine');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue