Skip to content

Commit 1dec010

Browse files
committed
Dockerfile使用ARG替换模板 && Dockerfile中添加USER
bottom USER make() > {}
1 parent fbe70d4 commit 1dec010

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

cluster/calcium/build_image.go

+36-27
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@ shift
4141
{{.Command}}
4242
`
4343

44-
const dockerFile = `FROM {{.Base}}
44+
const dockerFile = `ARG Base
45+
ARG Appdir
46+
ARG Appname
47+
ARG BuildRun
48+
ARG Reponame
49+
ARG UID
50+
FROM ${Base}
4551
ENV ERU 1
46-
ADD %s {{.Appdir}}/{{.Appname}}
52+
ADD ${Reponame} ${Appdir}/${Appname}
4753
ADD launcher /usr/local/bin/launcher
4854
ADD launcheroot /usr/local/bin/launcheroot
49-
WORKDIR {{.Appdir}}/{{.Appname}}
50-
RUN useradd -u %s -d /nonexistent -s /sbin/nologin -U {{.Appname}}
51-
RUN chown -R %s {{.Appdir}}/{{.Appname}}
52-
{{with .Build}}
53-
{{range $index, $value := .}}
54-
RUN {{$value}}
55-
{{end}}
56-
{{end}}
57-
`
55+
WORKDIR ${Appdir}/${Appname}
56+
RUN useradd -u ${UID} -d /nonexistent -s /sbin/nologin -U ${Appname}
57+
RUN chown -R ${UID} ${Appdir}/${Appname}
58+
RUN ${BuildRun}
59+
USER ${Appname}
60+
` // USER之后的layer都会以user的身份去RUN command,所以这里一定要把USER放到最下面
5861

5962
// richSpecs is used to format templates
6063
type richSpecs struct {
@@ -143,7 +146,7 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
143146
}
144147

145148
// use app.yaml file to create Specs instance
146-
// which we'll need to create Dockerfile later
149+
// which we'll need to generate build args later
147150
bytes, err := ioutil.ReadFile(filepath.Join(cloneDir, "app.yaml"))
148151
if err != nil {
149152
return ch, err
@@ -168,7 +171,7 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
168171
if err := createLauncher(buildDir, rs); err != nil {
169172
return ch, err
170173
}
171-
if err := createDockerfile(buildDir, reponame, rs); err != nil {
174+
if err := createDockerfile(buildDir); err != nil {
172175
return ch, err
173176
}
174177

@@ -181,6 +184,9 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
181184
return ch, err
182185
}
183186

187+
// generate build args
188+
buildArgs := generateBuildArgs(reponame, specs, rs)
189+
184190
// must be put here because of that `defer os.RemoveAll(buildDir)`
185191
buildOptions := enginetypes.ImageBuildOptions{
186192
Tags: []string{tag},
@@ -189,6 +195,7 @@ func (c *calcium) BuildImage(repository, version, uid, artifact string) (chan *t
189195
Remove: true,
190196
ForceRemove: true,
191197
PullParent: true,
198+
BuildArgs: buildArgs,
192199
}
193200
log.Infof("Building image %v with artifact %v at %v:%v", tag, artifact, buildPodname, node.Name)
194201
resp, err := node.Engine.ImageBuild(context.Background(), buildContext, buildOptions)
@@ -306,30 +313,32 @@ func createLauncher(buildDir string, rs richSpecs) error {
306313
}
307314

308315
// Dockerfile
309-
func createDockerfile(buildDir, reponame string, rs richSpecs) error {
316+
func createDockerfile(buildDir string) error {
310317
f, err := os.Create(filepath.Join(buildDir, "Dockerfile"))
311318
if err != nil {
312319
return err
313320
}
314321
defer f.Close()
315-
316-
dockerFileFormatted := fmt.Sprintf(dockerFile, reponame, rs.UID, rs.UID)
317-
t := template.New("docker file template")
318-
parsedTemplate, err := t.Parse(dockerFileFormatted)
319-
if err != nil {
320-
return err
321-
}
322-
err = parsedTemplate.Execute(f, rs)
323-
if err != nil {
324-
return err
325-
}
326-
327-
if err := f.Sync(); err != nil {
322+
if _, err := f.WriteString(dockerFile); err != nil {
328323
return err
329324
}
330325
return nil
331326
}
332327

328+
// generate build args
329+
func generateBuildArgs(reponame string, specs types.Specs, rs richSpecs) map[string]*string {
330+
buildArgs := map[string]*string{}
331+
buildArgs["Base"] = &(specs.Base)
332+
buildArgs["Appdir"] = &(rs.Appdir)
333+
buildArgs["Appname"] = &(rs.Appname)
334+
runCommands := strings.Join(specs.Build, " && ")
335+
buildArgs["BuildRun"] = &runCommands
336+
buildArgs["Reponame"] = &reponame
337+
buildArgs["UID"] = &rs.UID
338+
339+
return buildArgs
340+
}
341+
333342
// Image tag
334343
// 格式严格按照 Hub/HubPrefix/appname:version 来
335344
func createImageTag(config types.Config, appname, version string) string {

cluster/calcium/create_container.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func (c *calcium) makeContainerOptions(index int, quota map[string]int, specs ty
509509
user := specs.Appname
510510
// 如果是升级或者是raw, 就用root
511511
if entry.Privileged != "" || opts.Raw {
512-
user = ""
512+
user = "root"
513513
}
514514
// command and user
515515
slices := utils.MakeCommandLineArgs(entry.Command + " " + opts.ExtraArgs)
@@ -526,7 +526,7 @@ func (c *calcium) makeContainerOptions(index int, quota map[string]int, specs ty
526526
}
527527
slices = append([]string{fmt.Sprintf("/usr/local/bin/%s", starter), needNetwork}, slices...)
528528
// use default empty value, as root
529-
user = ""
529+
user = "root"
530530
}
531531
cmd := engineslice.StrSlice(slices)
532532

0 commit comments

Comments
 (0)