From d0b840bcd1a2f17bc34bb8cbae27190e7decd377 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 4 Dec 2020 17:37:41 +0100 Subject: [PATCH] Fix compose up build when no image name in compose file. Signed-off-by: Guillaume Tardif --- local/build.go | 8 +++----- local/compose.go | 6 +++++- local/e2e/compose_test.go | 4 +++- local/e2e/volume-test/docker-compose.yml | 2 +- local/e2e/volume-test/nginx-build/Dockerfile | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 local/e2e/volume-test/nginx-build/Dockerfile diff --git a/local/build.go b/local/build.go index 2d1a5a130..56ace5cf7 100644 --- a/local/build.go +++ b/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" diff --git a/local/compose.go b/local/compose.go index 2fb7cc5e2..3caabdd66 100644 --- a/local/compose.go +++ b/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) } } diff --git a/local/e2e/compose_test.go b/local/e2e/compose_test.go index 9a2211ecb..e7fa0d83c 100644 --- a/local/e2e/compose_test.go +++ b/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) diff --git a/local/e2e/volume-test/docker-compose.yml b/local/e2e/volume-test/docker-compose.yml index 09afab385..0c1df0c25 100644 --- a/local/e2e/volume-test/docker-compose.yml +++ b/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: diff --git a/local/e2e/volume-test/nginx-build/Dockerfile b/local/e2e/volume-test/nginx-build/Dockerfile new file mode 100644 index 000000000..865740ce6 --- /dev/null +++ b/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 \ No newline at end of file