Skip to content

Commit bdc0e8f

Browse files
author
Victor Vieux
committed
Merge pull request moby#1405 from jonasi/entrypoint-noargs
*Runtime: Allow ENTRYPOINT without CMD
2 parents 1b08ab9 + d00fb40 commit bdc0e8f

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

builder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func (builder *Builder) Create(config *Config) (*Container, error) {
3838
MergeConfig(config, img.Config)
3939
}
4040

41-
if config.Cmd == nil || len(config.Cmd) == 0 {
41+
if len(config.Entrypoint) != 0 && config.Cmd == nil {
42+
config.Cmd = []string{}
43+
} else if config.Cmd == nil || len(config.Cmd) == 0 {
4244
return nil, fmt.Errorf("No command specified")
4345
}
4446

buildfile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func (b *buildFile) CmdRun(args string) error {
9393
b.config.Cmd = nil
9494
MergeConfig(b.config, config)
9595

96+
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
97+
9698
utils.Debugf("Command to be executed: %v", b.config.Cmd)
9799

98100
if b.utilizeCache {
@@ -115,7 +117,7 @@ func (b *buildFile) CmdRun(args string) error {
115117
if err := b.commit(cid, cmd, "run"); err != nil {
116118
return err
117119
}
118-
b.config.Cmd = cmd
120+
119121
return nil
120122
}
121123

buildfile_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,40 @@ func TestBuildEntrypoint(t *testing.T) {
328328
}
329329
}
330330

331+
// testing #1405 - config.Cmd does not get cleaned up if
332+
// utilizing cache
333+
func TestBuildEntrypointRunCleanup(t *testing.T) {
334+
runtime, err := newTestRuntime()
335+
if err != nil {
336+
t.Fatal(err)
337+
}
338+
defer nuke(runtime)
339+
340+
srv := &Server{
341+
runtime: runtime,
342+
pullingPool: make(map[string]struct{}),
343+
pushingPool: make(map[string]struct{}),
344+
}
345+
346+
img := buildImage(testContextTemplate{`
347+
from {IMAGE}
348+
run echo "hello"
349+
`,
350+
nil, nil}, t, srv, true)
351+
352+
img = buildImage(testContextTemplate{`
353+
from {IMAGE}
354+
run echo "hello"
355+
add foo /foo
356+
entrypoint ["/bin/echo"]
357+
`,
358+
[][2]string{{"foo", "HEYO"}}, nil}, t, srv, true)
359+
360+
if len(img.Config.Cmd) != 0 {
361+
t.Fail()
362+
}
363+
}
364+
331365
func TestBuildImageWithCache(t *testing.T) {
332366
runtime, err := newTestRuntime()
333367
if err != nil {

container_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,28 @@ func TestEntrypoint(t *testing.T) {
996996
}
997997
}
998998

999+
func TestEntrypointNoCmd(t *testing.T) {
1000+
runtime := mkRuntime(t)
1001+
defer nuke(runtime)
1002+
container, err := NewBuilder(runtime).Create(
1003+
&Config{
1004+
Image: GetTestImage(runtime).ID,
1005+
Entrypoint: []string{"/bin/echo", "foobar"},
1006+
},
1007+
)
1008+
if err != nil {
1009+
t.Fatal(err)
1010+
}
1011+
defer runtime.Destroy(container)
1012+
output, err := container.Output()
1013+
if err != nil {
1014+
t.Fatal(err)
1015+
}
1016+
if strings.Trim(string(output), "\r\n") != "foobar" {
1017+
t.Error(string(output))
1018+
}
1019+
}
1020+
9991021
func grepFile(t *testing.T, path string, pattern string) {
10001022
f, err := os.Open(path)
10011023
if err != nil {

0 commit comments

Comments
 (0)