Docker build error

Hi,

I’m trying to build my crystal app from a Dockerfile running in a gitlab ci script. The build works fine from my mac but when running in ci it fails with this:

:
Removing intermediate container 6656df1512e2
45540200af26
Step 8/13 : RUN crystal build -Dmusl -o bin/pg_cli src/pg_cli.cr
 ---> Running in 771014db8f58
cc: error: _main.o: No such file or directory
cc: error: S-lice40U-I-nt841.o: No such file or directory
cc: error: P-ointer40U-I-nt841.o: No such file or directory
cc: error: A-rgumentE-rror.o: No such file or directory
:
cc: error: C-allS-tack5858R-epeatedF-rame.o: No such file or directory
 Error: execution of command failed with code: 1: `cc "${@}" -o '/tmp/pg_csvlog/bin/pg_cli'  -rdynamic  -lxml2 -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` -lpcre -lm /usr/lib/libgc.a -lpthread /usr/share/crystal/src/ext/libcrystal.a -levent -lrt -L/usr/bin/../lib/crystal/lib -L/usr/lib -L/usr/local/lib`
 The command '/bin/sh -c crystal build -Dmusl -o bin/pg_cli src/pg_cli.cr' returned a non-zero code: 1
 ERROR: Job failed: command terminated with exit code 1

In case it’s relevant, the Dockerfile looks like this:

FROM crystallang/crystal:latest-alpine AS builder
WORKDIR /tmp/pg_csvlog
RUN apk add --update --no-cache musl-dev libxml2-dev
COPY src/ ./src
COPY shard.yml .
RUN mkdir bin
RUN shards install
RUN crystal build -Dmusl -o bin/pg_cli src/pg_cli.cr
RUN crystal build -Dmusl -o bin/pg_cron src/pg_cron.cr

FROM alpine
RUN apk add --update libressl zlib pcre libevent libgcc libxml2 curl gzip
COPY --from=builder /tmp/pg_csvlog/bin/* /bin/

(I usually just do shards build -Dmusl but it seems to choke on building only one of the two binaries so I decided to break it up and see if it offered any further clues. It didn’t)

Any thoughts as to what might be causing this?

Thanks,

Steve

What happens if you just dont do -Dmusl and do shards build --production --static?

The error id similar to the ld glitch. Might be worth checking if using lld as a linker solves your issue.

Another possible cause is if your docker volume is full.

This failed as follows:

Step 7/11 : RUN shards build --static
 ---> Running in 5899dbf7b65e
Fetching https://github.com/will/crystal-pg.git
Fetching https://github.com/crystal-lang/crystal-db.git
Fetching https://github.com/taylorfinnell/awscr-signer.git
Fetching https://gitlab.com/steve-baldwin/slack-api.git
Fetching https://gitlab.com/steve-baldwin/slack-cli.git
Installing pg (0.20.0)
Installing db (0.8.0)
Installing awscr-signer (0.7.0)
Installing slack-api (0.2.8)
Installing slack-cli (0.1.7)
Building: pg_cron
Error target pg_cron failed to compile:
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libxml2.a(xzlib.o): in function `xz_decomp':
/home/buildozer/aports/main/libxml2/src/libxml2-2.9.10/xzlib.c:581: undefined reference to `lzma_code'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libxml2.a(xzlib.o): in function `xz_head':
/home/buildozer/aports/main/libxml2/src/libxml2-2.9.10/xzlib.c:411: undefined reference to `lzma_auto_decoder'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libxml2.a(xzlib.o): in function `is_format_lzma':
/home/buildozer/aports/main/libxml2/src/libxml2-2.9.10/xzlib.c:312: undefined reference to `lzma_properties_decode'
/usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libxml2.a(xzlib.o): in function `__libxml2_xzclose':
/home/buildozer/aports/main/libxml2/src/libxml2-2.9.10/xzlib.c:799: undefined reference to `lzma_end'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/tmp/pg_csvlog/bin/pg_cron'  -rdynamic -static  /usr/lib/libxml2.a -lz `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'` `command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'` /usr/lib/libpcre.a /usr/lib/libm.a /usr/lib/libgc.a /usr/lib/libpthread.a /usr/share/crystal/src/ext/libcrystal.a /usr/lib/libevent.a /usr/lib/librt.a -L/usr/bin/../lib/crystal/lib -L/usr/lib -L/usr/local/lib`

The command '/bin/sh -c shards build --static' returned a non-zero code: 1

I can see how to install lld but can you please tell me how to use it instead of ld from a shards build command?

Thanks.

I put in a df -h just before the crystal build and it looks like there is sufficient space:

:
Step 8/15 : RUN df -h
 ---> Running in c2d65bbc1879
Filesystem      Size  Used Available Use% Mounted on
overlay        64.0G  4.9G     59.1G   8% /
tmpfs          64.0M    0      64.0M   0% /dev
tmpfs           7.6G    0       7.6G   0% /sys/fs/cgroup
shm            64.0M    0      64.0M   0% /dev/shm
/dev/nvme0n1p1  64.0G 4.9G     59.1G   8% /etc/resolv.conf
/dev/nvme0n1p1  64.0G 4.9G     59.1G   8% /etc/hostname
/dev/nvme0n1p1  64.0G 4.9G     59.1G   8% /etc/hosts
tmpfs            7.6G   0       7.6G   0% /proc/acpi
tmpfs           64.0M   0      64.0M   0% /proc/kcore
tmpfs           64.0M   0      64.0M   0% /proc/keys
tmpfs           64.0M   0      64.0M   0% /proc/latency_stats
tmpfs           64.0M   0      64.0M   0% /proc/timer_list
tmpfs           64.0M   0      64.0M   0% /proc/sched_debug
tmpfs            7.6G   0       7.6G   0% /sys/firmware
:

After installing lld there are two options:

  1. Since lld is a drop replacement, so you can overwrite ld with lld.
  2. export CC="cc -fuse-ld=lld" and run things as usual

For checking the docker image you might need to $ docker system df because maybe the disk space reserved on the host for docker containers is full. I am not sure if df -h in the container will reveal the same information.

Thanks for your help. Unfortunately switching to lld didn’t solve the problem, so I guess it is a space issue. I’ll be following up with our devops folk.

Regards,

Steve