Skip to content

Commit 20cd4bd

Browse files
committed
Remove Dockerfile section labels
This was creating lots of unnecessary layers, which slows builds/pushes. Signed-off-by: Ben Firshman <[email protected]>
1 parent c6e521b commit 20cd4bd

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

pkg/dockerfile/generator.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@ import (
1111
"github.com/replicate/cog/pkg/model"
1212
)
1313

14-
const SectionPrefix = "### --> "
15-
16-
const (
17-
SectionStartingBuild = "Starting build"
18-
SectionInstallingSystemPackages = "Installing system packages"
19-
SectionInstallingPythonPrerequisites = "Installing Python prerequisites"
20-
SectionInstallingPython = "Installing Python"
21-
SectionInstallingPythonRequirements = "Installing Python requirements"
22-
SectionInstallingPythonPackages = "Installing Python packages"
23-
SectionInstallingCog = "Installing Cog"
24-
SectionCopyingCode = "Copying code"
25-
SectionPreInstall = "Running pre-install script"
26-
)
27-
2814
//go:embed embed/cog.whl
2915
var cogWheelEmbed []byte
3016

@@ -138,7 +124,7 @@ func (g *DockerfileGenerator) aptInstalls() (string, error) {
138124
if len(packages) == 0 {
139125
return "", nil
140126
}
141-
return g.sectionLabel(SectionInstallingSystemPackages) + "RUN apt-get update -qq && apt-get install -qy " +
127+
return "RUN apt-get update -qq && apt-get install -qy " +
142128
strings.Join(packages, " ") +
143129
" && rm -rf /var/lib/apt/lists/*", nil
144130
}
@@ -148,7 +134,7 @@ func (g *DockerfileGenerator) installPython() (string, error) {
148134

149135
py := g.Config.Environment.PythonVersion
150136

151-
return g.sectionLabel(SectionInstallingPythonPrerequisites) + `ENV PATH="/root/.pyenv/shims:/root/.pyenv/bin:$PATH"
137+
return `ENV PATH="/root/.pyenv/shims:/root/.pyenv/bin:$PATH"
152138
RUN apt-get update -q && apt-get install -qy --no-install-recommends \
153139
make \
154140
build-essential \
@@ -170,7 +156,7 @@ RUN apt-get update -q && apt-get install -qy --no-install-recommends \
170156
git \
171157
ca-certificates \
172158
&& rm -rf /var/lib/apt/lists/*
173-
` + g.sectionLabel(SectionInstallingPython+" "+g.Config.Environment.PythonVersion) + fmt.Sprintf(`RUN curl https://pyenv.run | bash && \
159+
` + fmt.Sprintf(`RUN curl https://pyenv.run | bash && \
174160
git clone https://github.com/momo-lab/pyenv-install-latest.git "$(pyenv root)"/plugins/pyenv-install-latest && \
175161
pyenv install-latest "%s" && \
176162
pyenv global $(pyenv install-latest --print "%s")`, py, py), nil
@@ -187,8 +173,7 @@ func (g *DockerfileGenerator) installCog() (string, error) {
187173
return "", fmt.Errorf("Failed to write %s: %w", cogFilename, err)
188174
}
189175
g.generatedPaths = append(g.generatedPaths, cogPath)
190-
return g.sectionLabel(SectionInstallingCog) +
191-
fmt.Sprintf(`COPY .cog/tmp/%s /tmp/%s
176+
return fmt.Sprintf(`COPY .cog/tmp/%s /tmp/%s
192177
RUN pip install /tmp/%s`, cogFilename, cogFilename, cogFilename), nil
193178
}
194179

@@ -197,7 +182,7 @@ func (g *DockerfileGenerator) pythonRequirements() (string, error) {
197182
if reqs == "" {
198183
return "", nil
199184
}
200-
return g.sectionLabel(SectionInstallingPythonRequirements) + fmt.Sprintf(`COPY %s /tmp/requirements.txt
185+
return fmt.Sprintf(`COPY %s /tmp/requirements.txt
201186
RUN pip install -r /tmp/requirements.txt && rm /tmp/requirements.txt`, reqs), nil
202187
}
203188

@@ -222,11 +207,11 @@ func (g *DockerfileGenerator) pipInstalls() (string, error) {
222207
extraIndexURLs += "--extra-index-url=" + indexURL
223208
}
224209

225-
return g.sectionLabel(SectionInstallingPythonPackages) + "RUN pip install " + findLinks + " " + extraIndexURLs + " " + strings.Join(packages, " "), nil
210+
return "RUN pip install " + findLinks + " " + extraIndexURLs + " " + strings.Join(packages, " "), nil
226211
}
227212

228213
func (g *DockerfileGenerator) copyCode() string {
229-
return g.sectionLabel(SectionCopyingCode) + `COPY . /src`
214+
return `COPY . /src`
230215
}
231216

232217
func (g *DockerfileGenerator) command() string {
@@ -241,15 +226,11 @@ func (g *DockerfileGenerator) preInstall() string {
241226
lines := []string{}
242227
for _, run := range g.Config.Environment.PreInstall {
243228
run = strings.TrimSpace(run)
244-
lines = append(lines, g.sectionLabel(SectionPreInstall+" "+run)+"RUN "+run)
229+
lines = append(lines, "RUN "+run)
245230
}
246231
return strings.Join(lines, "\n")
247232
}
248233

249-
func (g *DockerfileGenerator) sectionLabel(label string) string {
250-
return fmt.Sprintf("RUN %s%s\n", SectionPrefix, label)
251-
}
252-
253234
func filterEmpty(list []string) []string {
254235
filtered := []string{}
255236
for _, s := range list {

pkg/dockerfile/generator_test.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ import (
1111
)
1212

1313
func testInstallCog(generatedPaths []string) string {
14-
return `RUN ### --> Installing Cog
15-
COPY .cog/tmp/cog-0.0.1.dev-py3-none-any.whl /tmp/cog-0.0.1.dev-py3-none-any.whl
14+
return `COPY .cog/tmp/cog-0.0.1.dev-py3-none-any.whl /tmp/cog-0.0.1.dev-py3-none-any.whl
1615
RUN pip install /tmp/cog-0.0.1.dev-py3-none-any.whl`
1716
}
1817

1918
func testInstallPython(version string) string {
20-
return fmt.Sprintf(`RUN ### --> Installing Python prerequisites
21-
ENV PATH="/root/.pyenv/shims:/root/.pyenv/bin:$PATH"
19+
return fmt.Sprintf(`ENV PATH="/root/.pyenv/shims:/root/.pyenv/bin:$PATH"
2220
RUN apt-get update -q && apt-get install -qy --no-install-recommends \
2321
make \
2422
build-essential \
@@ -40,7 +38,6 @@ RUN apt-get update -q && apt-get install -qy --no-install-recommends \
4038
git \
4139
ca-certificates \
4240
&& rm -rf /var/lib/apt/lists/*
43-
RUN ### --> Installing Python 3.8
4441
RUN curl https://pyenv.run | bash && \
4542
git clone https://github.com/momo-lab/pyenv-install-latest.git "$(pyenv root)"/plugins/pyenv-install-latest && \
4643
pyenv install-latest "%s" && \
@@ -69,7 +66,6 @@ ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu
6966
` + testInstallCog(gen.generatedPaths) + `
7067
WORKDIR /src
7168
CMD ["python", "-m", "cog.server.http"]
72-
RUN ### --> Copying code
7369
COPY . /src`
7470

7571
gen = DockerfileGenerator{Config: conf, Arch: "gpu", Dir: tmpDir}
@@ -83,7 +79,6 @@ ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu
8379
` + testInstallPython("3.8") + testInstallCog(gen.generatedPaths) + `
8480
WORKDIR /src
8581
CMD ["python", "-m", "cog.server.http"]
86-
RUN ### --> Copying code
8782
COPY . /src`
8883

8984
require.Equal(t, expectedCPU, actualCPU)
@@ -117,16 +112,12 @@ ENV DEBIAN_FRONTEND=noninteractive
117112
ENV PYTHONUNBUFFERED=1
118113
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu
119114
` + testInstallCog(gen.generatedPaths) + `
120-
RUN ### --> Installing system packages
121115
RUN apt-get update -qq && apt-get install -qy ffmpeg cowsay && rm -rf /var/lib/apt/lists/*
122-
RUN ### --> Installing Python requirements
123116
COPY my-requirements.txt /tmp/requirements.txt
124117
RUN pip install -r /tmp/requirements.txt && rm /tmp/requirements.txt
125-
RUN ### --> Installing Python packages
126118
RUN pip install -f https://download.pytorch.org/whl/torch_stable.html torch==1.5.1+cpu pandas==1.2.0.12
127119
WORKDIR /src
128120
CMD ["python", "-m", "cog.server.http"]
129-
RUN ### --> Copying code
130121
COPY . /src`
131122

132123
gen = DockerfileGenerator{Config: conf, Arch: "gpu", Dir: tmpDir}
@@ -139,16 +130,12 @@ ENV PYTHONUNBUFFERED=1
139130
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu
140131
` + testInstallPython("3.8") +
141132
testInstallCog(gen.generatedPaths) + `
142-
RUN ### --> Installing system packages
143133
RUN apt-get update -qq && apt-get install -qy ffmpeg cowsay && rm -rf /var/lib/apt/lists/*
144-
RUN ### --> Installing Python requirements
145134
COPY my-requirements.txt /tmp/requirements.txt
146135
RUN pip install -r /tmp/requirements.txt && rm /tmp/requirements.txt
147-
RUN ### --> Installing Python packages
148136
RUN pip install torch==1.5.1 pandas==1.2.0.12
149137
WORKDIR /src
150138
CMD ["python", "-m", "cog.server.http"]
151-
RUN ### --> Copying code
152139
COPY . /src`
153140

154141
require.Equal(t, expectedCPU, actualCPU)

0 commit comments

Comments
 (0)