Docker run command from an HTTP API (gin-gonic)
package api
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
)
func Sdocker(c *gin.Context, image string, cmd ...string) {
args := append([]string{"run", "--rm", image}, cmd...)
out, err := exec.Command("docker", args...).CombinedOutput()
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.String(200, string(out))
}
func JSONdocker(c *gin.Context, image string, cmd ...string) {
args := append([]string{"run", "--rm", image}, cmd...)
out, err := exec.Command("docker", args...).CombinedOutput()
if err != nil {
c.JSON(500, gin.H{"message": fmt.Sprintf("%s (%s)", err.Error(), string(out))})
return
}
resp := make([]map[string]interface{}, 0)
err = json.Unmarshal(out, &resp)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, resp)
}