Docker构建失败,由于kafka未定义的错误。

huangapple go评论95阅读模式
英文:

Docker build fails due to kafka undefined error

问题

当我尝试构建我们的Go应用程序时,我们遇到了以下错误。

=> ERROR [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o myapp
 > [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o myapp:
#14 6.962 # main
#14 6.962 ./kafkaproducer.go:12:12: undefined: kafka.NewProducer
#14 6.962 ./kafkaproducer.go:12:31: undefined: kafka.ConfigMap
#14 6.962 ./kafkaproducer.go:23:10: undefined: kafka.Message
#14 6.962 ./kafkaproducer.go:39:13: undefined: kafka.Message

我的Docker文件如下:

FROM golang:1.16-alpine AS builder
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -o myapp
FROM busybox AS prod
COPY --from=builder /app .
CMD ["./myapp"]

在我的kafkaProducer go文件中,我导入了以下内容:

import (
	"fmt"
	"log"

	"github.com/confluentinc/confluent-kafka-go/kafka"
)

当我在本地构建应用程序时,它成功构建,但是通过docker构建时失败,因为无法下载kafka依赖项并且抛出未定义错误。请帮我解决这个问题。

英文:

When I am trying to build our go application we are getting the below error.

=> ERROR [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o myapp
 > [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o myapp:
#14 6.962 # main
#14 6.962 ./kafkaproducer.go:12:12: undefined: kafka.NewProducer
#14 6.962 ./kafkaproducer.go:12:31: undefined: kafka.ConfigMap
#14 6.962 ./kafkaproducer.go:23:10: undefined: kafka.Message
#14 6.962 ./kafkaproducer.go:39:13: undefined: kafka.Message

My Docker file is

FROM golang:1.16-alpine AS builder
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -o myapp
FROM busybox AS prod
COPY --from=builder /app .
CMD ["./myapp"]

In my kafkaProducer go file I am importing this

import (
	"fmt"
	"log"

	"github.com/confluentinc/confluent-kafka-go/kafka"
)

My application is built successfully when I build it locally but through docker build is getting failed as it is unable to download kafka dependency and undefined error is thrown.
Please help me fix this issue

答案1

得分: 2

所以在构建时似乎与cgo有关。我通过使用cgo进行编译并静态链接依赖项来解决了这个问题。

FROM golang AS builder

WORKDIR /app
COPY . .
RUN go mod download && \
    go build -o myapp -ldflags '-linkmode external -w -extldflags "-static"';


# 也可以使用alpine
FROM busybox 

COPY --from=builder /app/myapp myapp
CMD ["./myapp"]

我使用下面的代码(来自他们的存储库)进行测试,最初给出了你展示的错误。

package main

import (
	"fmt"

	"github.com/confluentinc/confluent-kafka-go/kafka"
)

func main() {

	p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
	if err != nil {
		panic(err)
	}

	defer p.Close()

	// 用于处理已生产消息的交付报告处理程序
	go func() {
		for e := range p.Events() {
			switch ev := e.(type) {
			case *kafka.Message:
				if ev.TopicPartition.Error != nil {
					fmt.Printf("交付失败:%v\n", ev.TopicPartition)
				} else {
					fmt.Printf("已交付消息到:%v\n", ev.TopicPartition)
				}
			}
		}
	}()

	// 异步地向主题生产消息
	topic := "myTopic"
	for _, word := range []string{"欢迎", "来到", "Confluent", "Kafka", "Golang", "客户端"} {
		p.Produce(&kafka.Message{
			TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
			Value:          []byte(word),
		}, nil)
	}

	// 在关闭之前等待消息交付
	p.Flush(15 * 1000)
}
英文:

So somehow this seems to depend on cgo when building. I managed to work around this by compiling with cgo, but linking the dependencies statically.

FROM golang AS builder

WORKDIR /app
COPY . .
RUN go mod download && \
    go build -o myapp -ldflags '-linkmode external -w -extldflags "-static"'


# works also with alpine
FROM busybox 

COPY --from=builder /app/myapp myapp
CMD ["./myapp"]

I have used the code below, from their repo, to test. Which gave me initially the kind of error you have shown.

package main

import (
	"fmt"

	"github.com/confluentinc/confluent-kafka-go/kafka"
)

func main() {

	p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
	if err != nil {
		panic(err)
	}

	defer p.Close()

	// Delivery report handler for produced messages
	go func() {
		for e := range p.Events() {
			switch ev := e.(type) {
			case *kafka.Message:
				if ev.TopicPartition.Error != nil {
					fmt.Printf("Delivery failed: %v\n", ev.TopicPartition)
				} else {
					fmt.Printf("Delivered message to %v\n", ev.TopicPartition)
				}
			}
		}
	}()

	// Produce messages to topic (asynchronously)
	topic := "myTopic"
	for _, word := range []string{"Welcome", "to", "the", "Confluent", "Kafka", "Golang", "client"} {
		p.Produce(&kafka.Message{
			TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
			Value:          []byte(word),
		}, nil)
	}

	// Wait for message deliveries before shutting down
	p.Flush(15 * 1000)
}

huangapple
  • 本文由 发表于 2021年8月28日 01:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/68957644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定