Skip to content

Commit

Permalink
Remove redundant install stages
Browse files Browse the repository at this point in the history
  • Loading branch information
8W9aG committed Dec 23, 2024
1 parent 9199ee5 commit 79d7715
Showing 1 changed file with 0 additions and 91 deletions.
91 changes: 0 additions & 91 deletions pkg/dockerfile/standard_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,97 +475,6 @@ func (g *StandardGenerator) pipInstalls() (string, error) {
}, "\n"), nil
}

func (g *StandardGenerator) pipInstall() (string, error) {
copyLine, containerPath, err := g.writeTemp("requirements.txt", []byte(g.pythonRequirementsContents))
if err != nil {
return "", err
}

pipInstallLine := "RUN --mount=type=cache,target=/root/.cache/pip pip install -t /dep -r " + containerPath
if g.strip {
pipInstallLine += " && " + StripDebugSymbolsCommand
}

return strings.Join([]string{
copyLine[0],
CFlags,
pipInstallLine,
"ENV CFLAGS=",
}, "\n"), nil
}

func (g *StandardGenerator) pipInstallStage(aptInstalls string) (string, error) {
installCog, err := g.installCog()
if err != nil {
return "", err
}
g.pythonRequirementsContents, err = g.Config.PythonRequirementsForArch(g.GOOS, g.GOARCH, []string{})
if err != nil {
return "", err
}

pipStageImage := "python:" + g.Config.Build.PythonVersion
if strings.Trim(g.pythonRequirementsContents, "") == "" {
return `FROM ` + pipStageImage + ` as deps
` + aptInstalls + `
` + installCog, nil
}

console.Debugf("Generated requirements.txt:\n%s", g.pythonRequirementsContents)
copyLine, containerPath, err := g.writeTemp("requirements.txt", []byte(g.pythonRequirementsContents))
if err != nil {
return "", err
}

// Not slim, so that we can compile wheels
fromLine := `FROM ` + pipStageImage + ` as deps`
// Sometimes, in order to run `pip install` successfully, some system packages need to be installed
// or some other change needs to happen
// this is a bodge to support that
// it will be reverted when we add custom dockerfiles
buildStageDeps := os.Getenv("COG_EXPERIMENTAL_BUILD_STAGE_DEPS")
if buildStageDeps != "" {
fromLine = fromLine + "\nRUN " + buildStageDeps
}

pipInstallLine := "RUN --mount=type=cache,target=/root/.cache/pip pip install -t /dep -r " + containerPath
if g.strip {
pipInstallLine += " && " + StripDebugSymbolsCommand
}

lines := []string{
fromLine,
aptInstalls,
installCog,
copyLine[0],
CFlags,
pipInstallLine,
"ENV CFLAGS=",
}
return strings.Join(lines, "\n"), nil
}

// copyPipPackagesFromInstallStage copies the Python dependencies installed in the deps stage into the main image
func (g *StandardGenerator) copyPipPackagesFromInstallStage() string {
// placing packages in workdir makes imports faster but seems to break integration tests
// return "COPY --from=deps --link /dep COPY --from=deps /src"
// ...except it's actually /root/.pyenv/versions/3.8.17/lib/python3.8/site-packages
py := g.Config.Build.PythonVersion
if g.Config.Build.GPU && (g.useCudaBaseImage || g.IsUsingCogBaseImage()) {
// this requires buildkit!
// we should check for buildkit and otherwise revert to symlinks or copying into /src
// we mount to avoid copying, which avoids having two copies in this layer
return `
RUN --mount=type=bind,from=deps,source=/dep,target=/dep \
cp -rf /dep/* $(pyenv prefix)/lib/python*/site-packages; \
cp -rf /dep/bin/* $(pyenv prefix)/bin; \
pyenv rehash
`
}

return "COPY --from=deps --link /dep /usr/local/lib/python" + py + "/site-packages"
}

func (g *StandardGenerator) runCommands() (string, error) {
runCommands := g.Config.Build.Run

Expand Down

0 comments on commit 79d7715

Please sign in to comment.