Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bkneis authored and pascalbreuninger committed Jan 9, 2025
1 parent dda1a05 commit fc0e5eb
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

2 changes: 1 addition & 1 deletion examples/build-multi-stage/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ FROM go AS final

COPY app /app

RUN echo hello
RUN echo hello
2 changes: 1 addition & 1 deletion examples/compose/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ FROM go AS final

COPY app /app

RUN echo hello
RUN echo hello
31 changes: 30 additions & 1 deletion examples/compose/devcontainer.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
FROM mcr.microsoft.com/devcontainers/javascript-node
FROM mcr.microsoft.com/devcontainers/go:1.22-bullseye AS go

ARG TARGETOS
ARG TARGETARCH

# Install Node.js
RUN \
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get update \
&& apt-get install -y --no-install-recommends nodejs \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Set environment variables for Rust
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH \
RUST_VERSION=1.69.0

# Install Protobuf compiler
RUN \
apt-get update \
&& apt-get install -y --no-install-recommends protobuf-compiler \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

FROM go AS final

COPY app /app

RUN echo hello
65 changes: 39 additions & 26 deletions pkg/devcontainer/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,28 +397,12 @@ func (r *runner) startContainer(
return containerDetails, nil
}

// This extends the build information for docker compose containers
func (r *runner) buildAndExtendDockerCompose(
ctx context.Context,
parsedConfig *config.SubstitutedConfig,
substitutionContext *config.SubstitutionContext,
project *composetypes.Project,
composeHelper *compose.ComposeHelper,
composeService *composetypes.ServiceConfig,
globalArgs []string,
) (string, string, *config.ImageMetadataConfig, string, error) {
var dockerFilePath, dockerfileContents, dockerComposeFilePath string
// prepareComposeBuildInfo modifies a compose project's devcontainer Dockerfile to ensure it can be extended with features
// If an Image is specified instead of a Build, the metadata from the Image is used to populate the build info
func (r *runner) prepareComposeBuildInfo(ctx context.Context, subCtx *config.SubstitutionContext, composeService *composetypes.ServiceConfig, buildTarget string) (*config.ImageBuildInfo, string, string, error) {
var dockerFilePath, dockerfileContents string
var imageBuildInfo *config.ImageBuildInfo
var err error

buildImageName := composeService.Image
// If Image is empty then we are building the dev container and use the default name docker-compose uses
if buildImageName == "" {
buildImageName = fmt.Sprintf("%s-%s", project.Name, composeService.Name)
}
buildTarget := "dev_container_auto_added_stage_label"

// Determine base imageName for generated features build
if composeService.Build != nil {
// Read Dockerfile
if path.IsAbs(composeService.Build.Dockerfile) {
Expand All @@ -429,7 +413,7 @@ func (r *runner) buildAndExtendDockerCompose(

originalDockerfile, err := os.ReadFile(dockerFilePath)
if err != nil {
return "", "", nil, "", err
return nil, "", "", err
}

// Determine build target, if a multi stage build ensure it is valid and modify the Dockerfile if necessary
Expand All @@ -439,7 +423,7 @@ func (r *runner) buildAndExtendDockerCompose(
} else {
lastStageName, modifiedDockerfile, err := dockerfile.EnsureDockerfileHasFinalStageName(string(originalDockerfile), config.DockerfileDefaultTarget)
if err != nil {
return "", "", nil, "", err
return nil, "", "", err
}

buildTarget = lastStageName
Expand All @@ -450,16 +434,45 @@ func (r *runner) buildAndExtendDockerCompose(
dockerfileContents = string(originalDockerfile)
}
}
imageBuildInfo, err = r.getImageBuildInfoFromDockerfile(substitutionContext, string(originalDockerfile), mappingToMap(composeService.Build.Args), originalTarget)
imageBuildInfo, err = r.getImageBuildInfoFromDockerfile(subCtx, string(originalDockerfile), mappingToMap(composeService.Build.Args), originalTarget)
if err != nil {
return "", "", nil, "", err
return nil, "", "", err
}
} else {
imageBuildInfo, err = r.getImageBuildInfoFromImage(ctx, substitutionContext, composeService.Image)
imageBuildInfo, err = r.getImageBuildInfoFromImage(ctx, subCtx, composeService.Image)
if err != nil {
return "", "", nil, "", err
return nil, "", "", err
}
}
return imageBuildInfo, dockerfileContents, buildTarget, nil
}

// This extends the build information for docker compose containers
func (r *runner) buildAndExtendDockerCompose(
ctx context.Context,
parsedConfig *config.SubstitutedConfig,
substitutionContext *config.SubstitutionContext,
project *composetypes.Project,
composeHelper *compose.ComposeHelper,
composeService *composetypes.ServiceConfig,
globalArgs []string,
) (string, string, *config.ImageMetadataConfig, string, error) {
var dockerFilePath, dockerfileContents, dockerComposeFilePath string
var imageBuildInfo *config.ImageBuildInfo
var err error

buildImageName := composeService.Image
// If Image is empty then we are building the dev container and use the default name docker-compose uses
if buildImageName == "" {
buildImageName = fmt.Sprintf("%s-%s", project.Name, composeService.Name)
}
buildTarget := "dev_container_auto_added_stage_label"

// Determine base imageName for generated features build
imageBuildInfo, dockerfileContents, buildTarget, err = r.prepareComposeBuildInfo(ctx, substitutionContext, composeService, buildTarget)
if err != nil {
return "", "", nil, "", err
}

extendImageBuildInfo, err := feature.GetExtendedBuildInfo(substitutionContext, imageBuildInfo, buildTarget, parsedConfig, r.Log, false)
if err != nil {
Expand Down

0 comments on commit fc0e5eb

Please sign in to comment.