0

dockerode api needs to be invoked with the following parameters in order to build an image from a dockerfile

ex:

docker.buildImage({
  context: __dirname,
  src: ['Dockerfile']
}, {
  t: 'myDockerImage'
}, function(error, output) {
  if (error) {
    console.error(error);
  }
  output.pipe(process.stdout);
});

where context denotes the directory where the dockerfile is located, src would contain the files that will be used to create the tarball and generate the dockerimage, and t denotes the tagname of the dockerimage.

However in my situatation i want to skip mapping the directory and the name of the dockerfile for dockerode to read and create the image from, or, in other word i want to skip the file read operation that goes inside the dockerode and instead suppy the content of the dockerfile directly as a string .

so, if the dockerfile contatins something like this

FROM alpine

I want to pass that string directly to dockeode to build the image instead of writing that text to a dockerfile and passing the path to the file.

is there a way, it would be possible

isnvi23h4
  • 1,910
  • 1
  • 27
  • 45

1 Answers1

0

Here is a way i found it to be possible

var dockerode = require('dockerode');
var tar = require('tar-stream');
var zlib = require('zlib');

var docker = new dockerode();
var pack = tar.pack();

function buildImage(dockerfile, tagname) {
  var header = {
    name: 'Dockerfile',
    type: 'file',
    size: dockerfile.length
  };

  var entry = pack.entry(header, function(err) {
    if (err) {
      console.log(err);
      pack.destroy(err);
    }

    pack.finalize();
  });

  entry.write(dockerfile);
  entry.end();

  docker.buildImage(pack.pipe(zlib.createGzip()), {
    t: tagname
  }, function(error, output) {
    if (error) {
      console.error(error);
    }
    output.pipe(process.stdout);
  });
}

var dockerfile = 'FROM alpine\n';
var tagname = 'myalpine';

buildImage(dockerfile, tagname);

where header contains the size of the string FROM alpine

isnvi23h4
  • 1,910
  • 1
  • 27
  • 45