pebbe / zmq4

A Go interface to ZeroMQ version 4
BSD 2-Clause "Simplified" License
1.17k stars 163 forks source link

Can't compile on Alpine Linux #83

Closed activeshadow closed 8 years ago

activeshadow commented 8 years ago

I have a Golang application that utilizes pebbe/zmq4 and I'd love to be able to run it in Alpine Linux due to the much smaller container size. However, I get the following error:

apk update
apk install gcc pkgconfig zeromq-dev
go run main.go
# github.com/pebbe/zmq4
In file included from /go/src/github.com/pebbe/zmq4/ctxoptions_unix.go:6:0:
/usr/include/zmq.h:46:19: fatal error: errno.h: No such file or directory
compilation terminated.

I'm using the golang:1.5-alpine container available on Docker Hub.

pebbe commented 8 years ago

I can't reproduce this. apk install doesn't work in golang:1.5-alpine

What is go run main.go?

Please provide a full log of what you are doing.

(peter) ~ docker run -i -t golang:1.5-alpine sh
/go # apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/community/x86_64/APKINDEX.tar.gz
v3.3.3-11-g4eba3be [http://dl-cdn.alpinelinux.org/alpine/v3.3/main]
v3.3.3-9-gfc38db2 [http://dl-cdn.alpinelinux.org/alpine/v3.3/community]
OK: 5858 distinct packages available
/go # apk install gcc pkgconfig zeromq-dev
apk-tools 2.6.5, compiled for x86_64.

usage: apk COMMAND [-h|--help] [-p|--root DIR] [-X|--repository REPO] [-q|--quiet] [-v|--verbose]
           [-i|--interactive] [-V|--version] [-f|--force] [-U|--update-cache] [--progress] [--progress-fd FD]
           [--no-progress] [--purge] [--allow-untrusted] [--wait TIME] [--keys-dir KEYSDIR]
           [--repositories-file REPOFILE] [--no-network] [--no-cache] [--arch ARCH] [--print-arch] [ARGS]...

The following commands are available:
  add       Add PACKAGEs to 'world' and install (or upgrade) them, while ensuring that all dependencies are met
  del       Remove PACKAGEs from 'world' and uninstall them
  fix       Repair package or upgrade it without modifying main dependencies
  update    Update repository indexes from all remote repositories
  info      Give detailed information about PACKAGEs or repositores
  search    Search package by PATTERNs or by indexed dependencies
  upgrade   Upgrade currently installed packages to match repositories
  cache     Download missing PACKAGEs to cache and/or delete unneeded files from cache
  version   Compare package versions (in installed database vs. available) or do tests on literal version
            strings
  index     Create repository index file from FILEs
  fetch     Download PACKAGEs from global repositories to a local directory
  audit     Audit the directories for changes
  verify    Verify package integrity and signature
  dot       Generate graphviz graphs
  policy    Show repository policy for packages
  stats     Show statistics about repositories and installations
ghost commented 8 years ago

Oops... sorry! It's apk add, not apk install.

main.go is the application I'm testing on Alpine Linux that uses your zmq4 library.

docker run -it -v `pwd`:/go/src golang:1.5-alpine /bin/sh
/go # apk update
/go # apk add gcc pkgconfig zeromq-dev
/go # cd /go/src
/go/src # go run main.go
# github.com/pebbe/zmq4
In file included from /go/src/github.com/pebbe/zmq4/ctxoptions_unix.go:6:0:
/usr/include/zmq.h:46:19: fatal error: errno.h: No such file or directory
compilation terminated.
pebbe commented 8 years ago

It looks like the package zeromq-dev is broken. It is missing the necessary standard header files.

Or Alpine Linux is broken. There is no package that contains the standard header files.

ghost commented 8 years ago

Yeah, Alpine Linux uses musl instead of glibc, so I figure that has something to do with it.

pebbe commented 8 years ago

I tried this:

~ docker run -i -t golang:1.5-alpine sh
/go # apk update
/go # apk add gcc pkgconfig zeromq-dev
/go # apk add git musl-dev
/go # go get github.com/pebbe/zmq4
/go # go test -v github.com/pebbe/zmq4

I got:

signal: segmentation fault (core dumped)
FAIL    github.com/pebbe/zmq4   0.003s

Is there another C compiler on Alpine Linux?

pebbe commented 8 years ago

cgo only works with gcc, so another c compiler is no option

ghost commented 8 years ago

Well damn... :(

ghost commented 8 years ago

I wonder if this might be a solution. I'll test it out when I can.

pebbe commented 8 years ago

This works:

~ docker run -i -t blang/golang-alpine sh
/go # apk update
/go # apk add gcc pkgconfig zeromq-dev
/go # apk add git musl-dev
/go # go get github.com/pebbe/zmq4
/go # go test -v github.com/pebbe/zmq4

Result:

PASS
ok      github.com/pebbe/zmq4   11.619s
ghost commented 8 years ago

Sweet! OK, so I'll test building the app in blang/golang-alpine, then copying it out and running it in a base Alpine Linux container.

pebbe commented 8 years ago

Let me know if that works.

gogochan commented 8 years ago

Thanks @pebbe . Was able to compile both static and shared on Alpine. For static compile, I had to install alpine-sdk.

manveru commented 6 years ago

In case someone has this issue in the future, here's the needed commands:

RUN apk add --no-cache zeromq-dev musl-dev pkgconfig alpine-sdk libsodium-dev
RUN CGO_LDFLAGS="$CGO_LDFLAGS -lstdc++ -lm -lsodium" \
  CGO_ENABLED=1 \
  GOOS=linux \
  go build -v -a --ldflags '-extldflags "-static" -v'
omani commented 6 years ago

this helped me so much. thank you very much @manveru

RoxanaTapia commented 6 years ago

Thank you @manveru I had the error: ImportError: Error loading shared library libzmq.so.5: No such file or directory (needed by /usr/local/lib/python3.6/site-packages/zmq/backend/cython/error.cpython-36m-x86_64-linux-gnu.so) while trying to install zeromq-dev (and also libzmq) with the RUN command in a Dockerfile with alpine3.6. Adding only the first RUN command that you suggested already fixed the problem. Thanks!

ns-kliu commented 2 years ago

@manveru Thank you very much For static link, I also need to add libzmq-static libsodium-static