Просмотр исходного кода

Merge pull request #1013 from gtardif/compose_build_fix

Fix compose up build when no image name in compose file
Guillaume Tardif 5 лет назад
Родитель
Сommit
4826d5155a

+ 3 - 5
local/build.go

@@ -56,7 +56,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
 			if imageName == "" {
 				imageName = project.Name + "_" + service.Name
 			}
-			opts[imageName] = s.toBuildOptions(service, project.WorkingDir)
+			opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
 			continue
 		}
 
@@ -116,11 +116,9 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
 	return err
 }
 
-func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string) build.Options {
+func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) build.Options {
 	var tags []string
-	if service.Image != "" {
-		tags = append(tags, service.Image)
-	}
+	tags = append(tags, imageTag)
 
 	if service.Build.Dockerfile == "" {
 		service.Build.Dockerfile = "Dockerfile"

+ 5 - 1
local/compose.go

@@ -66,7 +66,11 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro
 	opts := map[string]build.Options{}
 	for _, service := range project.Services {
 		if service.Build != nil {
-			opts[service.Name] = s.toBuildOptions(service, project.WorkingDir)
+			imageName := service.Image
+			if imageName == "" {
+				imageName = project.Name + "_" + service.Name
+			}
+			opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
 		}
 	}
 

+ 3 - 1
local/e2e/compose_test.go

@@ -94,7 +94,9 @@ func TestLocalComposeVolume(t *testing.T) {
 
 	const projectName = "compose-e2e-volume"
 
-	t.Run("up with volume", func(t *testing.T) {
+	t.Run("up with build and no image name, volume", func(t *testing.T) {
+		//ensure local test run does not reuse previously build image
+		c.RunDockerOrExitError("--context", "default", "rmi", "compose-e2e-volume_nginx")
 		c.RunDockerCmd("compose", "up", "-d", "--workdir", "volume-test", "--project-name", projectName)
 
 		output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)

+ 1 - 1
local/e2e/volume-test/docker-compose.yml

@@ -1,6 +1,6 @@
 services:
   nginx:
-    image: nginx
+    build: nginx-build
     volumes:
       - ./static:/usr/share/nginx/html
     ports:

+ 15 - 0
local/e2e/volume-test/nginx-build/Dockerfile

@@ -0,0 +1,15 @@
+#   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