2

I am planning to automate spinning up container and run some commands on it. But I get the below error

docker run -it alpine sh ls

Error I get is

docker error : the input device is not a TTY.

So I removed interactive part and ran

docker run -t alpine sh ls

I don't get the shell but docker is spinning

I run above docker commands in golangs os.exec package.

package main
import (
    "fmt"
    "os"
    "os/exec"
    "sync"
)
func main() {
    var wg sync.WaitGroup
wg.Add(1)
    go func() {
        defer wg.Done()
        cmd := exec.Command("docker", "run","-it","alpine","sh","ls")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        // log.Println(cmd.Run())
    }()
}()
    wg.Wait()
}

My intention is to run multiple shell scripts after spinning up the docker.

Any help would be appreciated. Thanks

5
  • Can you provide the actual code you're using that uses os.exec? What is in MY_CONTAINER? These commands alone are not very helpful. Commented Jun 3, 2022 at 3:10
  • @tentative added the code and image name Commented Jun 3, 2022 at 3:20
  • Note that running sh ls doesn't make any sense; that's going to result in an error (sh: can't open 'ls': No such file or directory). You would want either sh -c ls or simply ls. Commented Jun 3, 2022 at 3:26
  • @larsks 'ls' is just for example. I added there Commented Jun 3, 2022 at 3:29
  • 2
    A Docker container is a wrapper around some single process. Does this workflow actually make sense if you substitute "process" for "container"; "start a process, get a shell inside it, and run some commands"? Also look at the Docker Go SDK, and remember that if you can start a container, you can very easily use that ability to root the entire host. Commented Jun 3, 2022 at 10:38

1 Answer 1

2

One avenue to explore is the official packages moby/moby integration-cli and moby/moby integration-cli/cli with cli.go.

The func Docker(cmd icmd.Cmd, cmdOperators ...CmdOperator) *icmd is made to run a docker command, with parameters.

Example, for docker run:

cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "sh", "-c", "sleep 30; echo hi"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.