Compare commits

...

5 Commits

Author SHA1 Message Date
Ulysses Souza
0ba46049db Fix bind mounts when in project volumes definition
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
2022-06-01 23:49:51 +02:00
Guillaume Lours
6756732fe4 Merge pull request #9512 from ulyssessouza/import-dotenv-to-osenv
Import dotenv file to os environment
2022-05-30 18:00:45 +02:00
Ulysses Souza
67c13cf821 Import dotenv file to os environment
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
2022-05-30 17:36:41 +02:00
Guillaume Lours
dbafb02377 Merge pull request #9499 from glours/add-envs-e2e-tests
add e2e tests to verify env variables priority
2022-05-30 17:19:46 +02:00
Guillaume Lours
a1b3f95709 add e2e tests to verify env variables priority
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
2022-05-30 17:11:49 +02:00
19 changed files with 335 additions and 12 deletions

View File

@@ -27,6 +27,7 @@ import (
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
composegoutils "github.com/compose-spec/compose-go/utils"
dockercli "github.com/docker/cli/cli"
"github.com/docker/cli/cli-plugins/manager"
"github.com/docker/cli/cli/command"
@@ -257,6 +258,10 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
}
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := setEnvWithDotEnv(&opts)
if err != nil {
return err
}
parent := cmd.Root()
if parent != nil {
parentPrerun := parent.PersistentPreRunE
@@ -333,3 +338,27 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command {
command.Flags().MarkHidden("verbose") //nolint:errcheck
return command
}
func setEnvWithDotEnv(prjOpts *projectOptions) error {
options, err := prjOpts.toProjectOptions()
if err != nil {
return compose.WrapComposeError(err)
}
workingDir, err := options.GetWorkingDir()
if err != nil {
return err
}
envFromFile, err := cli.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), workingDir, options.EnvFile)
if err != nil {
return err
}
for k, v := range envFromFile {
if _, ok := os.LookupEnv(k); !ok {
if err = os.Setenv(k, v); err != nil {
return err
}
}
}
return nil
}

6
go.mod
View File

@@ -6,7 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.2
github.com/buger/goterm v1.0.4
github.com/cnabio/cnab-to-oci v0.3.1-beta1
github.com/compose-spec/compose-go v1.2.6
github.com/compose-spec/compose-go v1.2.7
github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.6.2
github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e
@@ -69,7 +69,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -123,7 +123,7 @@ require (
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
k8s.io/apimachinery v0.23.4 // indirect; see replace for the actual version used
k8s.io/client-go v0.23.4 // indirect; see replace for the actual version used
k8s.io/klog/v2 v2.30.0 // indirect

10
go.sum
View File

@@ -301,8 +301,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/compose-spec/compose-go v1.0.8/go.mod h1:REnCbBugoIdHB7S1sfkN/aJ7AJpNApGNjNiVjA9L8x4=
github.com/compose-spec/compose-go v1.2.6 h1:9l2Y/yNn/OSngnkUBP8h8CchTmMcf1MW4BeUEaZXy8k=
github.com/compose-spec/compose-go v1.2.6/go.mod h1:cg8yTeI+7rfQsEW9XHOLx0sNVjGKEXr6XwVB4fxmG3A=
github.com/compose-spec/compose-go v1.2.7 h1:eqKGZhdOQEGKW/FmFDt4xyEPhCpbA5dr0PkcWD895aI=
github.com/compose-spec/compose-go v1.2.7/go.mod h1:Jl9L8zJrt4aGY1XAz03DvHAu8V3/f00TK+uJL4BayDU=
github.com/compose-spec/godotenv v1.1.1/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
@@ -870,8 +870,9 @@ github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9FCCAVRQ=
@@ -2139,8 +2140,9 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=

View File

@@ -718,9 +718,15 @@ MOUNTS:
// so `Bind` API is used here with raw volume string
// see https://github.com/moby/moby/issues/43483
for _, v := range service.Volumes {
if v.Target == m.Target && v.Bind != nil && v.Bind.CreateHostPath {
binds = append(binds, v.String())
continue MOUNTS
if v.Target == m.Target {
switch {
case string(m.Type) != v.Type:
v.Source = m.Source
fallthrough
case v.Bind != nil && v.Bind.CreateHostPath:
binds = append(binds, v.String())
continue MOUNTS
}
}
}
}
@@ -911,10 +917,14 @@ func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.
}
}
bind, vol, tmpfs := buildMountOptions(volume)
bind, vol, tmpfs := buildMountOptions(project, volume)
volume.Target = path.Clean(volume.Target)
if bind != nil {
volume.Type = types.VolumeTypeBind
}
return mount.Mount{
Type: mount.Type(volume.Type),
Source: source,
@@ -927,7 +937,7 @@ func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.
}, nil
}
func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
func buildMountOptions(project types.Project, volume types.ServiceVolumeConfig) (*mount.BindOptions, *mount.VolumeOptions, *mount.TmpfsOptions) {
switch volume.Type {
case "bind":
if volume.Volume != nil {
@@ -944,6 +954,11 @@ func buildMountOptions(volume types.ServiceVolumeConfig) (*mount.BindOptions, *m
if volume.Tmpfs != nil {
logrus.Warnf("mount of type `volume` should not define `tmpfs` option")
}
if v, ok := project.Volumes[volume.Source]; ok && v.DriverOpts["o"] == types.VolumeTypeBind {
return buildBindOption(&types.ServiceVolumeBind{
CreateHostPath: true,
}), nil, nil
}
return nil, buildVolumeOptions(volume.Volume), nil
case "tmpfs":
if volume.Bind != nil {

View File

@@ -116,6 +116,9 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts
if len(opts.Environment) > 0 {
env := types.NewMappingWithEquals(opts.Environment)
projectEnv := env.Resolve(func(s string) (string, bool) {
if _, ok := service.Environment[s]; ok {
return "", false
}
v, ok := project.Environment[s]
return v, ok
}).RemoveEmpty()

View File

@@ -0,0 +1,169 @@
/*
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.
*/
package e2e
import (
"os"
"strings"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
func TestEnvPriority(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)
projectDir := "./fixtures/environment/env-priority"
t.Run("up", func(t *testing.T) {
c.RunDockerOrExitError("rmi", "env-compose-priority")
c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
"--project-directory", projectDir, "up", "-d", "--build")
})
// Full options activated
// 1. Compose file <-- Result expected
// 2. Shell environment variables
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("compose file priority", func(t *testing.T) {
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File")
})
// No Compose file, all other options
// 1. Compose file
// 2. Shell environment variables <-- Result expected
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("shell priority", func(t *testing.T) {
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
})
// No Compose file and env variable pass to the run command
// 1. Compose file
// 2. Shell environment variables <-- Result expected
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("shell priority from run command", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI=shell-run", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell-run")
})
// No Compose file & no env variable but override env file
// 1. Compose file
// 2. Shell environment variables
// 3. Environment file <-- Result expected
// 4. Dockerfile
// 5. Variable is not defined
t.Run("override env file", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
})
// No Compose file & no env variable but override env file
// 1. Compose file
// 2. Shell environment variables
// 3. Environment file <-- Result expected
// 4. Dockerfile
// 5. Variable is not defined
t.Run("env file", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Env File")
})
// No Compose file & no env variable, using an empty override env file
// 1. Compose file
// 2. Shell environment variables
// 3. Environment file
// 4. Dockerfile <-- Result expected
// 5. Variable is not defined
t.Run("use Dockerfile", func(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-priority/compose.yaml",
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.empty",
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Dockerfile")
})
t.Run("down", func(t *testing.T) {
c.RunDockerComposeCmd("--project-directory", projectDir, "down")
})
}
func TestEnvInterpolation(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)
projectDir := "./fixtures/environment/env-interpolation"
// No variable defined in the Compose file and env variable pass to the run command
// 1. Compose file
// 2. Shell environment variables <-- Result expected
// 3. Environment file
// 4. Dockerfile
// 5. Variable is not defined
t.Run("shell priority from run command", func(t *testing.T) {
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-interpolation/compose.yaml",
"--project-directory", projectDir, "config")
res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
})
}
func TestCommentsInEnvFile(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)
projectDir := "./fixtures/environment/env-file-comments"
t.Run("comments in env files", func(t *testing.T) {
c.RunDockerOrExitError("rmi", "env-file-comments")
c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-comments/compose.yaml",
"--project-directory", projectDir, "up", "-d", "--build")
res := c.RunDockerComposeCmd("-f", "./fixtures/environment/env-file-comments/compose.yaml",
"--project-directory", projectDir, "run", "--rm",
"-e", "COMMENT", "-e", "NO_COMMENT", "env-file-comments")
res.Assert(t, icmd.Expected{Out: `COMMENT=1234`})
res.Assert(t, icmd.Expected{Out: `NO_COMMENT=1234#5`})
c.RunDockerComposeCmd("--project-directory", projectDir, "down", "--rmi", "all")
})
}

View File

@@ -0,0 +1,2 @@
COMMENT=1234#5
NO_COMMENT="1234#5"

View File

@@ -0,0 +1,18 @@
# 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 alpine
ENV COMMENT=Dockerfile
ENV NO_COMMENT=Dockerfile
CMD ["sh", "-c", "printenv", "|", "grep", "COMMENT"]

View File

@@ -0,0 +1,5 @@
services:
env-file-comments:
build:
context: .
image: env-file-comments

View File

@@ -0,0 +1,2 @@
WHEREAMI=Env File
IMAGE=default_env:${WHEREAMI}

View File

@@ -0,0 +1,6 @@
services:
env-interpolation:
image: bash
environment:
IMAGE: ${IMAGE}
command: echo "$IMAGE"

View File

@@ -0,0 +1 @@
WHEREAMI=Env File

View File

@@ -0,0 +1 @@
WHEREAMI=override

View File

@@ -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 alpine
ENV WHEREAMI=Dockerfile
CMD ["printenv", "WHEREAMI"]

View File

@@ -0,0 +1,7 @@
services:
env-compose-priority:
image: env-compose-priority
build:
context: .
environment:
WHEREAMI: "Compose File"

View File

@@ -0,0 +1,3 @@
services:
env-compose-priority:
image: env-compose-priority

View File

@@ -167,6 +167,19 @@ func (c *E2eCLI) NewCmd(command string, args ...string) icmd.Cmd {
}
}
// NewCmdWithEnv creates a cmd object configured with the test environment set with additional env vars
func (c *E2eCLI) NewCmdWithEnv(envvars []string, command string, args ...string) icmd.Cmd {
env := append(os.Environ(),
append(envvars,
"DOCKER_CONFIG="+c.ConfigDir,
"KUBECONFIG=invalid")...
)
return icmd.Cmd{
Command: append([]string{command}, args...),
Env: env,
}
}
// MetricsSocket get the path where test metrics will be sent
func (c *E2eCLI) MetricsSocket() string {
return filepath.Join(c.ConfigDir, "./docker-cli.sock")

View File

@@ -18,11 +18,14 @@ package e2e
import (
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
func TestLocalComposeVolume(t *testing.T) {
@@ -88,3 +91,30 @@ func TestLocalComposeVolume(t *testing.T) {
assert.Assert(t, !strings.Contains(ls, "myvolume"))
})
}
func TestProjectVolumeBind(t *testing.T) {
if composeStandaloneMode {
t.Skip()
}
c := NewParallelE2eCLI(t, binDir)
const projectName = "compose-e2e-project-volume-bind"
t.Run("up with build and no image name, volume", func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", projectName)
assert.NilError(t, err)
defer os.RemoveAll(tmpDir)
c.RunDockerComposeCmd("--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "down")
c.RunDockerOrExitError("volume", "rm", "-f", projectName+"_project_data").Assert(t, icmd.Success)
cmd := c.NewCmdWithEnv([]string{"TEST_DIR=" + tmpDir},
"docker", "compose", "--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "up", "-d")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
defer c.RunDockerComposeCmd("--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "down")
c.RunCmd("sh", "-c", "echo SUCCESS > "+filepath.Join(tmpDir, "resultfile")).Assert(t, icmd.Success)
ret := c.RunDockerOrExitError("exec", "frontend", "bash", "-c", "cat /data/resultfile").Assert(t, icmd.Success)
assert.Assert(t, strings.Contains(ret.Stdout(), "SUCCESS"))
})
}