Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 4826d51

Browse files
authored
Merge pull request #1013 from gtardif/compose_build_fix
Fix compose up build when no image name in compose file
2 parents 3a8d577 + d0b840b commit 4826d51

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

local/build.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
5656
if imageName == "" {
5757
imageName = project.Name + "_" + service.Name
5858
}
59-
opts[imageName] = s.toBuildOptions(service, project.WorkingDir)
59+
opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
6060
continue
6161
}
6262

@@ -116,11 +116,9 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts
116116
return err
117117
}
118118

119-
func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string) build.Options {
119+
func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string, imageTag string) build.Options {
120120
var tags []string
121-
if service.Image != "" {
122-
tags = append(tags, service.Image)
123-
}
121+
tags = append(tags, imageTag)
124122

125123
if service.Build.Dockerfile == "" {
126124
service.Build.Dockerfile = "Dockerfile"

local/compose.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro
6666
opts := map[string]build.Options{}
6767
for _, service := range project.Services {
6868
if service.Build != nil {
69-
opts[service.Name] = s.toBuildOptions(service, project.WorkingDir)
69+
imageName := service.Image
70+
if imageName == "" {
71+
imageName = project.Name + "_" + service.Name
72+
}
73+
opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName)
7074
}
7175
}
7276

local/e2e/compose_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ func TestLocalComposeVolume(t *testing.T) {
9494

9595
const projectName = "compose-e2e-volume"
9696

97-
t.Run("up with volume", func(t *testing.T) {
97+
t.Run("up with build and no image name, volume", func(t *testing.T) {
98+
//ensure local test run does not reuse previously build image
99+
c.RunDockerOrExitError("--context", "default", "rmi", "compose-e2e-volume_nginx")
98100
c.RunDockerCmd("compose", "up", "-d", "--workdir", "volume-test", "--project-name", projectName)
99101

100102
output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
nginx:
3-
image: nginx
3+
build: nginx-build
44
volumes:
55
- ./static:/usr/share/nginx/html
66
ports:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Docker Compose CLI authors
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM nginx

0 commit comments

Comments
 (0)