├── .github
└── workflows
│ └── npm.yml
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── docs
├── assets
│ ├── css
│ │ ├── main.css
│ │ └── main.css.map
│ ├── images
│ │ ├── icons.png
│ │ ├── icons@2x.png
│ │ ├── widgets.png
│ │ └── widgets@2x.png
│ └── js
│ │ ├── main.js
│ │ └── search.js
├── classes
│ ├── _container_.container.html
│ ├── _container_.containerfs.html
│ ├── _container_.default.html
│ ├── _container_.exec.html
│ ├── _container_.execmanager.html
│ ├── _docker_.docker.html
│ ├── _image_.default.html
│ ├── _image_.image.html
│ ├── _network_.default.html
│ ├── _network_.network.html
│ ├── _node_.default.html
│ ├── _node_.node.html
│ ├── _plugin_.plugin.html
│ ├── _secret_.default.html
│ ├── _secret_.secret.html
│ ├── _service_.default.html
│ ├── _service_.service.html
│ ├── _swarm_.swarm.html
│ ├── _task_.default.html
│ ├── _task_.task.html
│ ├── _volume_.default.html
│ └── _volume_.volume.html
├── globals.html
├── index.html
└── modules
│ ├── _container_.html
│ ├── _docker_.html
│ ├── _image_.html
│ ├── _network_.html
│ ├── _node_.html
│ ├── _plugin_.html
│ ├── _secret_.html
│ ├── _service_.html
│ ├── _swarm_.html
│ ├── _task_.html
│ └── _volume_.html
├── examples
├── commands_and_kill.js
├── export_container.js
├── fetch_events.js
├── file_system.js
├── list_containers.js
├── logs_container.js
├── manage_containers.js
├── manage_images.js
├── pull_and_check_image.js
└── top_containers.js
├── install_docker.sh
├── jsdoc.json
├── package-lock.json
├── package.json
├── src
├── container.ts
├── docker.ts
├── image.ts
├── network.ts
├── node.ts
├── plugin.ts
├── secret.ts
├── service.ts
├── swarm.ts
├── task.ts
├── types
│ └── container.ts
└── volume.ts
├── test.sh
├── test
├── container.js
├── docker.js
├── image.js
├── network.js
├── swarm.js
├── test-load.tar
├── test.tar
└── volume.js
├── tsconfig.json
├── tslint.json
└── yarn.lock
/.github/workflows/npm.yml:
--------------------------------------------------------------------------------
1 | name: Publish Package to npmjs
2 |
3 | on:
4 | release:
5 | types: [created]
6 |
7 | jobs:
8 | publish:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v3
12 | - uses: actions/setup-node@v3
13 | with:
14 | node-version: '16.x'
15 | registry-url: 'https://registry.npmjs.org'
16 | - run: npm ci
17 | - run: npm run build
18 | - run: npm publish
19 | env:
20 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Runtime data
7 | pids
8 | *.pid
9 | *.seed
10 |
11 | # Directory for instrumented libs generated by jscoverage/JSCover
12 | lib-cov
13 |
14 | # Coverage directory used by tools like istanbul
15 | coverage
16 |
17 | # nyc test coverage
18 | .nyc_output
19 |
20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21 | .grunt
22 |
23 | # node-waf configuration
24 | .lock-wscript
25 |
26 | # Compiled binary addons (http://nodejs.org/api/addons.html)
27 | build/Release
28 | lib
29 |
30 | # Dependency directories
31 | node_modules
32 | jspm_packages
33 |
34 | # Optional npm cache directory
35 | .npm
36 |
37 | # Optional REPL history
38 | .node_repl_history
39 |
40 | *.swp
41 |
42 | test_folder
43 |
44 | # Editor
45 | .vscode
46 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js: "6"
3 | dist: trusty
4 | sudo: required
5 | group: deprecated-2017Q2
6 | env:
7 | - DOCKER_VERSION=17.06.0
8 | before_script:
9 | - chmod +x install_docker.sh
10 | - sudo ./install_docker.sh ${DOCKER_VERSION}
11 | - npm run build
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # docker-api
2 | [](https://travis-ci.org/AgustinCB/docker-api)
3 |
4 | Docker Remote API driver for node.js. It uses the same modem than [dockerode](https://github.com/apocas/dockerode), but the interface is promisified and with a different syntax.
5 |
6 | Support for:
7 |
8 | * **streams**
9 | * **stream demux**
10 | * **entities**
11 | * **run**
12 | * **tests**
13 | * **promises**
14 | * **full es6 support**
15 |
16 | The current status of the package is in beta state. This module covers the full [API reference](https://docs.docker.com/engine/api/v1.30), including experimental stuff such as plugins.
17 |
18 | Check the [reference](https://agustincb.github.io/docker-api/) and the [tests](https://github.com/AgustinCB/docker-api/tree/master/test) for full examples.
19 |
20 | ## Installation
21 | ```console
22 | $ npm install node-docker-api
23 | ```
24 |
25 | ## Usage
26 |
27 | You can find more into the [examples folder](https://github.com/AgustinCB/docker-api/tree/master/examples)
28 |
29 | ### Create, start, stop, restart and remove a container
30 |
31 | ``` js
32 | 'use strict';
33 | const {Docker} = require('node-docker-api');
34 |
35 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
36 |
37 | docker.container.create({
38 | Image: 'ubuntu',
39 | name: 'test'
40 | })
41 | .then(container => container.start())
42 | .then(container => container.stop())
43 | .then(container => container.restart())
44 | .then(container => container.delete({ force: true }))
45 | .catch(error => console.log(error));
46 | ```
47 |
48 | ### List, inspect and top containers
49 |
50 | ``` js
51 | 'use strict';
52 | const {Docker} = require('node-docker-api');
53 |
54 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
55 |
56 | // List
57 | docker.container.list()
58 | // Inspect
59 | .then(containers => containers[0].status())
60 | .then(container => container.top())
61 | .then(processes => console.log(processes))
62 | .catch(error => console.log(error));
63 | ```
64 |
65 | ### List, inspect and stat containers
66 |
67 | ``` js
68 | 'use strict';
69 | const {Docker} = require('node-docker-api');
70 |
71 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
72 |
73 | // List
74 | docker.container.list()
75 | // Inspect
76 | .then(containers => containers[0].status())
77 | .then(container => container.stats())
78 | .then(stats => {
79 | stats.on('data', stat => console.log('Stats: ', stat.toString()))
80 | stats.on('error', err => console.log('Error: ', err))
81 | })
82 | .catch(error => console.log(error));
83 | ```
84 |
85 | ### Get logs of a container
86 |
87 | ```js
88 | 'use strict';
89 | const {Docker} = require('node-docker-api');
90 |
91 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
92 | let container;
93 |
94 | docker.container.create({
95 | Image: 'ubuntu',
96 | name: 'test'
97 | })
98 | .then(container => container.logs({
99 | follow: true,
100 | stdout: true,
101 | stderr: true
102 | }))
103 | .then(stream => {
104 | stream.on('data', info => console.log(info))
105 | stream.on('error', err => console.log(err))
106 | })
107 | .catch(error => console.log(error));
108 | ```
109 |
110 | ### Export a container
111 |
112 | ``` js
113 | const {Docker} = require('node-docker-api');
114 | const fs = require('fs');
115 |
116 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
117 | let container;
118 |
119 | docker.container.create({
120 | Image: 'ubuntu',
121 | name: 'test'
122 | })
123 | .then(container => container.start())
124 | .then(container => container.export())
125 | .then(content => {
126 | const file = fs.createWriteStream("container.tar");
127 | file.end(content)
128 | })
129 | .catch(error => console.log(error));
130 | ```
131 |
132 | ### Manipulate file system in a container
133 |
134 | ``` js
135 | 'use strict';
136 | const fs = require('fs');
137 | const {Docker} = require('node-docker-api');
138 |
139 | const promisifyStream = stream => new Promise((resolve, reject) => {
140 | stream.on('data', data => console.log(data.toString()))
141 | stream.on('end', resolve)
142 | stream.on('error', reject)
143 | });
144 |
145 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
146 | let container;
147 |
148 | docker.container.create({
149 | Image: 'ubuntu',
150 | Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
151 | name: 'test'
152 | })
153 | .then(container => container.start())
154 | .then(_container => {
155 | container = _container
156 | return _container.fs.put('./file.tar', {
157 | path: 'root'
158 | })
159 | })
160 | .then(stream => promisifyStream(stream))
161 | .then(() => container.fs.get({ path: '/var/log/dmesg' }))
162 | .then(stream => {
163 | const file = fs.createWriteStream("file.jpg");
164 | stream.pipe(file);
165 | return promisifyStream(stream);
166 | })
167 | .then(() => container.status())
168 | .then(container => container.stop())
169 | .catch(error => console.log(error));
170 | ```
171 |
172 | ### Execute commands and kill containers
173 |
174 | ``` js
175 | 'use strict';
176 | const {Docker} = require('node-docker-api');
177 |
178 | const promisifyStream = stream => new Promise((resolve, reject) => {
179 | stream.on('data', data => console.log(data.toString()))
180 | stream.on('end', resolve)
181 | stream.on('error', reject)
182 | });
183 |
184 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
185 | let _container;
186 |
187 | docker.container.create({
188 | Image: 'ubuntu',
189 | Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
190 | name: 'test'
191 | })
192 | .then(container => container.start())
193 | .then(container => {
194 | _container = container
195 | return container.exec.create({
196 | AttachStdout: true,
197 | AttachStderr: true,
198 | Cmd: [ 'echo', 'test' ]
199 | })
200 | })
201 | .then(exec => {
202 | return exec.start({ Detach: false })
203 | })
204 | .then(stream => promisifyStream(stream))
205 | .then(() => _container.kill())
206 | .catch(error => console.log(error));
207 | ```
208 |
209 | ### Build, inspect and remove an image
210 |
211 | ``` js
212 | 'use strict';
213 | const {Docker} = require('node-docker-api');
214 | const tar = require('tar-fs');
215 |
216 | const promisifyStream = stream => new Promise((resolve, reject) => {
217 | stream.on('data', data => console.log(data.toString()))
218 | stream.on('end', resolve)
219 | stream.on('error', reject)
220 | });
221 |
222 | const docker = new Docker({ socketPath: '/var/run/docker.sock' });
223 |
224 | var tarStream = tar.pack('/path/to/Dockerfile')
225 | docker.image.build(tarStream, {
226 | t: 'testimg'
227 | })
228 | .then(stream => promisifyStream(stream))
229 | .then(() => docker.image.get('testimg').status())
230 | .then(image => image.remove())
231 | .catch(error => console.log(error));
232 | ```
233 |
234 | ### Pull and check history of an image
235 |
236 | ``` js
237 | 'use strict';
238 | const {Docker} = require('node-docker-api');
239 |
240 | const promisifyStream = (stream) => new Promise((resolve, reject) => {
241 | stream.on('data', (d) => console.log(d.toString()))
242 | stream.on('end', resolve)
243 | stream.on('error', reject)
244 | })
245 |
246 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
247 |
248 | return docker.image.create({}, { fromImage: 'ubuntu', tag: 'latest' })
249 | .then(stream => promisifyStream(stream))
250 | .then(() => docker.image.get('ubuntu').status())
251 | .then(image => image.history())
252 | .then(events => console.log(events))
253 | .catch(error => console.log(error))
254 | ```
255 |
256 | ### Fetch events from docker
257 |
258 | ``` js
259 | 'use strict'
260 | const fs = require('fs');
261 | const {Docker} = require('node-docker-api');
262 |
263 | const promisifyStream = stream => new Promise((resolve, reject) => {
264 | stream.on('data', data => console.log(data.toString()))
265 | stream.on('end', resolve)
266 | stream.on('error', reject)
267 | })
268 |
269 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
270 |
271 | docker.events({
272 | since: ((new Date().getTime() / 1000) - 60).toFixed(0)
273 | })
274 | .then(stream => promisifyStream(stream))
275 | .catch(error => console.log(error))
276 | ```
277 |
--------------------------------------------------------------------------------
/docs/assets/images/icons.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AgustinCB/docker-api/2098bd424e71ccdde8f379edac6acc2b47b317b2/docs/assets/images/icons.png
--------------------------------------------------------------------------------
/docs/assets/images/icons@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AgustinCB/docker-api/2098bd424e71ccdde8f379edac6acc2b47b317b2/docs/assets/images/icons@2x.png
--------------------------------------------------------------------------------
/docs/assets/images/widgets.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AgustinCB/docker-api/2098bd424e71ccdde8f379edac6acc2b47b317b2/docs/assets/images/widgets.png
--------------------------------------------------------------------------------
/docs/assets/images/widgets@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AgustinCB/docker-api/2098bd424e71ccdde8f379edac6acc2b47b317b2/docs/assets/images/widgets@2x.png
--------------------------------------------------------------------------------
/docs/globals.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
59 |
node-docker-api
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 | Index
68 |
69 |
70 |
71 | External modules
72 |
85 |
86 |
87 |
88 |
89 |
90 |
136 |
137 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_container_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "container" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "container"
63 |
64 |
65 |
66 |
150 |
209 |
210 |
Generated using TypeDoc
211 |
212 |
213 |
214 |
215 |
216 |
--------------------------------------------------------------------------------
/docs/modules/_docker_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "docker" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "docker"
63 |
64 |
65 |
66 |
134 |
193 |
194 |
Generated using TypeDoc
195 |
196 |
197 |
198 |
199 |
200 |
--------------------------------------------------------------------------------
/docs/modules/_image_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "image" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "image"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_network_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "network" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "network"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_node_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "node" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "node"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_plugin_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "plugin" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "plugin"
63 |
64 |
65 |
66 |
134 |
193 |
194 |
Generated using TypeDoc
195 |
196 |
197 |
198 |
199 |
200 |
--------------------------------------------------------------------------------
/docs/modules/_secret_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "secret" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "secret"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_service_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "service" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "service"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_swarm_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "swarm" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "swarm"
63 |
64 |
65 |
66 |
134 |
193 |
194 |
Generated using TypeDoc
195 |
196 |
197 |
198 |
199 |
200 |
--------------------------------------------------------------------------------
/docs/modules/_task_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "task" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "task"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/docs/modules/_volume_.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | "volume" | node-docker-api
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | - Preparing search index...
23 | - The search index is not available
24 |
25 |
node-docker-api
26 |
27 |
49 |
50 |
51 |
52 |
53 |
54 |
62 |
External module "volume"
63 |
64 |
65 |
66 |
138 |
197 |
198 |
Generated using TypeDoc
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/examples/commands_and_kill.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 | const Docker = require('node-docker-api').Docker
3 |
4 | const promisifyStream = (stream) => new Promise((resolve, reject) => {
5 | stream.on('data', (d) => console.log(d.toString()))
6 | stream.on('end', resolve)
7 | stream.on('error', reject)
8 | })
9 |
10 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
11 | let _container
12 |
13 | docker.container.create({
14 | Image: 'ubuntu',
15 | Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
16 | name: 'test'
17 | })
18 | .then((container) => container.start())
19 | .then((container) => {
20 | _container = container
21 | return container.exec.create({
22 | AttachStdout: true,
23 | AttachStderr: true,
24 | Cmd: [ 'echo', 'test' ]
25 | })
26 | })
27 | .then((exec) => {
28 | return exec.start({ Detach: false })
29 | })
30 | .then((stream) => promisifyStream(stream))
31 | .then(() => _container.kill())
32 | .catch((error) => console.log(error))
33 |
--------------------------------------------------------------------------------
/examples/export_container.js:
--------------------------------------------------------------------------------
1 | const Docker = require('node-docker-api').Docker,
2 | fs = require('fs')
3 |
4 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
5 | let container
6 |
7 | docker.container.create({
8 | Image: 'ubuntu',
9 | name: 'test'
10 | })
11 | .then((container) => container.start())
12 | .then((container) => container.export())
13 | .then((content) => {
14 | const file = fs.createWriteStream("container.tar");
15 | file.end(content)
16 | })
17 | .catch((error) => console.log(error))
18 |
--------------------------------------------------------------------------------
/examples/fetch_events.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Docker = require('node-docker-api').Docker,
4 | fs = require('fs')
5 |
6 | const promisifyStream = (stream) => new Promise((resolve, reject) => {
7 | stream.on('data', (d) => console.log(d.toString()))
8 | stream.on('end', resolve)
9 | stream.on('error', reject)
10 | })
11 |
12 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
13 |
14 | docker.events({
15 | since: ((new Date().getTime() / 1000) - 60).toFixed(0)
16 | })
17 | .then((stream) => promisifyStream(stream))
18 | .catch((error) => console.log(error))
19 |
--------------------------------------------------------------------------------
/examples/file_system.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 | const Docker = require('node-docker-api').Docker,
3 | fs = require('fs')
4 |
5 | const promisifyStream = (stream) => new Promise((resolve, reject) => {
6 | stream.on('data', (d) => console.log(d.toString()))
7 | stream.on('end', resolve)
8 | stream.on('error', reject)
9 | })
10 |
11 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
12 | let container
13 |
14 | docker.container.create({
15 | Image: 'ubuntu',
16 | Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
17 | name: 'test'
18 | })
19 | .then((container) => container.start())
20 | .then((_container) => {
21 | container = _container
22 | return _container.fs.put('./file.tar', {
23 | path: 'root'
24 | })
25 | })
26 | .then((stream) => promisifyStream(stream))
27 | .then(() => container.fs.get({ path: '/var/log/dmesg' }))
28 | .then((stream) => {
29 | const file = fs.createWriteStream("file.jpg");
30 | stream.pipe(file)
31 | return promisifyStream(stream)
32 | })
33 | .then(() => container.status())
34 | .then((container) => container.stop())
35 | .catch((error) => console.log(error))
36 |
--------------------------------------------------------------------------------
/examples/list_containers.js:
--------------------------------------------------------------------------------
1 | const Docker = require('node-docker-api').Docker
2 |
3 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
4 |
5 | // List
6 | docker.container.list()
7 | // Inspect
8 | .then((containers) => containers[0].status())
9 | .then((container) => container.stats())
10 | .then((stats) => {
11 | stats.on('data', (stat) => console.log('Stats: ', stat.toString()))
12 | stats.on('error', (err) => console.log('Error: ', err))
13 | })
14 | .catch((error) => console.log(error))
15 |
--------------------------------------------------------------------------------
/examples/logs_container.js:
--------------------------------------------------------------------------------
1 | const Docker = require('node-docker-api').Docker
2 |
3 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
4 | let container
5 |
6 | docker.container.create({
7 | Image: 'ubuntu',
8 | name: 'test'
9 | })
10 | .then((container) => container.logs({
11 | follow: true,
12 | stdout: true,
13 | stderr: true
14 | }))
15 | .then((stream) => {
16 | stream.on('data', (info) => console.log(info))
17 | stream.on('error', (err) => console.log(err))
18 | })
19 | .catch((error) => console.log(error))
20 |
--------------------------------------------------------------------------------
/examples/manage_containers.js:
--------------------------------------------------------------------------------
1 | const Docker = require('node-docker-api').Docker
2 |
3 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
4 |
5 | docker.container.create({
6 | Image: 'ubuntu',
7 | name: 'test'
8 | })
9 | .then((container) => container.start())
10 | .then((container) => container.stop())
11 | .then((container) => container.restart())
12 | .then((container) => container.delete({ force: true }))
13 | .catch((error) => console.log(error))
14 |
--------------------------------------------------------------------------------
/examples/manage_images.js:
--------------------------------------------------------------------------------
1 | const Docker = require('node-docker-api').Docker,
2 | tar = require('tar-fs')
3 |
4 | const promisifyStream = (stream) => new Promise((resolve, reject) => {
5 | stream.on('data', (d) => console.log(d.toString()))
6 | stream.on('end', resolve)
7 | stream.on('error', reject)
8 | })
9 |
10 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
11 |
12 | var tarStream = tar.pack('/path/to/Dockerfile')
13 | docker.image.build(tarStream, {
14 | t: 'testimg'
15 | })
16 | .then((stream) => promisifyStream(stream))
17 | .then(() => docker.image.get('testimg').status())
18 | .then((image) => image.remove())
19 | .catch((error) => console.log(error))
20 |
--------------------------------------------------------------------------------
/examples/pull_and_check_image.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | const Docker = require('node-docker-api').Docker
4 |
5 | const promisifyStream = (stream) => new Promise((resolve, reject) => {
6 | stream.on('data', (d) => console.log(d.toString()))
7 | stream.on('end', resolve)
8 | stream.on('error', reject)
9 | })
10 |
11 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
12 |
13 | return docker.image.create({}, { fromImage: 'ubuntu', tag: 'latest' })
14 | .then((stream) => promisifyStream(stream))
15 | .then(() => docker.image.get('ubuntu').status())
16 | .then((image) => image.history())
17 | .then((events) => console.log(events))
18 | .catch((error) => console.log(error))
19 |
--------------------------------------------------------------------------------
/examples/top_containers.js:
--------------------------------------------------------------------------------
1 | const Docker = require('node-docker-api').Docker
2 |
3 | const docker = new Docker({ socketPath: '/var/run/docker.sock' })
4 |
5 | // List
6 | docker.container.list()
7 | // Inspect
8 | .then((containers) => containers[0].status())
9 | .then((container) => container.top())
10 | .then((processes) => console.log(processes))
11 | .catch((error) => console.log(error))
12 |
--------------------------------------------------------------------------------
/install_docker.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -euo pipefail
3 |
4 | # argv[0]
5 | DOCKER_VERSION=$1
6 |
7 | service docker stop
8 | apt-get -y --purge remove docker docker-engine docker.io
9 |
10 | apt-get update
11 | apt-get install -y apt-transport-https ca-certificates curl software-properties-common
12 |
13 | # install gpg key for docker rpo
14 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
15 | apt-key fingerprint 0EBFCD88
16 |
17 | # enable docker repo
18 | add-apt-repository \
19 | "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
20 | $(lsb_release -cs) \
21 | stable"
22 | apt-get update
23 | apt-cache gencaches
24 |
25 | # install package
26 | apt-cache policy docker-ce
27 | apt-get -y --force-yes install docker-ce=${DOCKER_VERSION}~ce-0~ubuntu
28 | echo 'DOCKER_OPTS="-H unix:///var/run/docker.sock --pidfile=/var/run/docker.pid --experimental=true"' > /etc/default/docker
29 | #cat /etc/default/docker
30 |
31 | #service docker stop
32 |
33 | #/usr/bin/dockerd -H unix:///var/run/docker.sock --pidfile=/var/run/docker.pid --experimental=true &
34 |
35 | docker --version
36 |
--------------------------------------------------------------------------------
/jsdoc.json:
--------------------------------------------------------------------------------
1 | {
2 | "tags": {
3 | "allowUnknownTags": true,
4 | "dictionaries": [ "jsdoc", "closure" ]
5 | },
6 | "source": {
7 | "includePattern": ".+\\.js(doc|x)?$",
8 | "excludePattern": "(^|\\/|\\\\)_"
9 | },
10 | "plugins": [],
11 | "templates": {
12 | "cleverLinks": false,
13 | "monospaceLinks": false
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-docker-api",
3 | "version": "1.1.23",
4 | "description": "Docker Remote API driver for node",
5 | "main": "./lib/docker",
6 | "typings": "./lib/docker",
7 | "scripts": {
8 | "watch-test": "nodemon --watch build --watch test --exec 'npm run test'",
9 | "lint": "./node_modules/tslint/bin/tslint -c tslint.json 'src/**/*.ts'",
10 | "test": "chmod +x test.sh; ./test.sh",
11 | "gendoc": "typedoc --out docs src",
12 | "build": "tsc --outDir lib"
13 | },
14 | "repository": {
15 | "type": "git",
16 | "url": "git+https://github.com/AgustinCB/docker-api.git"
17 | },
18 | "keywords": [
19 | "docker",
20 | "api",
21 | "node"
22 | ],
23 | "author": "AgustinCB",
24 | "license": "\tGPL-2.0",
25 | "bugs": {
26 | "url": "https://github.com/AgustinCB/docker-api/issues"
27 | },
28 | "homepage": "https://github.com/AgustinCB/docker-api#readme",
29 | "devDependencies": {
30 | "@types/node": "10.5.1",
31 | "ava": "^0.17.0",
32 | "brace-expansion": "^1.1.7",
33 | "chai": "^3.5.0",
34 | "jsdox": "^0.4.10",
35 | "marked": "4.0.10",
36 | "mocha": "^2.5.3",
37 | "mustache": "2.2.1",
38 | "nodemon": "^1.11.0",
39 | "tslint": "^4.4.2",
40 | "typedoc": "^0.6.0",
41 | "typescript": "^2.1.6"
42 | },
43 | "dependencies": {
44 | "docker-modem": "^0.3.1",
45 | "memorystream": "^0.3.1"
46 | }
47 | }
--------------------------------------------------------------------------------
/src/docker.ts:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | import Modem = require('docker-modem')
4 | import ContainerManager from './container'
5 | import ImageManager from './image'
6 | import VolumeManager from './volume'
7 | import NetworkManager from './network'
8 | import NodeManager from './node'
9 | import PluginManager from './plugin'
10 | import SecretManager from './secret'
11 | import ServiceManager from './service'
12 | import SwarmManager from './swarm'
13 | import TaskManager from './task'
14 |
15 | /**
16 | * Docker class with all methods
17 | */
18 | export class Docker {
19 | modem: Modem
20 | container: ContainerManager
21 | image: ImageManager
22 | volume: VolumeManager
23 | network: NetworkManager
24 | node: NodeManager
25 | plugin: PluginManager
26 | secret: SecretManager
27 | service: ServiceManager
28 | swarm: SwarmManager
29 | task: TaskManager
30 |
31 | /**
32 | * Creates the Docker Object
33 | * @param {Object} opts Docker options
34 | */
35 | constructor (opts) {
36 | this.modem = new Modem(opts)
37 |
38 | this.container = new ContainerManager(this.modem)
39 | this.image = new ImageManager(this.modem)
40 | this.volume = new VolumeManager(this.modem)
41 | this.network = new NetworkManager(this.modem)
42 | this.node = new NodeManager(this.modem)
43 | this.plugin = new PluginManager(this.modem)
44 | this.secret = new SecretManager(this.modem)
45 | this.service = new ServiceManager(this.modem)
46 | this.swarm = new SwarmManager(this.modem)
47 | this.task = new TaskManager(this.modem)
48 | }
49 |
50 | /**
51 | * Validate credentials for a registry and get identity token,
52 | * if available, for accessing the registry without password
53 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/check-auth-configuration
54 | * @param {Object} opts Auth options
55 | * @return {Promise} Promise returning the result
56 | */
57 | auth (opts: Object): Promise