-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
82 lines (65 loc) · 1.79 KB
/
main.js
File metadata and controls
82 lines (65 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Dockerode from 'dockerode';
import tar from 'tar-fs';
import zlib from 'node:zlib';
import concat from 'concat-stream';
const IMAGE_NAME = 'localhost:5678/my-repo:docker-containerd-image-store-bug-repro';
function createTar() {
const pack = tar.pack();
pack.entry({ name: 'Dockerfile' }, 'FROM ubuntu/squid');
pack.finalize();
return pack; // this is a tar stream!
}
const dockerode = new Dockerode({ Promise });
function modemDialPromised(modem, options) {
return new Promise((resolve, reject) => {
modem.dial(options, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
}
const pack = createTar();
const buffer = await new Promise((resolve, reject) => {
pack.on('error', reject);
pack.pipe(zlib.createGzip())
.pipe(concat(resolve));
});
const opts = {
method: 'POST',
path: '/build?',
file: buffer, // this is a Buffer with build context tar.gz file
options: {
t: IMAGE_NAME,
forcerm: true,
nocache: true,
pull: true,
},
isStream: true,
statusCodes: {
200: true,
404: 'not found',
500: 'server error',
},
};
const stream = await modemDialPromised(dockerode.modem, opts);
await new Promise((resolve, reject) => {
stream.on('data', (data) => {
console.log(data.toString());
});
stream.on('end', resolve);
stream.on('error', reject);
});
// Push
const image = dockerode.getImage(IMAGE_NAME);
const optsPush = {
stream: true,
};
const dockerPushStream = await image.push(optsPush);
await new Promise((resolve, reject) => {
dockerPushStream.on('data', (data) => {
console.log(`Push: ${data.toString().trim()}`);
});
dockerPushStream.on('end', resolve);
dockerPushStream.on('error', reject);
});
console.log('done');