Go App可以在Docker中运行 - 意外结束JSON输入

问题描述 投票:1回答:1

我有以下控制器使用Go内置的包装器进行外部API调用。问题是,如果我运行没有docker的服务器,端点将返回有效数据。但是,当我从docker中运行它时,我得到的错误是unexpected end of JSON input

home.go

package controllers

import (
  "fmt"
  "encoding/json"

  "net/http"
  "time"
  "strconv"

  cmc "github.com/coincircle/go-coinmarketcap"
)

type HomeController struct{}

func NewHomeController() *HomeController {
  return &HomeController{}
}

func (hc HomeController) IndexEndpoint(w http.ResponseWriter, r *http.Request) {
  threeMonths := int64(60 * 60 * 24 * 90)
  now := time.Now()
  secs := now.Unix()
  start := secs - threeMonths
  end := secs

  fmt.Println("Time is " + strconv.FormatInt(end, 10))

  graph, _ := cmc.TickerGraph(&cmc.TickerGraphOptions{
    Start: start,
    End: end,
    Symbol: "ETH",
  })

  fmt.Println(graph)

  w.Header().Set("Access-Control-Allow-Origin", "*")
  w.Header().Set("Content-Type", "application/json")
  w.WriteHeader(http.StatusCreated)

  json.NewEncoder(w).Encode(graph)
}

这是我的码头设置:

Dockerfile

FROM golang:latest AS builder

COPY . $GOPATH/src/github.com/gohuygo/cryptodemo-api
WORKDIR $GOPATH/src/github.com/gohuygo/cryptodemo-api

RUN go get ./

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /app .

FROM scratch
COPY --from=builder /app ./
ENTRYPOINT ["./app"]

当涉及码头工具时,为什么抱怨坏json(即如何解决这个问题)?

谢谢

docker go
1个回答
3
投票

您的go应用程序可能尝试进行传出的HTTPS连接,但scratch容器不包含验证TLS证书所需的CA证书。

在这种情况下,考虑使用centurylink/ca-certs而不是scratch。它包括CA证书,您的程序应自动使用它们。

© www.soinside.com 2019 - 2024. All rights reserved.