Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Last active March 5, 2021 08:45
Show Gist options
  • Save developer-guy/a448728dff9ad9b7946fd83660f25e4d to your computer and use it in GitHub Desktop.
Save developer-guy/a448728dff9ad9b7946fd83660f25e4d to your computer and use it in GitHub Desktop.
Login to the Dockerhub using golang exec and pty, also, Fix for “cannot perform an interactive login from a non TTY device”
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"github.com/creack/pty"
)
func main() {
executablePath, err := exec.LookPath("docker")
if err != nil {
log.Fatalf("could not found docker, err %v", err)
}
cmd := exec.Command(executablePath, "login", "docker.io")
ptmx, err := pty.Start(cmd)
if err != nil {
panic(err)
}
// Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort.
id := os.Getenv("DOCKER_USERNAME")
password := os.Getenv("DOCKER_USERNAME")
go func() {
ptmx.Write([]byte(id))
ptmx.Write([]byte(password))
}()
var execOut bytes.Buffer
io.Copy(&execOut, ptmx)
count := len(id) + len(password)
io.CopyN(ioutil.Discard, &execOut, int64(count))
log.Println(execOut.String())
}
@developer-guy
Copy link
Author

DOCKER_USERNAME=xxx DOCKER_PASSWORD=xxx go run -v ./main.go
command-line-arguments
2021/03/05 11:44:05 Authenticating with existing credentials...
Login Succeeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment