Dockerfile alpine image apk cannot find Java 8
Asked Answered
J

3

7

I wanted to reduce my docker image and switched to an alpine base image instead. This lead to problems bash not finding apt. Turns out, I have to use apk instead. However, it does not seem to find the java version I need. Here is my dockerfile

#Use alpine golang
FROM golang:alpine

#Get the code from local code commit repo
WORKDIR /go/src/xxx
COPY . /go/src/xxx

#Install Java and cleanup after in the same layer
RUN apk update && apk add openjdk-8-jre-headless && rm -rf /var/lib/apt/lists/*

#Install dependencies recursively and remove the third_party directory after it has been used for compiling.
RUN go get ./... && go run setup.go && RUN rm -rf third_party
#More commands...



 ---> 32a6b6b5f196 
Step 4/8 : RUN apk update && apk add openjdk-8-jre-headless && rm -rf /var/lib/apt/lists/* 
 ---> Running in bc48930e48be 
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz 
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz 
v3.9.4-4-gd5dd5c1cd0 [http://dl-cdn.alpinelinux.org/alpine/v3.9/main] 
v3.9.4-5-gcfdf5452f1 [http://dl-cdn.alpinelinux.org/alpine/v3.9/community] 
OK: 9766 distinct packages available 
  openjdk-8-jre-headless (missing): 
·[91mERROR: unsatisfiable constraints: 
·[0m    required by: world[openjdk-8-jre-headless] 
The command '/bin/sh -c apk update && apk add openjdk-8-jre-headless && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 1

I have also tried

RUN apk update && apk add -t jessie-backports openjdk-8-jre-headless ca-certificates-java && rm -rf /var/lib/apt/lists/*

According to https://unix.stackexchange.com/questions/342403/openjdk-8-jre-headless-depends-ca-certificates-java-but-it-is-not-going-to-be but that also fails, not sure if the -t flags fault:

Step 4/7 : RUN apk update && apk add -t jessie-backports openjdk-8-jre-headless ca-certificates-java && rm -rf /var/lib/apt/lists/* 
 ---> Running in 1e2ef22e7c16 
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gz 
fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz 
v3.9.4-4-gd5dd5c1cd0 [http://dl-cdn.alpinelinux.org/alpine/v3.9/main] 
v3.9.4-5-gcfdf5452f1 [http://dl-cdn.alpinelinux.org/alpine/v3.9/community] 
OK: 9766 distinct packages available 
·[91mERROR: unsatisfiable constraints: 
·[0m  openjdk-8-jre-headless (missing): 
    required by: jessie-backports-0[openjdk-8-jre-headless] 
  ca-certificates-java (missing): 
    required by: jessie-backports-0[ca-certificates-java] 
The command '/bin/sh -c apk update && apk add -t jessie-backports openjdk-8-jre-headless ca-certificates-java && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 3 
Jokester answered 28/5, 2019 at 7:25 Comment(0)
U
8

Alpine uses entirely different package repositories than Debian or Ubuntu. The package openjdk-8-jre-headless doesn't exist there. You can check this here and find the correct package that fits your needs.

Unfold answered 28/5, 2019 at 7:50 Comment(0)
T
8

If you are using an Alpine image then use the below command to add java8

RUN apk add openjdk8-jre
Tedtedd answered 5/6, 2021 at 8:17 Comment(0)
T
3

You can check how the official alpine OpenJDK image downloads java and follow the same steps

check Dockerfile

In short, you can add the following code in your Dockerfile

# add a simple script that can auto-detect the appropriate JAVA_HOME value
# based on whether the JDK or only the JRE is installed
RUN { \
        echo '#!/bin/sh'; \
        echo 'set -e'; \
        echo; \
        echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; \
    } > /usr/local/bin/docker-java-home \
    && chmod +x /usr/local/bin/docker-java-home
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV PATH $PATH:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin

ENV JAVA_VERSION 8u212
ENV JAVA_ALPINE_VERSION 8.212.04-r0

RUN set -x \
    && apk add --no-cache \
        openjdk8="$JAVA_ALPINE_VERSION" \
    && [ "$JAVA_HOME" = "$(docker-java-home)" ]

I hope this helps

Troth answered 28/5, 2019 at 7:56 Comment(4)
That's even a bit overkill - apk add --no-cache openjdk8 is enough. 8.212.04-r0 is the current latest version.Forster
@Forster I always prefer being explicit about my dependency versions especially when I'm installing those on build time.Troth
That's good practice. However it's worth noting that old package versions are not kept on Alpine, so you can't specify anything but the latest version (unless other versions are available in different repositories, which are explicitly specified). Currently, the only openjdk8 version available in Alpine repositories 3.6, 3.7, 3.8, 3.9 and edge is 8.212.04-r0, so no other version could be specified, and once the openjdk8 package is updated, docker build will break.Forster
Oh ! I didn't know about this I guess official Dockerfile needs a pull request then :DTroth

© 2022 - 2024 — McMap. All rights reserved.