Skip to content

Instantly share code, notes, and snippets.

@eferro
Created December 31, 2023 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eferro/3b0f60d31df4c350774f99d54cd8f61e to your computer and use it in GitHub Desktop.
Save eferro/3b0f60d31df4c350774f99d54cd8f61e to your computer and use it in GitHub Desktop.
Simple wrapper to execute (interactive/non interactive) docker image
package main
import (
"fmt"
"log"
"os"
"os/exec"
)
const (
dockerRegistry = "XXXXXXXXXXXXX.dkr.ecr.eu-central-1.amazonaws.com"
dockerImage = dockerRegistry + "/XXXXXXXX"
cliLauncherScript = "non_interactive_cli_launcher.py"
)
func isStdinPipedToFile() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}
func prepareDockerParameters(allParameters []string, interactive bool) []string {
args := []string{"run", "--rm", "-u",
fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid()),
"-v", fmt.Sprintf("%s:/work/", os.Getenv("PWD")),
"-v", fmt.Sprintf("%s/.xxxxx:/opt/xxxx/", os.Getenv("HOME")),
"--hostname=myhostname"}
if interactive {
args = append(args, "-it")
} else if isStdinPipedToFile() {
args = append(args, "-i")
}
args = append(args, dockerImage)
if !interactive {
args = append(args, "python", cliLauncherScript)
}
args = append(args, allParameters...)
return args
}
func executeDocker(allParameters []string, interactive bool) {
args := prepareDockerParameters(allParameters, interactive)
cmd := exec.Command("docker", args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
func main() {
allParameters := os.Args[1:]
if len(allParameters) > 0 {
executeDocker(allParameters, false)
} else {
if isStdinPipedToFile() {
executeDocker(allParameters, false)
} else {
executeDocker(allParameters, true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment