ahmetb / go-dexec

It's like Go os/exec package but for Docker. What if you could exec programs remotely with the same interface as os/exec?
https://godoc.org/github.com/ahmetalpbalkan/go-dexec
Apache License 2.0
432 stars 38 forks source link

Missing output from fast-running commands #3

Open waitingkuo opened 8 years ago

waitingkuo commented 8 years ago

I wanna run sh -c "echo $(pwd)".

cmd is executed by dexec, but it doesn't output anything. cmd2 is executed by go's built-in exec , it works.

package main

import (
  "log"

  "github.com/ahmetalpbalkan/dexec"
  "github.com/fsouza/go-dockerclient"
  "os/exec"
)

func main() {
  cl, _ := docker.NewClient("unix:///var/run/docker.sock")
  d := dexec.Docker{cl}

  m, _ := dexec.ByCreatingContainer(docker.CreateContainerOptions{
    Config: &docker.Config{Image: "busybox"}})

  cmd := d.Command(m, "sh", "-c", "echo $(pwd)")
  b, err := cmd.Output()
  if err != nil {
    log.Fatal(err)
  }
  log.Printf("%s", b)

  cmd2 := exec.Command("sh", "-c", "echo $(pwd)")
  bb, err := cmd2.Output()
  if err != nil {
    log.Fatal(err)
  }
  log.Printf("%s", bb) 

}
ahmetb commented 8 years ago

@waitingkuo ah that's nasty. That sounds a bit like the known issue we got at https://godoc.org/github.com/ahmetalpbalkan/go-dexec#hdr-Known_issues

  • You may receive empty stdout/stderr from commands if the executed command does not end with a trailing new line or has a different flushing behavior.

Can you try appending an empty line echo after that? (echo $(pwd);echo)

waitingkuo commented 8 years ago

@ahmetalpbalkan it's still the same. Sometimes it can output correctly, sometimes empty.

ahmetb commented 8 years ago

@waitingkuo yep that flakiness actually is my experience with running fast commands as well while I developed this. It appears like there is either a flushing bug in the go-dockerclient's hijack protocol implementation I am using or in the docker engine. (Former is more likely.)

When you run slower commands (or add sleeps it is actually working fine). What I can suggest is, try using https://godoc.org/github.com/fsouza/go-dockerclient directly (you can take a look at dexec source code and see how we call into that library) and repro it with the go-dockerclient only, and open an issue on its repository.

That would fix the downstream.

ahmetb commented 8 years ago

@fsouza any ideas? Have you heard of any missing output cases from go-dockerclient attach/hijack functionality?

fsouza commented 8 years ago

@ahmetalpbalkan I haven't, but I can help debugging it. I'll try to reproduce it using only go-dockerclient.