dockerode icon indicating copy to clipboard operation
dockerode copied to clipboard

Source directory and Dockerfile in different locations

Open bitttttten opened this issue 10 months ago • 2 comments

I have my source code in ~/projects/repo, and I have a Dockerfile inside ~/dockerfiles.

I'm trying to write a script where I can pass in the source code path, like " ~/projects/repo", and then it will run the Dockerfile against that. So I thought I could do someting like:

import Docker from 'dockerode';
import fs from 'fs';
import path from 'path';
import url from 'url';
import { nanoid } from 'nanoid';
import { LogStream } from './utils/log-stream.mjs'

const docker = new Docker();

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const dockerFilePath = path.join(__dirname, '../dockerfiles/Dockerfile')

export async function buildImage(sourcePath, imageName, deploymentId) {
  buildId = nanoid()
  const absoluteSourcePath = path.resolve(sourcePath);

  if (!fs.existsSync(absoluteSourcePath)) {
    throw new Error(`Source path '${absoluteSourcePath}' does not exist`);
  }

  await docker.buildImage({
    context: absoluteSourcePath,
    src: [dockerFilePath]
  }, {
    t: imageName
  });

  console.log(`[${buildId}] Docker image '${imageName}' built successfully`);

}

So then I can run buildImage("~/projects/my-repo", "my-repo", "repo-id").

Although I get the error

Error: ENOENT: no such file or directory, lstat '~/projects/my-repo/Users/bitttttten/dockerfiles/Dockerfile'
}```

In theory it looks like it should work, however it seems to be putting the Dockerfile onto the context, rather than looking for it relatively. 

Am I doing something wrong here? I've tried many different combinations and so far I'm stuck 😅 

bitttttten avatar Mar 31 '24 15:03 bitttttten

I'm also getting the same error

MahmoudH96 avatar Apr 12 '24 14:04 MahmoudH96

I've tried every combination as well. This seems to be a limitation of dockerode. A possible workaround is moving the Dockerfile to the top directory and having it named "Dockerfile" but this isn't ideal for every project.

tmart-ops avatar Jun 18 '24 15:06 tmart-ops