mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-24 12:35:32 +00:00
Merge pull request #23 from rafalsiwiec/more-detailed-up-methods
More detailed up methods
This commit is contained in:
commit
b606d7c054
4 changed files with 63 additions and 13 deletions
28
index.js
28
index.js
|
|
@ -69,10 +69,34 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) =>
|
||||||
* @param {?(string|string[])} [options.config]
|
* @param {?(string|string[])} [options.config]
|
||||||
* @param {?object} [options.env]
|
* @param {?object} [options.env]
|
||||||
*/
|
*/
|
||||||
const up = function (options) {
|
const upAll = function (options) {
|
||||||
return execCompose('up', [ '-d' ], options);
|
return execCompose('up', [ '-d' ], options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string[]} services
|
||||||
|
* @param {object} options
|
||||||
|
* @param {string} options.cwd
|
||||||
|
* @param {boolean} [options.log]
|
||||||
|
* @param {?(string|string[])} [options.config]
|
||||||
|
* @param {?object} [options.env]
|
||||||
|
*/
|
||||||
|
const upMany = function (services, options) {
|
||||||
|
return execCompose('up', [ '-d' ].concat(services), options);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} service
|
||||||
|
* @param {object} options
|
||||||
|
* @param {string} options.cwd
|
||||||
|
* @param {boolean} [options.log]
|
||||||
|
* @param {?(string|string[])} [options.config]
|
||||||
|
* @param {?object} [options.env]
|
||||||
|
*/
|
||||||
|
const upOne = function (service, options) {
|
||||||
|
return execCompose('up', [ '-d', service ], options);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {object} options
|
* @param {object} options
|
||||||
* @param {string} options.cwd
|
* @param {string} options.cwd
|
||||||
|
|
@ -197,4 +221,4 @@ const buildOne = function (service, options) {
|
||||||
return execCompose('build', [ service ], options);
|
return execCompose('build', [ service ], options);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { up, kill, down, stop, rm, exec, run, buildAll, buildMany, buildOne };
|
module.exports = { upAll, upMany, upOne, kill, down, stop, rm, exec, run, buildAll, buildMany, buildOne };
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,18 @@ npm install --save-dev docker-compose
|
||||||
|
|
||||||
`docker-compose` current supports these commands:
|
`docker-compose` current supports these commands:
|
||||||
|
|
||||||
* `up(options)` - create and start containers - always uses the `-d` flag due to non interactive mode
|
* `upAll(options)` - Create and start containers - always uses the `-d` flag due to non interactive mode
|
||||||
|
* `upMany(services, options)` - Create and start containers specified in `services` - always uses the `-d` flag due to non interactive mode
|
||||||
|
* `upOne(service, options)` - Create and start container specified in `service` - always uses the `-d` flag due to non interactive mode
|
||||||
* `down(options)` - Stop and remove containers, networks, images, and volumes
|
* `down(options)` - Stop and remove containers, networks, images, and volumes
|
||||||
* `kill(options)` - Kill containers
|
* `kill(options)` - Kill containers
|
||||||
* `stop(options)` - Stop services
|
* `stop(options)` - Stop services
|
||||||
* `rm(options)` - Remove stopped containers - always uses the `-f` flag due to non interactive mode
|
* `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
|
* `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
|
* `run(container, command, options)` - Run `command` inside `container`, uses `-T` to properly handle stdin & stdout
|
||||||
|
* `buildAll(options)` - Build all images
|
||||||
|
* `buildMany(services, options)` - Build images of specified services
|
||||||
|
* `buildOne(service, options)` - Build image of specified service
|
||||||
|
|
||||||
All commands return a `Promise({object})` with an stdout and stderr strings
|
All commands return a `Promise({object})` with an stdout and stderr strings
|
||||||
```javascript
|
```javascript
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,4 @@ services:
|
||||||
container_name: compose_test_mongodb
|
container_name: compose_test_mongodb
|
||||||
alpine:
|
alpine:
|
||||||
image: alpine:3.7
|
image: alpine:3.7
|
||||||
container_name: compose_test_apline
|
container_name: compose_test_alpine
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,12 @@ const isContainerRunning = async name => new Promise((resolve, reject) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const repoTags = imageInfo => imageInfo.RepoTags || [];
|
||||||
|
|
||||||
const imageExists = async name => {
|
const imageExists = async name => {
|
||||||
const images = await docker.listImages();
|
const images = await docker.listImages();
|
||||||
|
|
||||||
const foundImage = images.findIndex(imageInfo => imageInfo.RepoTags.includes(name));
|
const foundImage = images.findIndex(imageInfo => repoTags(imageInfo).includes(name));
|
||||||
|
|
||||||
return foundImage > -1;
|
return foundImage > -1;
|
||||||
};
|
};
|
||||||
|
|
@ -32,7 +34,7 @@ const removeImagesStartingWith = async searchString => {
|
||||||
const images = await docker.listImages();
|
const images = await docker.listImages();
|
||||||
|
|
||||||
for (const image of images) {
|
for (const image of images) {
|
||||||
for (const repoTag of image.RepoTags) {
|
for (const repoTag of repoTags(image)) {
|
||||||
if (repoTag.startsWith(searchString)) {
|
if (repoTag.startsWith(searchString)) {
|
||||||
const dockerImage = docker.getImage(repoTag);
|
const dockerImage = docker.getImage(repoTag);
|
||||||
|
|
||||||
|
|
@ -44,14 +46,33 @@ const removeImagesStartingWith = async searchString => {
|
||||||
};
|
};
|
||||||
|
|
||||||
test('ensure container gets started', async assert => {
|
test('ensure container gets started', async assert => {
|
||||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||||
|
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||||
|
|
||||||
assert.true(await isContainerRunning('/compose_test_mongodb'));
|
assert.true(await isContainerRunning('/compose_test_mongodb'));
|
||||||
assert.end();
|
assert.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('ensure only single container gets started', async assert => {
|
||||||
|
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||||
|
await compose.upOne('alpine', { cwd: path.join(__dirname), log: true });
|
||||||
|
|
||||||
|
assert.true(await isContainerRunning('/compose_test_alpine'));
|
||||||
|
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||||
|
assert.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ensure only multiple containers get started', async assert => {
|
||||||
|
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||||
|
await compose.upMany([ 'alpine' ], { cwd: path.join(__dirname), log: true });
|
||||||
|
|
||||||
|
assert.true(await isContainerRunning('/compose_test_alpine'));
|
||||||
|
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||||
|
assert.end();
|
||||||
|
});
|
||||||
|
|
||||||
test('ensure container gets down', async assert => {
|
test('ensure container gets down', async assert => {
|
||||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||||
|
|
||||||
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||||
|
|
@ -59,7 +80,7 @@ test('ensure container gets down', async assert => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('ensure container gets stopped', async assert => {
|
test('ensure container gets stopped', async assert => {
|
||||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||||
await compose.stop({ cwd: path.join(__dirname), log: true });
|
await compose.stop({ cwd: path.join(__dirname), log: true });
|
||||||
|
|
||||||
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||||
|
|
@ -67,7 +88,7 @@ test('ensure container gets stopped', async assert => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('ensure container gets killed', async assert => {
|
test('ensure container gets killed', async assert => {
|
||||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||||
await compose.kill({ cwd: path.join(__dirname), log: true });
|
await compose.kill({ cwd: path.join(__dirname), log: true });
|
||||||
|
|
||||||
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||||
|
|
@ -79,7 +100,7 @@ test('ensure custom ymls are working', async assert => {
|
||||||
const cwd = path.join(__dirname);
|
const cwd = path.join(__dirname);
|
||||||
const log = true;
|
const log = true;
|
||||||
|
|
||||||
await compose.up({ cwd, log, config });
|
await compose.upAll({ cwd, log, config });
|
||||||
assert.true(await isContainerRunning('/compose_test_mongodb_2'));
|
assert.true(await isContainerRunning('/compose_test_mongodb_2'));
|
||||||
|
|
||||||
// config & [config] are the same thing, ensures that multiple configs are handled properly
|
// config & [config] are the same thing, ensures that multiple configs are handled properly
|
||||||
|
|
@ -104,7 +125,7 @@ test('ensure run and exec are working', async assert => {
|
||||||
|
|
||||||
const opts = { cwd: path.join(__dirname), log: false };
|
const opts = { cwd: path.join(__dirname), log: false };
|
||||||
|
|
||||||
await compose.up(opts);
|
await compose.upAll(opts);
|
||||||
|
|
||||||
assert.true(await isContainerRunning('/compose_test_mongodb'));
|
assert.true(await isContainerRunning('/compose_test_mongodb'));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue