Go code:
package main
import (
"fmt"
"time"
)
func main() {
var local,_ = time.LoadLocation("Asia/Shanghai")
fmt.Println(time.Now())
fmt.Println(time.Now().In(local))
}
Dockerfile:
FROM scratch
COPY ./main /main
CMD [ "/main" ]
build.sh:
rm -rf main
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main .
docker build -t hello-go .
docker run hello-go
All the file are in one folder named hello-go. When I run my Go code in my osx. It works.
➜ hello-go go run main.go
2017-12-13 21:50:53.482933 +0800 CST m=+0.000350077
2017-12-13 21:50:53.483007 +0800 CST
When i build the docker image and start it. one error like this:
➜ hello-go ./build.sh
Sending build context to Docker daemon 1.937MB
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY ./main /main
---> 80da783f6c5d
Step 3/3 : CMD /main
---> Running in 85cb022b3ce2
---> 485ddd3a08dd
Removing intermediate container 85cb022b3ce2
Successfully built 485ddd3a08dd
Successfully tagged hello-go:latest
2017-12-13 13:52:59.861173734 +0000 UTC m=+0.000252335
panic: time: missing Location in call to Time.In
goroutine 1 [running]:
time.Time.In(0xbe846a0ef355744e, 0x4d607, 0x539b80, 0x0, 0x0, 0x0, 0xc420037f70)
/usr/local/go/src/time/time.go:1073 +0xc0
main.main()
/Users/liujichun/Desktop/workspace/docker/hello-go/main.go:11 +0xee
➜ hello-go
I know the scratch image is empty. What should i do to make it.
Starting from Go 1.15, time/tzdata is available, just import the package in your main package.
Reference: https://golang.org/pkg/time/tzdata/
Docker FROM alphine
RUN apk --no-cache add tzdata
see answer in: https://stackoverflow.com/a/62159987/4143613
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With