Skip to content

Commit

Permalink
Use a deterministic target path for feature dir mounts with useBuildC…
Browse files Browse the repository at this point in the history
…ontexts (#247)
  • Loading branch information
aaronlehmann authored Jun 24, 2024
1 parent 7ebdf44 commit 7b22c45
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions devcontainer/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ FROM localhost:5000/envbuilder-test-codercom-code-server:latest
USER root
# Rust tomato - Example description!
WORKDIR `+featureOneDir+`
WORKDIR /.envbuilder/features/one
ENV TOMATO=example
RUN --mount=type=bind,from=envbuilder_feature_one,target=`+featureOneDir+`,rw _CONTAINER_USER="1000" _REMOTE_USER="1000" ./install.sh
RUN --mount=type=bind,from=envbuilder_feature_one,target=/.envbuilder/features/one,rw _CONTAINER_USER="1000" _REMOTE_USER="1000" ./install.sh
# Go potato - Example description!
WORKDIR `+featureTwoDir+`
WORKDIR /.envbuilder/features/two
ENV POTATO=example
RUN --mount=type=bind,from=envbuilder_feature_two,target=`+featureTwoDir+`,rw VERSION="potato" _CONTAINER_USER="1000" _REMOTE_USER="1000" ./install.sh
RUN --mount=type=bind,from=envbuilder_feature_two,target=/.envbuilder/features/two,rw VERSION="potato" _CONTAINER_USER="1000" _REMOTE_USER="1000" ./install.sh
USER 1000`, params.DockerfileContent)

require.Equal(t, map[string]string{
Expand Down
2 changes: 2 additions & 0 deletions devcontainer/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ func (s *Spec) Compile(featureRef, featureName, featureDir, containerUser, remot
sort.Strings(runDirective)
// See https://containers.dev/implementors/features/#invoking-installsh
if useBuildContexts {
// Use a deterministic target directory to make the resulting Dockerfile cacheable
featureDir = "/.envbuilder/features/" + featureName
fromDirective = "FROM scratch AS envbuilder_feature_" + featureName + "\nCOPY --from=" + featureRef + " / /\n"
runDirective = append([]string{"RUN", "--mount=type=bind,from=envbuilder_feature_" + featureName + ",target=" + featureDir + ",rw"}, runDirective...)
} else {
Expand Down
2 changes: 1 addition & 1 deletion devcontainer/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ func TestCompile(t *testing.T) {
fromDirective, runDirective, err := spec.Compile("coder/test:latest", "test", "/.envbuilder/feature/test-d8e8fc", "containerUser", "remoteUser", true, nil)
require.NoError(t, err)
require.Equal(t, "FROM scratch AS envbuilder_feature_test\nCOPY --from=coder/test:latest / /", strings.TrimSpace(fromDirective))
require.Equal(t, "WORKDIR /.envbuilder/feature/test-d8e8fc\nRUN --mount=type=bind,from=envbuilder_feature_test,target=/.envbuilder/feature/test-d8e8fc,rw _CONTAINER_USER=\"containerUser\" _REMOTE_USER=\"remoteUser\" ./install.sh", strings.TrimSpace(runDirective))
require.Equal(t, "WORKDIR /.envbuilder/features/test\nRUN --mount=type=bind,from=envbuilder_feature_test,target=/.envbuilder/features/test,rw _CONTAINER_USER=\"containerUser\" _REMOTE_USER=\"remoteUser\" ./install.sh", strings.TrimSpace(runDirective))
})
}

0 comments on commit 7b22c45

Please sign in to comment.