dockerfileshsdkman

sdkman does not install java in a dockerfile


I have this docker file:

# We are going to star from the jhipster image
FROM jhipster/jhipster

# install as root
USER root

### Setup docker cli (don't need docker daemon) ###
# Install some packages
RUN apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y

# Add Dockers official GPG key:
RUN ["/bin/bash", "-c", "set -o pipefail && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -"]

# Add a stable repository
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# Setup aws credentials as environment variables
ENV AWS_ACCESS_KEY_ID "change it!"
ENV AWS_SECRET_ACCESS_KEY "change it!"

# noninteractive install for tzdata
ARG DEBIAN_FRONTEND=noninteractive

# set timezone for tzdata
ENV TZ=America/Sao_Paulo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install the latest version of Docker Engine - Community and also aws cli
RUN apt-get update && apt-get install docker-ce docker-ce-cli containerd.io awscli -y

# change back to default user
USER jhipster

# install skd and java version 1.8
RUN curl -s "https://get.sdkman.io" | bash
RUN bash $HOME/.sdkman/bin/sdkman-init.sh
RUN bash -c "sdk install java 8.0.222.j9-adpt"

When I run a command to build an image from this dockerfile it fails on the last step with a message:

/bin/sh: 1: sdk: not found

When I install it on my local machine it runs sdkman (sdk) on bash. But on this script it calls it from sh not bash. How can I make it calls skdman (sdk) from sh? What I actually want to do is install a specific java version through sdkman (sdk). Is there another way to do it?


Solution

  • For sdk command to be available you need to run source sdkman-init.sh.

    Here is a working sample with java 11 on centos.

    FROM centos:latest
    
    ARG CANDIDATE=java
    ARG CANDIDATE_VERSION=11.0.6-open
    
    ENV SDKMAN_DIR=/root/.sdkman
    
    # update the image
    RUN yum -y upgrade
    
    # install requirements, install and configure sdkman
    # see https://sdkman.io/usage for configuration options
    RUN yum -y install curl ca-certificates zip unzip openssl which findutils && \
        update-ca-trust && \
        curl -s "https://get.sdkman.io" | bash && \
        echo "sdkman_auto_answer=true" > $SDKMAN_DIR/etc/config && \
        echo "sdkman_auto_selfupdate=false" >> $SDKMAN_DIR/etc/config
    
    # Source sdkman to make the sdk command available and install candidate
    RUN bash -c "source $SDKMAN_DIR/bin/sdkman-init.sh && sdk install $CANDIDATE $CANDIDATE_VERSION"
    
    # Add candidate path to $PATH environment variable
    ENV JAVA_HOME="$SDKMAN_DIR/candidates/java/current"
    ENV PATH="$JAVA_HOME/bin:$PATH"
    
    ENTRYPOINT ["/bin/bash", "-c", "source $SDKMAN_DIR/bin/sdkman-init.sh && \"$@\"", "-s"]
    CMD ["sdk", "help"]