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))
|