Fixes #5147
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
| ... | ... |
@@ -70,6 +70,9 @@ type buildFile struct {
|
| 70 | 70 |
// Deprecated, original writer used for ImagePull. To be removed. |
| 71 | 71 |
outOld io.Writer |
| 72 | 72 |
sf *utils.StreamFormatter |
| 73 |
+ |
|
| 74 |
+ // cmdSet indicates is CMD was setted in current Dockerfile |
|
| 75 |
+ cmdSet bool |
|
| 73 | 76 |
} |
| 74 | 77 |
|
| 75 | 78 |
func (b *buildFile) clearTmp(containers map[string]struct{}) {
|
| ... | ... |
@@ -306,12 +309,17 @@ func (b *buildFile) CmdCmd(args string) error {
|
| 306 | 306 |
if err := b.commit("", b.config.Cmd, fmt.Sprintf("CMD %v", cmd)); err != nil {
|
| 307 | 307 |
return err |
| 308 | 308 |
} |
| 309 |
+ b.cmdSet = true |
|
| 309 | 310 |
return nil |
| 310 | 311 |
} |
| 311 | 312 |
|
| 312 | 313 |
func (b *buildFile) CmdEntrypoint(args string) error {
|
| 313 | 314 |
entrypoint := b.buildCmdFromJson(args) |
| 314 | 315 |
b.config.Entrypoint = entrypoint |
| 316 |
+ // if there is no cmd in current Dockerfile - cleanup cmd |
|
| 317 |
+ if !b.cmdSet {
|
|
| 318 |
+ b.config.Cmd = nil |
|
| 319 |
+ } |
|
| 315 | 320 |
if err := b.commit("", b.config.Cmd, fmt.Sprintf("ENTRYPOINT %v", entrypoint)); err != nil {
|
| 316 | 321 |
return err |
| 317 | 322 |
} |
| ... | ... |
@@ -1310,8 +1310,8 @@ func TestBuildEntrypointRunCleanup(t *testing.T) {
|
| 1310 | 1310 |
if err != nil {
|
| 1311 | 1311 |
t.Fatal(err) |
| 1312 | 1312 |
} |
| 1313 |
- // Cmd inherited from busybox, maybe will be fixed in #5147 |
|
| 1314 |
- if expected := "[/bin/sh]"; res != expected {
|
|
| 1313 |
+ // Cmd must be cleaned up |
|
| 1314 |
+ if expected := "<no value>"; res != expected {
|
|
| 1315 | 1315 |
t.Fatalf("Cmd %s, expected %s", res, expected)
|
| 1316 | 1316 |
} |
| 1317 | 1317 |
logDone("build - cleanup cmd after RUN")
|
| ... | ... |
@@ -1875,3 +1875,36 @@ func TestBuildFromGIT(t *testing.T) {
|
| 1875 | 1875 |
} |
| 1876 | 1876 |
logDone("build - build from GIT")
|
| 1877 | 1877 |
} |
| 1878 |
+ |
|
| 1879 |
+func TestBuildCleanupCmdOnEntrypoint(t *testing.T) {
|
|
| 1880 |
+ name := "testbuildcmdcleanuponentrypoint" |
|
| 1881 |
+ defer deleteImages(name) |
|
| 1882 |
+ if _, err := buildImage(name, |
|
| 1883 |
+ `FROM scratch |
|
| 1884 |
+ CMD ["test"] |
|
| 1885 |
+ ENTRYPOINT ["echo"]`, |
|
| 1886 |
+ true); err != nil {
|
|
| 1887 |
+ t.Fatal(err) |
|
| 1888 |
+ } |
|
| 1889 |
+ if _, err := buildImage(name, |
|
| 1890 |
+ fmt.Sprintf(`FROM %s |
|
| 1891 |
+ ENTRYPOINT ["cat"]`, name), |
|
| 1892 |
+ true); err != nil {
|
|
| 1893 |
+ t.Fatal(err) |
|
| 1894 |
+ } |
|
| 1895 |
+ res, err := inspectField(name, "Config.Cmd") |
|
| 1896 |
+ if err != nil {
|
|
| 1897 |
+ t.Fatal(err) |
|
| 1898 |
+ } |
|
| 1899 |
+ if expected := "<no value>"; res != expected {
|
|
| 1900 |
+ t.Fatalf("Cmd %s, expected %s", res, expected)
|
|
| 1901 |
+ } |
|
| 1902 |
+ res, err = inspectField(name, "Config.Entrypoint") |
|
| 1903 |
+ if err != nil {
|
|
| 1904 |
+ t.Fatal(err) |
|
| 1905 |
+ } |
|
| 1906 |
+ if expected := "[cat]"; res != expected {
|
|
| 1907 |
+ t.Fatalf("Entrypoint %s, expected %s", res, expected)
|
|
| 1908 |
+ } |
|
| 1909 |
+ logDone("build - cleanup cmd on ENTRYPOINT")
|
|
| 1910 |
+} |