Go application hangs in Docker

5/3/2018

I have a very simple go application that works fine under Windows and Mac. (I am a golang newbie btw). The purpose of the application is to subscribe to a google cloud pubsub subscription and forward the messages to another application using REST.

The code looks like:

func getOrCreateTopic() {
    log.Infof("1")
    ctx := context.Background()

    log.Infof("2")
    topicName := viper.GetString("gce.pubsub.topic")
    log.Infof("3")
    topic = client.Topic(topicName)
    log.Infof("4")
    exists, _ := topic.Exists(ctx)
    log.Infof("5")
    if topic == nil || !exists {
        topic, err = client.CreateTopic(ctx, topicName)
        if err != nil {
            log.Errorf("Failed to create topic %s: %v", topicName, err)
            os.Exit(2)
        }
    }
}

func main() {
    ...
    getOrCreateTopic();
    ...
}

I created a docker container and published it to our kubernetes cluster in google cloud. What I see in the logs is everything until "4" and then nothing more. But the process is still shown as running in kubernetes.

I am lost, it seems like the call to the API just hangs.

-- SebastianStehle
docker
go
google-cloud-platform
kubernetes

1 Answer

2/25/2020

In case anyone else is encountering this same issue, I was able to solve it using the link from the OP. However, I looked past this link the first time I saw it because it mentioned using the cloud pubsub emulator.

I am not using the pubsub emulator in my project so I continued to read until I got to the section about SSL certificates. The absence of SSL certificates was the root cause of the hanging function.

I am using the scratch docker image in my project which does not include ssl certificates by default. I was able to resolve the hanging code by copying the ssl certificates from my golang build image in my multi-stage build.

FROM golang
COPY ./ /go/src/app
WORKDIR /go/src/app
RUN CGO_ENABLED=0 GOOS=linux go build -o /build/main

FROM scratch
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=0 /build/main /main
CMD ["/main"]

ps: I know this post is old, but I nearly missed the resolution so I wanted to help anyone else who might enounter this issue in the future.

-- Jaredp37
Source: StackOverflow