mirror of
https://github.com/valentineus/simple-container.git
synced 2025-04-28 00:41:24 +03:00
Realization of the basic functionality of creating the container
This commit is contained in:
parent
b36916c7f5
commit
15f294f270
77
README.md
77
README.md
@ -5,7 +5,7 @@
|
|||||||
[](https://www.codacy.com/app/valentineus/simple-container/files)
|
[](https://www.codacy.com/app/valentineus/simple-container/files)
|
||||||
[](https://gitter.im/valentineus/simple-container)
|
[](https://gitter.im/valentineus/simple-container)
|
||||||
|
|
||||||
Simple and fast work with the Docker container
|
Simple and fast work with the Docker container.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
```bash
|
```bash
|
||||||
@ -13,16 +13,85 @@ npm install --save simple-container
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Using
|
## Using
|
||||||
|
A simple example:
|
||||||
```JavaScript
|
```JavaScript
|
||||||
import Container from 'simple-container';
|
import Container from 'simple-container';
|
||||||
|
|
||||||
var container = new Container({});
|
var container = new Container();
|
||||||
container.create();
|
|
||||||
|
/* Create the container */
|
||||||
|
container.create('postgres:alpine');
|
||||||
|
|
||||||
|
/* Start the container */
|
||||||
|
container.start();
|
||||||
|
|
||||||
|
/* Stop and destroy the container */
|
||||||
|
container.destroy();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Remote connection
|
||||||
|
By default, the local service is used.
|
||||||
|
|
||||||
|
For example, if you want to use connection settings:
|
||||||
|
```JavaScript
|
||||||
|
var container = new Container({
|
||||||
|
host: '127.0.0.1',
|
||||||
|
port: 3000
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Examples and details in
|
||||||
|
[the documentation](https://github.com/apocas/dockerode#getting-started).
|
||||||
|
|
||||||
|
### Creating a container
|
||||||
|
You can create a container in two ways.
|
||||||
|
|
||||||
|
A simple way - to specify the name of the image:
|
||||||
|
```JavaScript
|
||||||
|
container.create('hello-world:latest');
|
||||||
|
```
|
||||||
|
|
||||||
|
Another way - to provide an object with parameters:
|
||||||
|
```JavaScript
|
||||||
|
container.create({
|
||||||
|
Image: 'postgres:alpine',
|
||||||
|
Env: ['POSTGRES_PASSWORD = password'],
|
||||||
|
Ports: [{
|
||||||
|
IP: '0.0.0.0',
|
||||||
|
PrivatePort: 5432,
|
||||||
|
PublicPort: 5432,
|
||||||
|
Type: 'tcp'
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Examples and details in
|
||||||
|
[the documentation](https://github.com/apocas/dockerode#manipulating-a-container).
|
||||||
|
|
||||||
|
An additional example of obtaining a private image:
|
||||||
|
```JavaScript
|
||||||
|
var auth = {
|
||||||
|
username: 'username',
|
||||||
|
password: 'password',
|
||||||
|
email: 'email@example.org',
|
||||||
|
serveraddress: 'https://index.docker.io/v1'
|
||||||
|
};
|
||||||
|
|
||||||
|
container.create({
|
||||||
|
Image: 'project:latest',
|
||||||
|
authconfig: auth
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Details in
|
||||||
|
[the documentation](https://github.com/apocas/dockerode#pull-from-private-repos).
|
||||||
|
|
||||||
|
## Debugging
|
||||||
|
Use the `DEBUG` variable with the `container` option.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
[](https://github.com/eslint/eslint)
|
[](https://github.com/eslint/eslint)
|
||||||
|
|
||||||
[MIT](LICENSE.md).
|
[MIT](LICENSE.md).
|
||||||
Copyright (c)
|
Copyright (c)
|
||||||
[Valentin Popov](https://valentineus.link/).
|
[Valentin Popov](mailto:info@valentineus.link).
|
@ -26,7 +26,9 @@
|
|||||||
"mocha": "^4.0.0"
|
"mocha": "^4.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ancient-funicular": "^3.0.0"
|
"ancient-funicular": "^3.0.0",
|
||||||
|
"debug": "^3.1.0",
|
||||||
|
"dockerode": "^2.5.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha src/tests/index.js --require babel-core/register",
|
"test": "mocha src/tests/index.js --require babel-core/register",
|
||||||
|
140
src/lib/index.js
140
src/lib/index.js
@ -1,3 +1,141 @@
|
|||||||
import { Item } from 'ancient-funicular';
|
import { Item } from 'ancient-funicular';
|
||||||
|
import createDebug from 'debug';
|
||||||
|
import Docker from 'dockerode';
|
||||||
|
import { statSync } from 'fs';
|
||||||
|
|
||||||
export class Container extends Item {};
|
/**
|
||||||
|
* @class Container
|
||||||
|
*/
|
||||||
|
export default class Container extends Item {
|
||||||
|
/**
|
||||||
|
* @constructs Container
|
||||||
|
* @param {Object} options
|
||||||
|
*/
|
||||||
|
constructor(options) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @type {Object}
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
this._docker = null;
|
||||||
|
|
||||||
|
if (this._isObject(options)) {
|
||||||
|
this._docker = new Docker(options);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
|
||||||
|
var stats = statSync(socket);
|
||||||
|
|
||||||
|
if (!stats.isSocket()) {
|
||||||
|
throw new Error('');
|
||||||
|
}
|
||||||
|
|
||||||
|
this._docker = new Docker({
|
||||||
|
socketPath: socket
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {Object|String} options
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
create(options) {
|
||||||
|
var self = this;
|
||||||
|
var name = null;
|
||||||
|
|
||||||
|
if (!self._isString(options) && !self._isObject(options)) {
|
||||||
|
throw new Error('The variable \'options\' is not correct.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self._isString(options)) {
|
||||||
|
name = options;
|
||||||
|
options = { Image: name };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self._isObject(options)) {
|
||||||
|
name = options.Image;
|
||||||
|
}
|
||||||
|
|
||||||
|
self._pullImage(name, (error) => {
|
||||||
|
self._handlerError(error);
|
||||||
|
self._createContainer(options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
destroy() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {String} name
|
||||||
|
* @param {Function} callback
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
_pullImage(name, callback) {
|
||||||
|
var self = this;
|
||||||
|
self._docker.pull(name).then(stream => {
|
||||||
|
self._docker.modem.followProgress(stream, callback, self.debug);
|
||||||
|
}).catch(error => self._handlerError(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {Object} options
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
_createContainer(options) {
|
||||||
|
var self = this;
|
||||||
|
self._docker.createContainer(options).then(container => {
|
||||||
|
/* @todo Регистрация класса */
|
||||||
|
}).catch(error => self._handlerError(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {*} value - The variable to check
|
||||||
|
* @returns {Boolean} Result of checking
|
||||||
|
* @description Checks the type of the variable
|
||||||
|
*/
|
||||||
|
_isString(value) {
|
||||||
|
return typeof value === 'string';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {*} value - The variable to check
|
||||||
|
* @returns {Boolean} Result of checking
|
||||||
|
* @description Checks the type of the variable
|
||||||
|
*/
|
||||||
|
_isObject(value) {
|
||||||
|
return typeof value === 'object';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {*} error
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
_handlerError(error) {
|
||||||
|
if (!!error) {
|
||||||
|
throw new Error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {*}
|
||||||
|
* @description
|
||||||
|
*/
|
||||||
|
debug() {
|
||||||
|
var args = Array.prototype.slice.call(arguments);
|
||||||
|
var debug = createDebug('container');
|
||||||
|
debug.apply(null, args);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user