Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source directory and Dockerfile in different locations #762

Open
bitttttten opened this issue Mar 31, 2024 · 2 comments
Open

Source directory and Dockerfile in different locations #762

bitttttten opened this issue Mar 31, 2024 · 2 comments

Comments

@bitttttten
Copy link

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 😅 
@MahmoudH96
Copy link

I'm also getting the same error

@tmart-ops
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants