Skip to content

Commit bdc4ea9

Browse files
committed
it builds wordpress
1 parent aeef680 commit bdc4ea9

File tree

4 files changed

+147
-10
lines changed

4 files changed

+147
-10
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
# dockerode-compose
1+
# dockerode-compose
2+
3+
docker-compose in Node.js using [dockerode](https://github.com/apocas/dockerode).
4+
5+
Work in progress...
6+
7+
## Installation
8+
9+
`npm install dockerode-compose`
10+
11+
12+
### Getting started
13+
14+
To use `dockerode-compose` first you need to instantiate it:
15+
16+
``` js
17+
var Dockerode = require('dockerode');
18+
var DockerodeCompose = require('dockerode-compose');
19+
20+
var docker = new Dockerode();
21+
var compose = new DockerodeCompose(docker);
22+
23+
(async () => {
24+
var state = await compose.compose('./test/wordpress.yml', 'wordpress');
25+
console.log(state);
26+
})();
27+
```
28+
29+
## Tests
30+
31+
* `docker pull ubuntu:latest` to prepare your system for the tests.
32+
* Tests are implemented using `mocha` and `chai`. Run them with `npm test`.
33+
34+
## Examples
35+
36+
Check the examples folder for more specific use cases examples.
37+
38+
## License
39+
40+
Pedro Dias - [@pedromdias](https://twitter.com/pedromdias)
41+
42+
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:
43+
44+
http://www.apache.org/licenses/LICENSE-2.0.html
45+
46+
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.

lib/compose.js

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class Compose {
1515
self.projectName = projectName;
1616
try {
1717
self.recipe = yaml.load(fs.readFileSync(file, 'utf8'));
18-
console.log(self.recipe);
1918
output.secrets = await self.loadSecrets();
2019
output.volumes = await self.loadVolumes();
2120
output.configs = await self.loadConfigs();
2221
output.networks = await self.loadNetworks();
22+
output.services = await self.loadServices();
2323
return output;
2424
} catch (e) {
2525
throw e;
@@ -107,7 +107,8 @@ class Compose {
107107
'Labels': network.labels,
108108
'Attachable': network.attachable,
109109
'EnableIPv6': network.enable_ipv6,
110-
'Internal': network.internal
110+
'Internal': network.internal,
111+
'CheckDuplicate': true
111112
};
112113
if (network.name !== undefined) {
113114
opts.Name = networkName;
@@ -132,8 +133,95 @@ class Compose {
132133
throw err;
133134
}
134135
}
136+
137+
if (networks.length === 0) {
138+
try {
139+
await this.docker.createNetwork({ 'Name': this.projectName + '_default', 'CheckDuplicate': true });
140+
} catch (err) {
141+
throw err;
142+
}
143+
}
144+
135145
return networks;
136146
}
147+
148+
149+
async loadServices() {
150+
var services = [];
151+
var serviceNames = Object.keys(this.recipe.services || []);
152+
for (var serviceName of serviceNames) {
153+
var service = this.recipe.services[serviceName];
154+
155+
var opts = {
156+
'name': this.projectName + '_' + serviceName,
157+
'Image': service.image,
158+
'HostConfig': this.buildHostConfig(service),
159+
'Env': this.buildEnvVars(service),
160+
"NetworkingConfig": {
161+
"EndpointsConfig": {
162+
}
163+
}
164+
};
165+
166+
opts.NetworkingConfig.EndpointsConfig[this.projectName + '_default'] = {
167+
"IPAMConfig": {},
168+
"Links": [],
169+
"Aliases": [
170+
serviceName,
171+
]
172+
};
173+
174+
if (service.volumes) {
175+
opts['Volumes'] = {};
176+
for (var volume of service.volumes) {
177+
var v = volume.split(':');
178+
opts['Volumes'][v[1]] = {};
179+
}
180+
}
181+
182+
if (service.name !== undefined) {
183+
opts.Name = serviceName;
184+
}
185+
try {
186+
var container = await this.docker.createContainer(opts);
187+
await container.start();
188+
services.push(container);
189+
} catch (err) {
190+
throw err;
191+
}
192+
}
193+
return services;
194+
}
195+
196+
buildHostConfig(service) {
197+
var output = {
198+
'RestartPolicy': { 'Name': service.restart }
199+
};
200+
201+
if (service.volumes) {
202+
output['Binds'] = service.volumes;
203+
}
204+
205+
if (service.ports && service.ports.length > 0) {
206+
var ports = {};
207+
for (var portb of service.ports) {
208+
var p = portb.split(':');
209+
ports[p[1] + '/tcp'] = [{ 'HostPort': p[0] }]
210+
}
211+
output['PortBindings'] = ports;
212+
}
213+
214+
return output;
215+
}
216+
buildEnvVars(service) {
217+
var output = [];
218+
219+
var envsNames = Object.keys(service.environment || []);
220+
for (var envName of envsNames) {
221+
output.push(envName + '=' + service.environment[envName])
222+
}
223+
return output;
224+
}
137225
}
138226

139227
module.exports = Compose;

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
{
22
"name": "dockerode-compose",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "docker-compose in nodejs using dockerode",
5-
"main": "lib/compose.js",
5+
"main": "./lib/compose.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
99
"repository": {
1010
"type": "git",
1111
"url": "git+https://github.com/apocas/dockerode-compose.git"
1212
},
13-
"author": "",
14-
"license": "ISC",
13+
"author": "Pedro Dias <[email protected]>",
14+
"license": "Apache-2.0",
1515
"bugs": {
1616
"url": "https://github.com/apocas/dockerode-compose/issues"
1717
},
18+
"keywords": [
19+
"docker",
20+
"docker-compose"
21+
],
1822
"homepage": "https://github.com/apocas/dockerode-compose#readme",
1923
"dependencies": {
2024
"dockerode": "^3.2.1",

0 commit comments

Comments
 (0)