Skip to content

Spawn docker build relative to the Dockerfile #9729

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

Merged
merged 9 commits into from
Jul 4, 2025

Conversation

404Wolf
Copy link
Contributor

@404Wolf 404Wolf commented Jun 24, 2025

Fixes this beta container issue.

Set build context relative to the Dockerfile so that COPYs do not break.
Also just cleaning up some duplicated helpers tests


  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • Wrangler / Vite E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: bugfix
  • Wrangler V3 Backport
    • TODO (before merge)
    • Wrangler PR:
    • Not necessary because: not in v3

@404Wolf 404Wolf requested a review from a team as a code owner June 24, 2025 19:04
Copy link

changeset-bot bot commented Jun 24, 2025

🦋 Changeset detected

Latest commit: bcacff9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Jun 24, 2025
@IRCody IRCody requested a review from a team June 24, 2025 19:08
@skepticfx
Copy link
Member

Thank you 🚀

Copy link

pkg-pr-new bot commented Jun 24, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@9729

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@9729

miniflare

npm i https://pkg.pr.new/miniflare@9729

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@9729

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@9729

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@9729

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@9729

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@9729

wrangler

npm i https://pkg.pr.new/wrangler@9729

commit: bcacff9

@404Wolf
Copy link
Contributor Author

404Wolf commented Jun 24, 2025

Realized that we want the dirname of the dockerfile path, not the dockerfile path :/

@emily-shen emily-shen added the e2e Run wrangler + vite-plugin e2e tests on a PR label Jun 26, 2025
@emily-shen
Copy link
Contributor

emily-shen commented Jun 26, 2025

hey, thanks for the contribution! would you be able to add a regression test for this? probably in e2e/containers.dev.test.ts
let me know if you need any help with this :)

@emily-shen
Copy link
Contributor

also i'm not sure if misunderstanding the problem, but do we want to spawn docker relative to the dockerfile? the dockerfile and image_build_context is defined in the wrangler config relative to the wrangler config, so it would make sense to spawn docker relative to the wrangler config too, and pass in the paths defined there. there might be a bug with how we resolve paths, but i'm struggling to reproduce.

can you give an example of something that fails without this fix and works with? (i guess this comment is also just still asking for the regression test :p)

@404Wolf
Copy link
Contributor Author

404Wolf commented Jun 26, 2025

also i'm not sure if misunderstanding the problem, but do we want to spawn docker relative to the dockerfile? the dockerfile and image_build_context is defined in the wrangler config relative to the wrangler config, so it would make sense to spawn docker relative to the wrangler config too, and pass in the paths defined there. there might be a bug with how we resolve paths, but i'm struggling to reproduce.

can you give an example of something that fails without this fix and works with? (i guess this comment is also just still asking for the regression test :p)

Hey Emily, I should have more time to look into this tomorrow, but the general issue is for when you have a setup that looks like:

cloudflare/wrangler.jsonc
cloudflare/main.ts
server/main.ts
Dockerfile

And you need the Dockerfile in the scope of the docker build, so you specify ".." for the image, but the build fails / COPYs fail because docker is run from CWD=./cloudflare

According to the docs:

image_build_context: "The build context of the application, by default it is the directory of image."
And if I have image set to ".." I feel it should spawn docker from ".."

@emily-shen
Copy link
Contributor

emily-shen commented Jun 26, 2025

ah i see if i deploy with that file structure, I was confused because it doesn't seem to happen in dev. I think the actual bug is on this line:

buildContext: container.image_build_context ?? ".",

it should be falling back to the directory of the dockerfile rather than the cwd - buildContext is what gets passed into the docker build command, and the build command itself can spawn from wherever.

also, does setting image_build_context fix the current issue, or is it also broken somehow? here is a quick repro i've been working off, where setting image_build_context to ".." seems to fix the issue - https://github.com/emily-shen/ancient-firefly-1260/tree/main

sorry for all the back and forth, just making sure we've not messed this up in multiple places! thanks so much for the contribution 🧡

@emily-shen emily-shen force-pushed the spawn-docker-correctly branch from 1fe108a to ed5604a Compare July 3, 2025 19:04
@emily-shen emily-shen force-pushed the spawn-docker-correctly branch from ed5604a to ae4b2a5 Compare July 4, 2025 08:42
@emily-shen emily-shen added skip-pr-description-validation Skip validation of the required PR description format skip-v3-pr Skip validation of presence of a v3 backport PR labels Jul 4, 2025
},
} as unknown as ChildProcess;
})
// 2. docker image inspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test need 2, 3, 4, 5 ?

Sounds like what we want to test is the build path ("resolve the docker build context path based on the dockerfile location, if image_build_context is not provided")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wanted to test all the config resolution up until the build, but I don't think there's a good way to test just that part of the deploy path?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that ideally we should test that pathToDockerFile is correctly set by the build command. And then add another test testing that the build command reflects the passed value (explicit path or default to Dockerfile)

With the current, there are ~100 LOC that are not related to what we are trying to test and that might need to be updated when something unrelated to what we want to test changes.

If there is no easy/quick way to fix this, I don't think it should block merging the PR but we should revisit later. It could benefit test speed, coverage and maintainability.

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few suggestions

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Jul 4, 2025
@emily-shen emily-shen enabled auto-merge July 4, 2025 14:55
@@ -28,7 +28,7 @@ export async function constructBuildCommand(
const dockerfile = readFileSync(options.pathToDockerfile, "utf-8");
// pipe in the dockerfile
buildCmd.push("-f", "-");
buildCmd.push(options.buildContext);
buildCmd.push(options.buildContext ?? path.dirname(options.pathToDockerfile));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some JSDoc to the function to describe this behavior?

@@ -10,7 +10,8 @@ export type BuildArgs = {
/** image tag in the format `name:tag`, where tag is optional */
tag: string;
pathToDockerfile: string;
buildContext: string;
/** image_build_context or args.PATH. if not provided, defaults to the dockerfile directory */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for updating the doc

@emily-shen emily-shen added this pull request to the merge queue Jul 4, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 4, 2025
@emily-shen emily-shen added this pull request to the merge queue Jul 4, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 4, 2025
@emily-shen emily-shen added this pull request to the merge queue Jul 4, 2025
Merged via the queue into cloudflare:main with commit 1b3a2b7 Jul 4, 2025
66 of 72 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Jul 4, 2025
@workers-devprod workers-devprod added the contribution [Holopin] Recognizes an open-source contribution, big or small label Jul 4, 2025
Copy link

holopin-bot bot commented Jul 4, 2025

Congratulations @404Wolf, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cmcp29fn9367107i264bxxicw

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account.
Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!

@workers-devprod workers-devprod mentioned this pull request Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution [Holopin] Recognizes an open-source contribution, big or small e2e Run wrangler + vite-plugin e2e tests on a PR skip-pr-description-validation Skip validation of the required PR description format skip-v3-pr Skip validation of presence of a v3 backport PR
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants