Skip to content

Commit

Permalink
Merge pull request #1943 from AkihiroSuda/fix-1942
Browse files Browse the repository at this point in the history
compose: show detailed errors again
  • Loading branch information
AkihiroSuda committed Jan 30, 2023
2 parents 1bbfe69 + 9dae8e5 commit ca4a763
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
52 changes: 52 additions & 0 deletions cmd/nerdctl/compose_up_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright The containerd 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 main

import (
"fmt"
"runtime"
"testing"

"github.com/containerd/nerdctl/pkg/testutil"
"gotest.tools/v3/icmd"
)

// https://github.com/containerd/nerdctl/issues/1942
func TestComposeUpDetailedError(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("FIXME: test does not work on Windows yet (runtime \"io.containerd.runc.v2\" binary not installed \"containerd-shim-runc-v2.exe\": file does not exist)")
}
base := testutil.NewBase(t)
dockerComposeYAML := fmt.Sprintf(`
services:
foo:
image: %s
runtime: invalid
`, testutil.CommonImage)
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

c := base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d")
expected := icmd.Expected{
ExitCode: 1,
Err: `exec: \"invalid\": executable file not found in $PATH`,
}
if base.Target == testutil.Docker {
expected.Err = `Unknown runtime specified invalid`
}
c.Assert(expected)
}
6 changes: 2 additions & 4 deletions pkg/composer/serviceparser/serviceparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ func warnUnknownFields(svc types.ServiceConfig) {
}

type Container struct {
Name string // e.g., "compose-wordpress_wordpress_1"
Detached bool
RunArgs []string // {"--pull=never", ...}
Name string // e.g., "compose-wordpress_wordpress_1"
RunArgs []string // {"--pull=never", ...}
}

type Build struct {
Expand Down Expand Up @@ -462,7 +461,6 @@ func newContainer(project *types.Project, parsed *Service, i int) (*Container, e
c.Name = svc.ContainerName
}

c.Detached = true
c.RunArgs = []string{
"--name=" + c.Name,
"--pull=never", // because image will be ensured before running replicas with `nerdctl run`.
Expand Down
10 changes: 6 additions & 4 deletions pkg/composer/up_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparse
defer os.RemoveAll(tempDir)
cidFilename := filepath.Join(tempDir, "cid")

if container.Detached && !service.Unparsed.StdinOpen && !service.Unparsed.Tty {
var runFlagD bool
if !service.Unparsed.StdinOpen && !service.Unparsed.Tty {
container.RunArgs = append([]string{"-d"}, container.RunArgs...)
runFlagD = true
}

//add metadata labels to container https://github.com/compose-spec/compose-spec/blob/master/spec.md#labels
Expand All @@ -164,11 +166,11 @@ func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparse
if service.Unparsed.StdinOpen {
cmd.Stdin = os.Stdin
}

if !container.Detached {
if !runFlagD {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
// Always propagate stderr to print detailed error messages (https://github.com/containerd/nerdctl/issues/1942)
cmd.Stderr = os.Stderr

err = cmd.Run()
if err != nil {
Expand Down

0 comments on commit ca4a763

Please sign in to comment.