浏览代码

Merge pull request #1381 from gtardif/fix_up_build

Fix compose up --build not forcing rebuild of present image
Nicolas De loof 4 年之前
父节点
当前提交
12ffdd1405

+ 5 - 5
local/compose/build.go

@@ -69,11 +69,6 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
 			return err
 		}
 
-		if service.Image != "" {
-			if localImagePresent {
-				continue
-			}
-		}
 		if service.Build != nil {
 			if localImagePresent && service.PullPolicy != types.PullPolicyBuild {
 				continue
@@ -82,6 +77,11 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
 			opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
 			continue
 		}
+		if service.Image != "" {
+			if localImagePresent {
+				continue
+			}
+		}
 
 		// Buildx has no command to "just pull", see
 		// so we bake a temporary dockerfile that will just pull and export pulled image

+ 9 - 1
local/e2e/compose/compose_build_test.go

@@ -53,6 +53,7 @@ func TestLocalComposeBuild(t *testing.T) {
 		})
 
 		res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
+		res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
 
 		output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
 		assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
@@ -64,7 +65,14 @@ func TestLocalComposeBuild(t *testing.T) {
 	t.Run("no rebuild when up again", func(t *testing.T) {
 		res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d")
 
-		assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout())
+		assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static"), res.Stdout())
+	})
+
+	t.Run("rebuild when up --build", func(t *testing.T) {
+		res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d", "--build")
+
+		res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
+		res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
 	})
 
 	t.Run("cleanup build project", func(t *testing.T) {

+ 1 - 1
local/e2e/compose/fixtures/build-test/compose.yml

@@ -5,5 +5,5 @@ services:
       - 8070:80
 
   nginx2:
-    build: nginx-build
+    build: nginx-build2
     image: custom-nginx

+ 17 - 0
local/e2e/compose/fixtures/build-test/nginx-build2/Dockerfile

@@ -0,0 +1,17 @@
+#   Copyright 2020 Docker Compose CLI authors
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+
+#       http://www.apache.org/licenses/LICENSE-2.0
+
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+FROM nginx
+
+COPY static2 /usr/share/nginx/html

+ 10 - 0
local/e2e/compose/fixtures/build-test/nginx-build2/static2/index.html

@@ -0,0 +1,10 @@
+<!doctype html>
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>Docker Nginx</title>
+</head>
+<body>
+  <h2>Hello from Nginx container</h2>
+</body>
+</html>