# Golang app not compiling in docker

**URL:** <https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501>\
**Category:** Non-Java Clients\
**Created:** [15 April 2021 16:14 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501 "2021-04-15T16:14:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgelza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/georgelza/32/3026_2.png) [@georgelza](https://forum.confluent.io/u/georgelza)\
**Post date:** [15 April 2021 16:14 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/1 "2021-04-15T16:14:27Z")

</div>

Hi all

Please see attached. if I compile my code directly on system, it works, when I compile/build into a docker images I get errors as per the attached.

Anyone seen this, I’m also attaching my docker file.

G

**Dockerfile**

```auto
FROM golang:alpine as build-env

RUN apk update && apk add bash ca-certificates git gcc g++ libc-dev

WORKDIR /scrubber

COPY . .

RUN go build -o scrubber .

CMD ./scrubber

```

**Err.log → Output from above build.**

```auto
Sending build context to Docker daemon 41.54MB

Step 1/6 : FROM golang:alpine as build-env

---> 14ee78639386

Step 2/6 : RUN apk update && apk add bash ca-certificates git gcc g++ libc-dev

---> Using cache

---> 159487736117

Step 3/6 : WORKDIR /scrubber

---> Using cache

---> 04f9d5590771

Step 4/6 : COPY . .

---> Using cache

---> 313c9ab381f8

Step 5/6 : RUN go build -o scrubber .

---> Running in 19c14cb0cbd2

e[91m# github.com/confluentinc/confluent-kafka-go/kafka

/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_glibc_linux.a(rdkafka_txnmgr.o): in function `rd_kafka_txn_set_fatal_error':

(.text+0x149): undefined reference to `__strdup'

/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_glibc_linux.a(rdkafka_txnmgr.o): in function `rd_kafka_txn_set_abortable_error':

(.text+0x683): undefined reference to `__strdup'

/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_glibc_linux.a(rdkafka_admin.o): in function `rd_kafka_NewTopic_new':

```

and this above message is repeated probably 50+ times

---

<div class="post-metadata">

**Author:** ![georgelza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/georgelza/32/3026_2.png) [@georgelza](https://forum.confluent.io/u/georgelza)\
**Post date:** [15 April 2021 18:21 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/2 "2021-04-15T18:21:47Z")

</div>

update,

I’ve added “librdkafka-dev pkgconf” to the docker file as modules to pull in, still getting same errors.

G

---

<div class="post-metadata">

**Author:** ![rick](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rick/32/49_2.png) [@rick](https://forum.confluent.io/u/rick)\
**Post date:** [16 April 2021 11:58 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/3 "2021-04-16T11:58:00Z")

</div>

@georgelza maybe this GH issue has some clues to your compilation issue?

> <https://github.com/confluentinc/confluent-kafka-go/issues/454>
>
> Description
> ===========
> 
> When using the 1.4.0 release, the bundled \_librdkafk…a\_ fails to work as intended with the latest alpine.
> 
> How to reproduce
> ================
> 
> Build an application which uses \_github.com/confluentinc/confluent-kafka-go\_, in a container, using the alpine:3.11 image (which, at the time of writing, is actually alpine:3.11.5) as a base. Errors such as the following will be displayed:
> 
> \`\`\`
> go: downloading github.com/confluentinc/confluent-kafka-go v1.4.0
> # github.com/confluentinc/confluent-kafka-go/kafka
> /usr/lib/gcc/x86\_64-alpine-linux-musl/9.2.0/../../../../x86\_64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.4.0/kafka/librdkafka/librdkafka\_glibc\_linux.a(rdkafka\_txnmgr.o): in function \`rd\_kafka\_txn\_set\_fatal\_error':
> (.text+0x141): undefined reference to \`\_\_strdup'
> /usr/lib/gcc/x86\_64-alpine-linux-musl/9.2.0/../../../../x86\_64-alpine-linux-musl/bin/ld: /go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.4.0/kafka/librdkafka/librdkafka\_glibc\_linux.a(rdkafka\_txnmgr.o): in function \`rd\_kafka\_txn\_set\_abortable\_error':
> (.text+0x64f): undefined reference to \`\_\_strdup'
> ...
> \`\`\`
> 
> Installing a fresh build of \_librdkafka\_ 1.4.0 into the container and building the application with \`-tags dynamic\` resolves the issue.
> 
> Could possibly be because the bundled \_librdkafka\_ was built from a :edge release of alpine, instead of :latest?
> 
> Checklist
> =========
> Please provide the following information:
> 
> - \[x\] confluent-kafka-go and librdkafka version (\`LibraryVersion()\`): 1.4.0, bundled.
> - \[\] Apache Kafka broker version: N/A
> - \[\] Client configuration: N/A
> - \[x\] Operating system: Alpine Linux
> - \[\] Provide client logs (with \`"debug": ".."\` as necessary): N/A
> - \[\] Provide broker log excerpts: N/A
> - \[\] Critical issue: No

---

<div class="post-metadata">

**Author:** ![georgelza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/georgelza/32/3026_2.png) [@georgelza](https://forum.confluent.io/u/georgelza)\
**Post date:** [16 April 2021 14:21 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/4 "2021-04-16T14:21:02Z")

</div>

so being the noob at this. where do I add the “-tags dynamic” tags.

looking at some dogs it implies with the go build command, but with us doing a docker build, using a docker file, i never issue a go build command.

G

---

<div class="post-metadata">

**Author:** ![rick](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rick/32/49_2.png) [@rick](https://forum.confluent.io/u/rick)\
**Post date:** [16 April 2021 15:14 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/5 "2021-04-16T15:14:30Z")

</div>

I think next I might try the golang docs, in particular the Docker information. Maybe start here?

[https://hub.docker.com/\_/golang](https://hub.docker.com/_/golang)

---

<div class="post-metadata">

**Author:** ![georgelza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/georgelza/32/3026_2.png) [@georgelza](https://forum.confluent.io/u/georgelza)\
**Post date:** [16 April 2021 19:03 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/6 "2021-04-16T19:03:56Z")

</div>

don’t think this is golang related, this is more docker and have feeling the combination of docker/golang and kafka.

surely someone else have deployed a go app accessing CP Kafka in K8s. be keen to see their docker file.

G

---

<div class="post-metadata">

**Author:** ![georgelza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/georgelza/32/3026_2.png) [@georgelza](https://forum.confluent.io/u/georgelza)\
**Post date:** [16 April 2021 19:08 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/7 "2021-04-16T19:08:42Z")

</div>

… let me add, my other client → receiving app is also golang and inside docker.  
App 1, read text file, do some logic, drop onto Kafka topic.  
App 2, consume topic (This app that I have problem with) more logic, and then send via gRPC to App 3, some more logic and then into a unstructured DB ( This app in docker works)  
App 2, if just direct compiled/executed, it works, when attempting to build into docker image then have these problems.

G

---

<div class="post-metadata">

**Author:** ![georgelza](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/georgelza/32/3026_2.png) [@georgelza](https://forum.confluent.io/u/georgelza)\
**Post date:** [16 April 2021 19:57 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/8 "2021-04-16T19:57:12Z")

</div>

checking, but looks like i got it build.

**changed**  
RUN apk update && apk add bash ca-certificates git gcc g++ libc-dev  
**to**  
RUN apk update && apk add bash ca-certificates git gcc g++ libc-dev librdkafka-dev pkgconf

**change**  
RUN go build -o scrubber .  
**to**  
RUN go build -tags musl -o scrubber .

G

---

<div class="post-metadata">

**Author:** ![SuJingyu111](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/sujingyu111/32/2505_2.png) [@SuJingyu111](https://forum.confluent.io/u/SuJingyu111)\
**Post date:** [31 October 2022 00:20 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/9 "2022-10-31T00:20:49Z")

</div>

God you saved my life georgelza. Thank you so much!

---

<div class="post-metadata">

**Author:** ![farkhad](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/farkhad/32/2621_2.png) [@farkhad](https://forum.confluent.io/u/farkhad)\
**Post date:** [16 December 2022 18:58 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/10 "2022-12-16T18:58:43Z")

</div>

I took this a bit further, since Go requires disk space, and created `multistage.Dockerfile`

```auto
# syntax=docker/dockerfile:1

## Build
FROM golang:1.19.3-alpine AS build

RUN apk update && apk add gcc g++ libc-dev librdkafka-dev pkgconf

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY *.go ./

RUN go build -tags musl -o myapp .

## Deploy
FROM alpine:latest

RUN apk update && apk add gcc g++ libc-dev librdkafka-dev pkgconf

COPY --from=build /app/myapp /

ENTRYPOINT ["/myapp"]

```

Image size ~250Mb + “myapp” size.

Open question: is it possible to avoid loading gcc, g++, etc in Deploy section of multistage Dockerfile?

---

<div class="post-metadata">

**Author:** ![mwtech](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@mwtech](https://forum.confluent.io/u/mwtech)\
**Post date:** [22 June 2023 09:51 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/11 "2023-06-22T09:51:09Z")

</div>

Thank you all, you saved me a lot of time)

---

<div class="post-metadata">

**Author:** ![OneCricketeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/onecricketeer/32/1551_2.png) [@OneCricketeer](https://forum.confluent.io/u/OneCricketeer)\
**Post date:** [23 June 2023 03:21 UTC](https://forum.confluent.io/t/golang-app-not-compiling-in-docker/1501/12 "2023-06-23T03:21:24Z")

</div>

Also note there are other go native libraries like franz and (now deprecated) sarama.
