Skip to content

Commit 48f63b4

Browse files
authored
Merge pull request #5 from msoghoian/pull-push
Docker push and pull support
2 parents 334b981 + 18d38b2 commit 48f63b4

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,65 @@ docker.command('login -u myusername -p mypassword').then(function (data) {
600600
// Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
601601
```
602602

603+
* docker pull
604+
605+
```js
606+
docker.command('pull nginx:latest').then(function (data) {
607+
console.log('data = ', data);
608+
// Successfully pulled image
609+
}, function (rejected) {
610+
console.log('rejected = ', rejected);
611+
// Failed to pull image
612+
});
613+
614+
// data = { command: 'docker pull nginx:1.15.2 ',
615+
// raw:'1.15.2: Pulling from library/nginx\nDigest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424\nStatus: Image is up to date for nginx:1.15.2\n',
616+
// login: '1.15.2: Pulling from library/nginx\nDigest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424\nStatus: Image is up to date for nginx:1.15.2' }
617+
618+
// rejected = error: 'Error: Command failed: docker pull nginx:999.999.999
619+
// Error response from daemon: manifest for nginx:999.999.999 not found
620+
// ' stdout = '' stderr = 'Error response from daemon: manifest for nginx:999.999.999 not found
621+
```
622+
623+
* docker push
624+
625+
```js
626+
docker.command('push nginx:latest').then(function (data) {
627+
console.log('data = ', data);
628+
// Successfully pulled image
629+
}, function (rejected) {
630+
console.log('rejected = ', rejected);
631+
// Failed to pull image
632+
});
633+
634+
// data = { command: 'docker push mattsoghoian/test ',
635+
// raw:
636+
// 'The push refers to repository [docker.io/<username>/<repo>]\n08d25fa0442e: Preparing\na8c4aeeaa045: Preparing\ncdb3f9544e4c: Preparing\n08d25fa0442e: Mounted from library/nginx\na8c4aeeaa045: Mounted from library/nginx\ncdb3f9544e4c: Mounted from library/nginx\nlatest: digest: sha256:4ffd9758ea9ea360fd87d0cee7a2d1cf9dba630bb57ca36b3108dcd3708dc189 size: 948\n',
637+
// login:
638+
// 'The push refers to repository [docker.io/<username>/<repo>]\n08d25fa0442e: Preparing\na8c4aeeaa045: Preparing\ncdb3f9544e4c: Preparing\n08d25fa0442e: Mounted from library/nginx\na8c4aeeaa045: Mounted from library/nginx\ncdb3f9544e4c: Mounted from library/nginx\nlatest: digest: sha256:4ffd9758ea9ea360fd87d0cee7a2d1cf9dba630bb57ca36b3108dcd3708dc189 size: 948' }
639+
640+
// rejected = error: 'Error: Command failed: docker push nginx
641+
// An image does not exist locally with the tag: nginx
642+
// ' stdout = 'The push refers to repository [docker.io/library/nginx]
643+
// ' stderr = 'An image does not exist locally with the tag: nginx
644+
645+
// rejected = error: 'Error: Command failed: docker push nginx
646+
// errors:
647+
// denied: requested access to the resource is denied
648+
// unauthorized: authentication required
649+
// ' stdout = 'The push refers to repository [docker.io/library/nginx]
650+
// 08d25fa0442e: Preparing
651+
// a8c4aeeaa045: Preparing
652+
// cdb3f9544e4c: Preparing
653+
// cdb3f9544e4c: Layer already exists
654+
// 08d25fa0442e: Layer already exists
655+
// a8c4aeeaa045: Layer already exists
656+
// ' stderr = 'errors:
657+
// denied: requested access to the resource is denied
658+
// unauthorized: authentication required
659+
// '
660+
```
661+
603662

604663
## License
605664

src/index.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,62 @@ test("docker-cli-js", (t) => {
132132
});
133133
});
134134

135+
t.test("pull latest", (t) => {
136+
137+
const docker = new Docker();
138+
139+
return docker.command("pull nginx").then(function(data) {
140+
console.log("data = ", data);
141+
t.ok(data.login);
142+
});
143+
});
144+
145+
t.test("pull specific tag", (t) => {
146+
147+
const docker = new Docker();
148+
149+
return docker.command("pull nginx:1.15.2").then(function(data) {
150+
console.log("data = ", data);
151+
t.ok(data.login);
152+
});
153+
});
154+
155+
t.test("pull intentionally failed, invalid image", (t) => {
156+
157+
const docker = new Docker();
158+
159+
return docker.command("pull nginx:999.999.999").then(function(data) {
160+
console.log("data = ", data);
161+
t.notOk(data.login);
162+
}, function(rejected) {
163+
console.log("rejected = ", rejected);
164+
t.ok(/error/.test(rejected));
165+
});
166+
});
167+
168+
t.test("push intentionally failed, denied repo access", (t) => {
169+
170+
const docker = new Docker();
171+
172+
return docker.command("push nginx").then(function(data) {
173+
console.log("data = ", data);
174+
t.ok(data.login);
175+
}, function(rejected) {
176+
console.log("rejected = ", rejected);
177+
t.ok(/error/.test(rejected));
178+
});
179+
});
180+
181+
t.test("push intentionally failed, local image does not exist", (t) => {
182+
183+
const docker = new Docker();
184+
185+
return docker.command("push dmarionertfulthestoncoag").then(function(data) {
186+
console.log("data = ", data);
187+
t.ok(data.login);
188+
}, function(rejected) {
189+
console.log("rejected = ", rejected);
190+
t.ok(/error/.test(rejected));
191+
});
192+
});
135193
});

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ const extractResult = function(result: any) {
130130
run(resultp: any) {
131131
resultp.login = resultp.raw.trim();
132132

133+
return resultp;
134+
},
135+
},
136+
{
137+
re: / pull /,
138+
run(resultp: any) {
139+
resultp.login = resultp.raw.trim();
140+
141+
return resultp;
142+
},
143+
},
144+
{
145+
re: / push /,
146+
run(resultp: any) {
147+
resultp.login = resultp.raw.trim();
148+
133149
return resultp;
134150
},
135151
},

0 commit comments

Comments
 (0)