envbuilder icon indicating copy to clipboard operation
envbuilder copied to clipboard

feat: allow specifying Kaniko targets for multi-stage dockerfiles

Open hamishc opened this issue 1 year ago • 2 comments

I've encountered issues with large multi-stage dockerfiles when targeting one particular stage. For example, if we have a devcontainer.json like

{
    "name": "envbuilder",
    "build": {
		"dockerfile": "Dockerfile",
		"args": {
		},
        "target": "devenv"
	},
}

and a Dockerfile as the following

FROM alpine AS runtime-deps

FROM runtime-deps AS devenv

FROM runtime-deps AS long-build-stage
COPY some-build-artifact/ /

then running docker run -it -v /my/repo:/workspaces/empty coder/envbuilder:latest will result in the following error

Failed to build: do build: error building stage: failed to get files used from context: failed to get fileinfo for /workspaces/empty/.devcontainer/some-build-artifact: lstat /workspaces/empty/.devcontainer/some-build-artifact: no such file or directory
Falling back to the default image...
error: do build: error building stage: failed to get files used from context: failed to get fileinfo for /workspaces/empty/.devcontainer/some-build-artifact: lstat /workspaces/empty/.devcontainer/some-build-artifact: no such file or directory: no fallback image has been specified
error: running command "envbuilder": do build: error building stage: failed to get files used from context: failed to get fileinfo for /workspaces/empty/.devcontainer/some-build-artifact: lstat /workspaces/empty/.devcontainer/some-build-artifact: no such file or directory:
    github.com/coder/envbuilder.run.func4
        /home/hamish/code/envbuilder/envbuilder.go:271
  - no fallback image has been specified

This is because we don't specify the build target in KanikoOptions. Specifying this option can also substantially decrease build times, especially when combined with the SkipUnusedStages option - which can allow for large unused build stages to be avoided entirely.

hamishc avatar Dec 16 '24 11:12 hamishc

Any chance getting this reviewed sometime soon? This is blocking the adoption of coder for our org, as we rely on multi-stage dockerfiles and devcontainers for most of our repositories.

hamishc avatar Mar 14 '25 06:03 hamishc

I would also love to get this. As far as I can tell, currently it just takes the last stage? Really weird behavior when you are used to native devcontainer development.

bedaberner avatar May 27 '25 11:05 bedaberner