Skip to content

Commit

Permalink
it builds wordpress
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Apr 6, 2021
1 parent aeef680 commit bdc4ea9
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 10 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# dockerode-compose
# dockerode-compose

docker-compose in Node.js using [dockerode](https://github.com/apocas/dockerode).

Work in progress...

## Installation

`npm install dockerode-compose`


### Getting started

To use `dockerode-compose` first you need to instantiate it:

``` js
var Dockerode = require('dockerode');
var DockerodeCompose = require('dockerode-compose');

var docker = new Dockerode();
var compose = new DockerodeCompose(docker);

(async () => {
var state = await compose.compose('./test/wordpress.yml', 'wordpress');
console.log(state);
})();
```

## Tests

* `docker pull ubuntu:latest` to prepare your system for the tests.
* Tests are implemented using `mocha` and `chai`. Run them with `npm test`.

## Examples

Check the examples folder for more specific use cases examples.

## License

Pedro Dias - [@pedromdias](https://twitter.com/pedromdias)

Licensed under the Apache license, version 2.0 (the "license"); You may not use this file except in compliance with the license. You may obtain a copy of the license at:

http://www.apache.org/licenses/LICENSE-2.0.html

Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. See the license for the specific language governing permissions and limitations under the license.
92 changes: 90 additions & 2 deletions lib/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Compose {
self.projectName = projectName;
try {
self.recipe = yaml.load(fs.readFileSync(file, 'utf8'));
console.log(self.recipe);
output.secrets = await self.loadSecrets();
output.volumes = await self.loadVolumes();
output.configs = await self.loadConfigs();
output.networks = await self.loadNetworks();
output.services = await self.loadServices();
return output;
} catch (e) {
throw e;
Expand Down Expand Up @@ -107,7 +107,8 @@ class Compose {
'Labels': network.labels,
'Attachable': network.attachable,
'EnableIPv6': network.enable_ipv6,
'Internal': network.internal
'Internal': network.internal,
'CheckDuplicate': true
};
if (network.name !== undefined) {
opts.Name = networkName;
Expand All @@ -132,8 +133,95 @@ class Compose {
throw err;
}
}

if (networks.length === 0) {
try {
await this.docker.createNetwork({ 'Name': this.projectName + '_default', 'CheckDuplicate': true });
} catch (err) {
throw err;
}
}

return networks;
}


async loadServices() {
var services = [];
var serviceNames = Object.keys(this.recipe.services || []);
for (var serviceName of serviceNames) {
var service = this.recipe.services[serviceName];

var opts = {
'name': this.projectName + '_' + serviceName,
'Image': service.image,
'HostConfig': this.buildHostConfig(service),
'Env': this.buildEnvVars(service),
"NetworkingConfig": {
"EndpointsConfig": {
}
}
};

opts.NetworkingConfig.EndpointsConfig[this.projectName + '_default'] = {
"IPAMConfig": {},
"Links": [],
"Aliases": [
serviceName,
]
};

if (service.volumes) {
opts['Volumes'] = {};
for (var volume of service.volumes) {
var v = volume.split(':');
opts['Volumes'][v[1]] = {};
}
}

if (service.name !== undefined) {
opts.Name = serviceName;
}
try {
var container = await this.docker.createContainer(opts);
await container.start();
services.push(container);
} catch (err) {
throw err;
}
}
return services;
}

buildHostConfig(service) {
var output = {
'RestartPolicy': { 'Name': service.restart }
};

if (service.volumes) {
output['Binds'] = service.volumes;
}

if (service.ports && service.ports.length > 0) {
var ports = {};
for (var portb of service.ports) {
var p = portb.split(':');
ports[p[1] + '/tcp'] = [{ 'HostPort': p[0] }]
}
output['PortBindings'] = ports;
}

return output;
}
buildEnvVars(service) {
var output = [];

var envsNames = Object.keys(service.environment || []);
for (var envName of envsNames) {
output.push(envName + '=' + service.environment[envName])
}
return output;
}
}

module.exports = Compose;
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "dockerode-compose",
"version": "1.0.0",
"version": "1.0.1",
"description": "docker-compose in nodejs using dockerode",
"main": "lib/compose.js",
"main": "./lib/compose.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/apocas/dockerode-compose.git"
},
"author": "",
"license": "ISC",
"author": "Pedro Dias <[email protected]>",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/apocas/dockerode-compose/issues"
},
"keywords": [
"docker",
"docker-compose"
],
"homepage": "https://github.com/apocas/dockerode-compose#readme",
"dependencies": {
"dockerode": "^3.2.1",
Expand Down

0 comments on commit bdc4ea9

Please sign in to comment.