Jelajahi Sumber

Merge pull request #1246 from ulyssessouza/label-envfiles

Add environment_files label on service's `env_file` section
Guillaume Tardif 4 tahun lalu
induk
melakukan
66fb7903ad
3 mengubah file dengan 23 tambahan dan 2 penghapusan
  1. 2 0
      api/compose/tags.go
  2. 18 0
      cli/cmd/compose/up.go
  3. 3 2
      local/compose/labels.go

+ 2 - 0
api/compose/tags.go

@@ -25,4 +25,6 @@ const (
 	ServiceTag = "com.docker.compose.service"
 	// VolumeTag allow to track resource related to a compose volume
 	VolumeTag = "com.docker.compose.volume"
+	// EnvironmentFileLabel is set in containers with the option "--env-file" when set
+	EnvironmentFileLabel = "com.docker.compose.project.environment_file"
 )

+ 18 - 0
cli/cmd/compose/up.go

@@ -21,6 +21,7 @@ import (
 	"errors"
 	"fmt"
 	"os"
+	"path/filepath"
 
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/compose"
@@ -175,6 +176,23 @@ func setup(ctx context.Context, opts composeOptions, services []string) (*client
 			service.PullPolicy = types.PullPolicyBuild
 		}
 	}
+	if opts.EnvFile != "" {
+		var services types.Services
+		for _, s := range project.Services {
+			ef := opts.EnvFile
+			if ef != "" {
+				if !filepath.IsAbs(ef) {
+					ef = filepath.Join(project.WorkingDir, opts.EnvFile)
+				}
+				if s.Labels == nil {
+					s.Labels = make(map[string]string)
+				}
+				s.Labels[compose.EnvironmentFileLabel] = ef
+				services = append(services, s)
+			}
+		}
+		project.Services = services
+	}
 
 	return c, project, nil
 }

+ 3 - 2
local/compose/labels.go

@@ -21,8 +21,9 @@ import (
 	"strconv"
 	"strings"
 
-	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/docker/api/types/filters"
+
+	"github.com/docker/compose-cli/api/compose"
 )
 
 const (
@@ -38,7 +39,7 @@ const (
 	configHashLabel      = "com.docker.compose.config-hash"
 	networkLabel         = compose.NetworkTag
 
-	//ComposeVersion Compose version
+	// ComposeVersion Compose version
 	ComposeVersion = "1.0-alpha"
 )