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

4

1 回答 1

0

这是我发现可行的一种方法

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);

其中header包含字符串的大小FROM alpine

于 2019-08-02T15:11:19.557 回答