4

I am building a java app runnign on alpine and installing jdk11 with alpine package manager but suddenly from last night something is wrong. I haven't changed anything in my docker file so it could be something with some packages being updated?

This is my Dockerfile

FROM amd64/alpine:3.7
RUN apk update
RUN apk --no-cache add openjdk11-jdk --repository=http://dlcdn.alpinelinux.org/alpine/edge/community
ENV JAVA_HOME=/usr/lib/jvm/default-jvm
ENV PATH=$JAVA_HOME/bin:$PATH

RUN apk --no-cache add bash
COPY ./target/myapp-.jar /
EXPOSE 80
CMD ["java", "-jar", "/myapp.jar"]

It used to build the image fine but suddenly I am seeing the following error

 ---> Running in 324f142bbf15
fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
... //some content omitted for simplicity
(25/25) Installing openjdk11-jre (11.0.9_p11-r1)
Executing busybox-1.27.2-r11.trigger
Executing java-common-0.3-r0.trigger
sort: unrecognized option: V
BusyBox v1.27.2 (2018-06-06 09:08:44 UTC) multi-call binary.

Usage: sort [-nrugMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR] [FILE]...

Sort lines of text

    -o FILE Output to FILE
    -c  Check whether input is sorted
    -b  Ignore leading blanks
    -f  Ignore case
    -i  Ignore unprintable characters
    -d  Dictionary order (blank or alphanumeric only)
    -g  General numerical sort
    -M  Sort month
    -n  Sort numbers
    -t CHAR Field separator
    -k N[,M] Sort by Nth field
    -r  Reverse sort order
    -s  Stable (don't sort ties alphabetically)
    -u  Suppress duplicate lines
    -z  Lines are terminated by NUL, not newline

I was not getting this error before Could anyone help resolving this, please? This causes java not being installed properly on alpine linux

Ciccio
  • 189
  • 10

1 Answers1

4

Essentially, your Dockerfile has two crucial issues related to this error.

The first one: you fix the specific version of Alpine base image (which is good) to 3.7, however, you force apk to use not 3.7's native repository, but the edge one, which is a development repository containing the latest software and is meant to have packages with versions progressing over the time.

This is the exact reason of the error you see, because the openjdk11-jdk package was last updated pretty recently (Nov 30) and is targeted to the latest Alpine version without caring about compatibility with older 3.7. So, you took openjdk11-jdk from the edge repository, and it worked for you for some time, but once 3.7 and the latest Alpine versions became different enough, your build became crippled. In 3.7, sort doesn't have -V option, but in later releases it does:

$ docker run -it alpine:3.7
/ # sort -V
sort: unrecognized option: V
BusyBox v1.27.2 (2018-06-06 09:08:44 UTC) multi-call binary.

Usage: sort [-nrugMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR] [FILE]...

<Long RTFM goes here>

$ docker run -it alpine:3.13
/ # sort -V

<Silently works>

The second issue is that you don't specify the exact version for the package itself, which means literally "install whatever you want". Usually this means "install the latest available version", so, again, apk successfully finds the latest version in the edge repository (as you forced it to look there), and accepts it as a valid one, which is not true: this package version is not meant to run on 3.7.

Occasionally, it seems that Alpine 3.7 has only OpenJDK 7 and 8 (this is probably why you targeted the edge repo), so the good solution here would be:

  1. Bump the base image version to the newer one (e.g. Alpine 3.13 already has openjdk11 in its repository);
  2. Don't target the edge repository, it's not meant to be used for reproducible builds;
  3. Fix the specific version of packages being installed.

The beginning of the Dockerfile would look like:

FROM amd64/alpine:3.13.0
RUN apk update
RUN apk --no-cache add openjdk11-jdk=11.0.9_p11-r1

Once these things are done, the only way your build may surprisingly crash is the occasional modification of the specific package version in the repository or its deletion (which is unlikely, and would it even happen, the error message would be very clear and comprehensible).

As the bottom line I would recommend using Hadolint to catch such lingering bugs before you hit them.

Danila Kiver
  • 3,418
  • 1
  • 21
  • 31
  • Thank you. Before your answer I had resolved this by updating to latest alpine. I found useful your help in getting the specific jdk version rather than the edge. Thanks alot! All workign fine now – Ciccio Jan 17 '21 at 12:07