我对docker非常陌生,我不知道如何在运行时接受参数。我的代码如下所示:
package main
import (
"fmt"
//"flag"
"os"
"net/http"
"io/ioutil"
"reflect"
)
func main() {
var args string
// flag.Parse()
// args := flag.Args()
fmt.Println("Enter the URL : ")
fmt.Scanf("%s ",&args)
fmt.Println(args)
if len(args) < 1 {
fmt.Println(reflect.TypeOf(args),"Please Enter the URL")
os.Exit(1)
}
retrieve(args) //call the retrieve function
}
func retrieve(url string){ //gives the source code as
output.
resp, err := http.Get(url)
if err != nil{
fmt.Println("read error is:", err)
return
}
body, err := ioutil.ReadAll(resp.Body);
if err != nil{
fmt.Println("read error is:", err)
return
} else{
fmt.Println(string(body))
}
}
Dockerfile看起来像这样:
FROM golang:1.7-alpine
ADD . /home
WORKDIR /home
CMD ["go","run","fetchSource.go"]
我已经注释了无法正常工作的代码。我只想在运行时接受参数,以便取消注释这些行。
我已经尝试在Dockerfile中进行以下更改,并且它也起作用了。
FROM golang:1.7-alpine
ADD . /home
WORKDIR /home
# This builds a binary first
RUN ["go", "build"]
# Now you can run the executable and pass arguments at the run time.
ENTRYPOINT ["./home"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句