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

devcontainer: support multi-stage build with dangling build stage #231

Closed
johnstcn opened this issue Jun 11, 2024 · 0 comments · Fixed by #343
Closed

devcontainer: support multi-stage build with dangling build stage #231

johnstcn opened this issue Jun 11, 2024 · 0 comments · Fixed by #343
Assignees

Comments

@johnstcn
Copy link
Member

johnstcn commented Jun 11, 2024

Currently, building a devcontainer that specifies a multi-stage build with a "dangling" FROM image AS alias fails.

Example integration test that reproduces the issue:

func TestBuildFromDevcontainerWithMultistage(t *testing.T) {
	t.Parallel()

	// Ensures that a Git repository with a devcontainer.json is cloned and built.
	srv := createGitServer(t, gitServerOptions{
		files: map[string]string{
			"devcontainer.json": `{
				"name": "Test",
				"build": {
					"dockerfile": "Dockerfile"
				},
			}`,
			"Dockerfile": fmt.Sprintf(`FROM %s as a
RUN date > /root/date.txt
FROM %s as b
COPY --from=a /root/date.txt /date.txt`, testImageUbuntu, testImageUbuntu),
		},
	})
	ctr, err := runEnvbuilder(t, options{env: []string{
		envbuilderEnv("GIT_URL", srv.URL),
	}})
	require.NoError(t, err)

	output := execContainer(t, ctr, "cat /date.txt")
	require.NotEmpty(t, strings.TrimSpace(output))
}

The above test fails with the following error:

--- FAIL: TestBuildFromDevcontainerWithMultistage (0.39s)
    /home/coder/src/coder/envbuilder/integration/integration_test.go:1207: "\x1b[1menvbuilder\x1b[22m - Build development environments from repositories in a container"
    /home/coder/src/coder/envbuilder/integration/integration_test.go:1207: "#1: 📦 Cloning \x1b[36mhttp://127.0.0.1:39405\x1b[0m to \x1b[36m/workspaces/empty\x1b[0m..."
    /home/coder/src/coder/envbuilder/integration/integration_test.go:1207: "#1: 👤 Using no authentication!"
    /home/coder/src/coder/envbuilder/integration/integration_test.go:1207: "#1: 📦 Cloned repository! [9.601971ms]"
    /home/coder/src/coder/envbuilder/integration/integration_test.go:1207: "error: compile devcontainer.json: parse image from dockerfile: parse image ref \"localhost:5000/envbuilder-test-ubuntu:latest as b\": could not parse reference: localhost:5000/envbuilder-test-ubuntu:latest as b"
    /home/coder/src/coder/envbuilder/integration/integration_test.go:526: 
        	Error Trace:	/home/coder/src/coder/envbuilder/integration/integration_test.go:526
        	Error:      	Received unexpected error:
        	            	error: compile devcontainer.json: parse image from dockerfile: parse image ref "localhost:5000/envbuilder-test-ubuntu:latest as b": could not parse reference: localhost:5000/envbuilder-test-ubuntu:latest as b
        	Test:       	TestBuildFromDevcontainerWithMultistage

It looks like we need to handle this in devcontainer.ImageFromDockerfile.

Workaround: remove the dangling directive:

FROM image AS a
RUN thing
FROM anotherimage AS b
COPY --from=a /something /somewhere

becomes

FROM image AS a
RUN thing
FROM anotherimage
COPY --from=a /something /somewhere
@coder-labeler coder-labeler bot added the bug label Jun 11, 2024
@johnstcn johnstcn changed the title devcontainer: support multi-stage build devcontainer: support multi-stage build with dangling FROM directive Jun 13, 2024
@johnstcn johnstcn changed the title devcontainer: support multi-stage build with dangling FROM directive devcontainer: support multi-stage build with dangling build stage Jun 13, 2024
@mtojek mtojek mentioned this issue Jun 17, 2024
36 tasks
@matifali matifali removed the bug label Oct 15, 2024
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

Successfully merging a pull request may close this issue.

3 participants