转到代码:
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
所有文件都在一个名为hello-go的文件夹中。当我在我的osx中运行Go代码时。有用。
➜ 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
当我构建docker镜像并启动它时。像这样的一个错误:
➜ 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
我知道刮痕图片是空的。我该怎么做才能做到。
答案 0 :(得分:2)
使用此库https://godoc.org/4d63.com/tz
他们的示例(请参见TaskSchedule:
Type: AWS::Events::Rule
DeletionPolicy: Delete
Properties:
Description: >
Run every two hours.
ScheduleExpression: !Ref TaskRate
State: ENABLED
Targets:
- Id: ECSTarget
Arn: !GetAtt ecsCluster.Arn
EcsParameters:
TaskCount: 1
TaskDefinitionArn: !Ref TaskDefinitionDaily
)
tz.LoadLocation
答案 1 :(得分:1)
答案 2 :(得分:1)
我认为解决此问题的正确方法是按照库的要求导入 tzdata。您可以使用 TZ
环境变量。
我在 GitHub here
上找到的 Dockerfile 中指出了解决方案总结一切:
FROM golang:alpine AS build
RUN apk update && apk add ca-certificates && apk add tzdata
WORKDIR /app
ADD . .
RUN CGO_ENABLED=0 GOOS=linux go build -o myapp
FROM scratch AS final
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /app/myapp /
ENV TZ Australia/Sydney
ENTRYPOINT ["/myapp"]
答案 3 :(得分:0)
从 Go 1.15 开始,time/tzdata 可用,只需在主包中导入包即可。