UNPKG

857 BJavaScriptView Raw
1'use strict'
2const Docker = require('node-docker-api').Docker
3
4const 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
10const docker = new Docker({ socketPath: '/var/run/docker.sock' })
11let _container
12
13docker.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))