Skip to content
Advertisement

How to add many SSL certificates for Java application inside docker?

I’m new in docker. I want to add several certificates for Java application inside Docker. I’m using this code in Dockerfile:

RUN keytool -importcert -noprompt -trustcacerts -alias artifactory -file /files/cert.crt -keystore local -storepass changeit

and it works fine, but only for one certificate. How I can add all certificates from /files folder with one line command or in some cycle or maybe with bash file?

UPDATED: I used next bash to add certificates:

for cert in ${tempdir}/*.crt; do
cert2=$(basename $cert)
echo "# ${cert2}" >> ${destdir}/${cert2}
${openssl} x509 -inform der -in ${cert} -outform pem -out ${destdir}/${cert2}
keytool -importcert -noprompt -trustcacerts -alias artifactory -file /${destdir}/${cert2} -keystore local -storepass changeit
done

but got next error: “keytool: command not found”. The command keytool works fine when I run it in the docker container.

Advertisement

Answer

In dockerfile call bash file:

RUN apk update && apk add bash openssl wget && rm -rf /var/cache/apk/*
COPY getcerts.sh getcerts.sh
RUN chmod +x getcerts.sh && ./getcerts.sh

Bash script:

for cert in ${tempdir}/*.crt; do
keytool -importcert -noprompt -trustcacerts -alias artifactory-${cert2} -file /${destdir}/${cert2} -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit
done
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement