-
Notifications
You must be signed in to change notification settings - Fork 410
Support building from Dockerfile.in templates in devcontainer.json using configurable preprocessing tool #1233
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
Open
Mathiyarasy
wants to merge
12
commits into
devcontainers:main
Choose a base branch
from
Mathiyarasy:dev/Mathi/dotinsupport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0859e15
Support for building from Dockerfile.in-style templates using a new d…
Mathiyarasy e7b2478
Refine Dockerfile preprocessor tool
Mathiyarasy c4245b6
add test cases
Mathiyarasy 907c19e
update
Mathiyarasy 97db436
add test case for windows
Mathiyarasy 51db57d
update windows test
Mathiyarasy 04bf682
update
Mathiyarasy bcbf397
Add more control
Mathiyarasy fa5b8da
fix test cases
Mathiyarasy f663bb1
simplify contract
Mathiyarasy 13523b3
Address review comments
Mathiyarasy 2924c4b
modify comment
Mathiyarasy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as path from 'path'; | ||
| import { DevContainerFromDockerComposeConfig, DevContainerFromDockerfileConfig } from '../spec-configuration/configuration'; | ||
| import { ContainerError, toErrorText } from '../spec-common/errors'; | ||
| import { CLIHost } from '../spec-common/cliHost'; | ||
| import { runCommandNoPty } from '../spec-common/commonUtils'; | ||
| import { Log, LogLevel, makeLog } from '../spec-utils/log'; | ||
|
|
||
| function dockerfilePreprocessorToolDocs(): string { | ||
| return "Set 'dockerfilePreprocessor.tool', optional 'dockerfilePreprocessor.args', and 'dockerfilePreprocessor.generatedDockerfilePath' in devcontainer.json. The CLI invokes the tool with configured args and validates that the generated Dockerfile exists at the configured path."; | ||
| } | ||
|
|
||
| export async function preprocessDockerExtensionFile( | ||
| params: { cliHost: CLIHost; output: Log }, | ||
| config: Pick<DevContainerFromDockerfileConfig | DevContainerFromDockerComposeConfig, 'dockerfilePreprocessor'>, | ||
| dockerfilePath: string | ||
| ): Promise<string> { | ||
|
|
||
| const tool = config.dockerfilePreprocessor?.tool?.trim(); | ||
| const args = (config.dockerfilePreprocessor?.args || []).map(arg => arg.trim()).filter(arg => arg.length > 0); | ||
| const generatedDockerfilePath = config.dockerfilePreprocessor?.generatedDockerfilePath?.trim(); | ||
| if (!tool) { | ||
| throw new ContainerError({ | ||
| description: `A Dockerfile preprocessor tool is required to build from '${dockerfilePath}'. ${dockerfilePreprocessorToolDocs()}`, | ||
| data: { fileWithError: dockerfilePath }, | ||
| }); | ||
| } | ||
| if (!generatedDockerfilePath) { | ||
| throw new ContainerError({ | ||
| description: `dockerfilePreprocessor.generatedDockerfilePath is required. ${dockerfilePreprocessorToolDocs()}`, | ||
| data: { fileWithError: dockerfilePath }, | ||
| }); | ||
| } | ||
|
|
||
| const { cliHost, output } = params; | ||
| const infoOutput = makeLog(output, LogLevel.Info); | ||
| const workdirPath = path.dirname(dockerfilePath); | ||
| const generatedOutputPath = path.resolve(workdirPath, generatedDockerfilePath); | ||
| const generatedOutputDir = path.dirname(generatedOutputPath); | ||
| await cliHost.mkdirp(generatedOutputDir); | ||
| const staleOutputPaths = [generatedOutputPath]; | ||
| for (const stalePath of staleOutputPaths) { | ||
| if (!await cliHost.isFile(stalePath)) { | ||
| continue; | ||
| } | ||
| await cliHost.remove(stalePath); | ||
| } | ||
|
|
||
| // Minimal contract: tool args are user-controlled and run in the Dockerfile | ||
| // directory. The CLI only provides the resolved generated Dockerfile path. | ||
| const env = { | ||
| ...cliHost.env, | ||
| DEVCONTAINER_DOCKERFILE_PREPROCESSOR_GENERATED_DOCKERFILE: generatedOutputPath, | ||
| }; | ||
| const invocationArgs = [...args]; | ||
|
|
||
| try { | ||
| infoOutput.write(`Preprocessing '${dockerfilePath}' -> '${generatedOutputPath}'`); | ||
| await runCommandNoPty({ | ||
| exec: cliHost.exec, | ||
| cmd: tool, | ||
| args: invocationArgs, | ||
| cwd: workdirPath, | ||
| env, | ||
| output: infoOutput, | ||
| print: 'continuous', | ||
| }); | ||
| } catch (err) { | ||
| const originalError = err as { | ||
| message?: string; | ||
| stderr?: Buffer | string; | ||
| cmdOutput?: string; | ||
| code?: number; | ||
| signal?: string; | ||
| }; | ||
| const stderrText = typeof originalError?.stderr === 'string' ? originalError.stderr : originalError?.stderr?.toString(); | ||
| throw new ContainerError({ | ||
| description: `Dockerfile preprocessing failed while running '${tool}'. ${dockerfilePreprocessorToolDocs()}`, | ||
| originalError: { | ||
| message: `${originalError?.message || 'Dockerfile preprocessing command failed.'} ${toErrorText(stderrText || originalError?.cmdOutput || '')}`.trim(), | ||
| code: originalError?.code, | ||
| signal: originalError?.signal, | ||
| stderr: originalError?.stderr, | ||
| }, | ||
| data: { fileWithError: dockerfilePath }, | ||
| }); | ||
| } | ||
|
|
||
| const generatedExists = await cliHost.isFile(generatedOutputPath); | ||
| if (!generatedExists) { | ||
| throw new ContainerError({ | ||
| description: `Dockerfile preprocessing did not produce '${generatedOutputPath}'. Ensure the configured tool writes the final Dockerfile to the configured generatedDockerfile path. ${dockerfilePreprocessorToolDocs()}`, | ||
| data: { fileWithError: dockerfilePath }, | ||
| }); | ||
| } | ||
|
|
||
| infoOutput.write(`Preprocessed Dockerfile written to '${generatedOutputPath}'`); | ||
|
|
||
| return generatedOutputPath; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/test/configs/dockercomposefile-cpp-preprocessor/.devcontainer/Dockerfile.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #define BASE_IMAGE ubuntu:20.04 | ||
| #define INSTALL_NODE | ||
| #define INSTALL_PYTHON | ||
|
|
||
| FROM BASE_IMAGE | ||
|
|
||
| #ifdef INSTALL_NODE | ||
| RUN apt-get update && apt-get install -y nodejs | ||
| #endif | ||
|
|
||
| #ifdef INSTALL_PYTHON | ||
| RUN apt-get update && apt-get install -y python3 | ||
| #endif | ||
|
|
||
| #include "common.Dockerfile" | ||
| #include "tools.Dockerfile" |
18 changes: 18 additions & 0 deletions
18
src/test/configs/dockercomposefile-cpp-preprocessor/.devcontainer/devcontainer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "Docker Compose Cpp Preprocessor", | ||
| "dockerComposeFile": "docker-compose.yml", | ||
| "service": "app", | ||
| "workspaceFolder": "/workspace", | ||
| "dockerfilePreprocessor": { | ||
| "tool": "cpp", | ||
| "generatedDockerfilePath": "Dockerfile", | ||
| "args": [ | ||
| "-P", | ||
| "Dockerfile.in", | ||
| "Dockerfile" | ||
| ] | ||
| }, | ||
| "features": { | ||
| "ghcr.io/devcontainers/features/git": "latest" | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/test/configs/dockercomposefile-cpp-preprocessor/.devcontainer/docker-compose.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| version: '3.8' | ||
|
|
||
| services: | ||
| app: | ||
| build: | ||
| context: .. | ||
| dockerfile: Dockerfile.in | ||
| command: sleep infinity | ||
| volumes: | ||
| - ..:/workspace:cached |
16 changes: 16 additions & 0 deletions
16
src/test/configs/dockercomposefile-cpp-preprocessor/Dockerfile.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #define BASE_IMAGE ubuntu:20.04 | ||
| #define INSTALL_NODE | ||
| #define INSTALL_PYTHON | ||
|
|
||
| FROM BASE_IMAGE | ||
|
|
||
| #ifdef INSTALL_NODE | ||
| RUN apt-get update && apt-get install -y nodejs | ||
| #endif | ||
|
|
||
| #ifdef INSTALL_PYTHON | ||
| RUN apt-get update && apt-get install -y python3 | ||
| #endif | ||
|
|
||
| #include "common.Dockerfile" | ||
| #include "tools.Dockerfile" |
1 change: 1 addition & 0 deletions
1
src/test/configs/dockercomposefile-cpp-preprocessor/common.Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| RUN apt-get update && apt-get install -y curl wget |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #!/usr/bin/env bash | ||
| echo hello |
2 changes: 2 additions & 0 deletions
2
src/test/configs/dockercomposefile-cpp-preprocessor/tools.Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| RUN apt-get update && apt-get install -y vim | ||
| COPY ./test.sh /usr/local/bin/test.sh |
18 changes: 18 additions & 0 deletions
18
src/test/configs/dockerfile-autoconf-preprocessor/.devcontainer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "build": { | ||
| "dockerfile": "Dockerfile.in" | ||
| }, | ||
| "dockerfilePreprocessor": { | ||
| "tool": "sh", | ||
| "args": [ | ||
| "-c", | ||
| "autoconf && ./configure" | ||
| ], | ||
| "generatedDockerfilePath": "Dockerfile" | ||
| }, | ||
| "features": { | ||
| "ghcr.io/devcontainers/features/github-cli:1": { | ||
| "version": "latest" | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/test/configs/dockerfile-autoconf-preprocessor/Dockerfile.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| FROM @BASE_IMAGE@ | ||
|
|
||
| ARG APP_PORT=@APP_PORT@ | ||
| EXPOSE @APP_PORT@ | ||
|
|
||
| WORKDIR /workspace | ||
| COPY . /workspace | ||
|
|
||
| CMD ["npm", "start"] |
11 changes: 11 additions & 0 deletions
11
src/test/configs/dockerfile-autoconf-preprocessor/configure.ac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| AC_INIT([generate-dockerfile], [1.0]) | ||
| AC_CONFIG_SRCDIR([Dockerfile.in]) | ||
|
|
||
| BASE_IMAGE='node:22-bookworm' | ||
| APP_PORT='3000' | ||
|
|
||
| AC_SUBST([BASE_IMAGE]) | ||
| AC_SUBST([APP_PORT]) | ||
|
|
||
| AC_CONFIG_FILES([Dockerfile]) | ||
| AC_OUTPUT |
20 changes: 20 additions & 0 deletions
20
src/test/configs/dockerfile-cmake-preprocessor/.devcontainer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "build": { | ||
| "dockerfile": "Dockerfile.in" | ||
| }, | ||
| "dockerfilePreprocessor": { | ||
| "tool": "cmake", | ||
| "args": [ | ||
| "-S", | ||
| ".", | ||
| "-B", | ||
| "build" | ||
| ], | ||
| "generatedDockerfilePath": "build/Dockerfile" | ||
| }, | ||
| "features": { | ||
| "ghcr.io/devcontainers/features/github-cli:1": { | ||
| "version": "latest" | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/test/configs/dockerfile-cmake-preprocessor/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| cmake_minimum_required(VERSION 3.16) | ||
| project(GenerateDockerfile NONE) | ||
|
|
||
| set(BASE_IMAGE "node:22-bookworm") | ||
| set(APP_PORT "3000") | ||
|
|
||
| configure_file( | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/Dockerfile.in | ||
| ${CMAKE_CURRENT_BINARY_DIR}/Dockerfile | ||
| @ONLY | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| FROM @BASE_IMAGE@ | ||
|
|
||
| ARG APP_PORT=@APP_PORT@ | ||
| EXPOSE @APP_PORT@ | ||
|
|
||
| WORKDIR /workspace | ||
| COPY . /workspace | ||
|
|
||
| CMD ["npm", "start"] |
20 changes: 20 additions & 0 deletions
20
src/test/configs/dockerfile-cmake2-preprocessor/.devcontainer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "build": { | ||
| "dockerfile": "Dockerfile.in" | ||
| }, | ||
| "dockerfilePreprocessor": { | ||
| "tool": "cmake", | ||
| "args": [ | ||
| "-S", | ||
| ".", | ||
| "-B", | ||
| "build" | ||
| ], | ||
| "generatedDockerfilePath": "Dockerfile" | ||
| }, | ||
| "features": { | ||
| "ghcr.io/devcontainers/features/github-cli:1": { | ||
| "version": "latest" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.