Docker: get the content of a container and upload it to an s3 bucket with nodejs

Posted on: 2020-04-01
Software versions: NodeJS 10, Docker 1.37, Typescript 3.8

We can use aws-sdk and Dockerode and play with streams to upload to take the result of the Docker "GET archive" docker endpoint and upload it to an s3 bucket. Here is an example with typescript:

import AWS from 'aws-sdk'; import Docker from 'dockerode'; const s3 = new AWS.S3(); const docker = new Docker(); async function uploadToS3 () { const container = this.docker.getContainer('my-container'); // here the response we get is a readable stream... const dockerResponse = await container.getArchive({ path: `/path/to/the/directory/to/save` }); await new Promise((resolve, reject) => { this.s3.upload({ Bucket: 'my-bucket', Key: `archive.tar`, // and we can give that stream directly to the AWS SDK: Body: dockerResponse, }, err => { if (err) { reject(err); } }); // once the response has been entirely read, we can resolve the promise: dockerResponse.on('close', resolve); dockerResponse.on('error', e => { console.error(e); reject(e); }); }); }