Skip to content

Instantly share code, notes, and snippets.

@gesellix
Forked from andystanton/JVM DNS TTL Policy.md
Created November 28, 2017 22:24
Show Gist options
  • Save gesellix/ccd47ec0a6929d12bf2ce0d1adc00d50 to your computer and use it in GitHub Desktop.
Save gesellix/ccd47ec0a6929d12bf2ce0d1adc00d50 to your computer and use it in GitHub Desktop.
A script that inspects the DNS TTL for a JVM in a supplied Docker image.

A script that inspects the DNS TTL for a JVM in a supplied Docker image. The image must also contain a JDK.

It does this by generating a Docker image containing a Java program that outputs the JVM's DNS TTL and executing this, then cleaning up the container, image and temporary files.

Usage

$ ./jvm-dns-ttl-policy.sh openjdk:8

This will download the image if necessary and then output the image JVM's DNS TTL:

Implementation DNS TTL for JVM in Docker image openjdk:8 is 30 seconds
#!/bin/sh
if [ -z "${1}" ]; then
echo "Usage: ${0} <image>"
exit 1
fi
target_image="${1}"
dockerfile="
FROM ${target_image}
WORKDIR /var/tmp
RUN printf ' \\
public class DNSTTLPolicy { \\
public static void main(String args[]) { \\
System.out.printf(\"Implementation DNS TTL for JVM in Docker image '${target_image}' is %%d seconds\\\\n\", sun.net.InetAddressCachePolicy.get()); \\
} \\
}' >DNSTTLPolicy.java
RUN javac DNSTTLPolicy.java -XDignore.symbol.file
CMD java DNSTTLPolicy
ENTRYPOINT java DNSTTLPolicy
"
tag_name="jvm-dns-ttl-policy"
output_file="$(mktemp)"
function cleanup() {
rm "${output_file}"
docker rmi "${tag_name}" >/dev/null
}
trap "cleanup; exit" SIGHUP SIGINT SIGTERM
docker build -t "${tag_name}" - <<<"${dockerfile}" -q &>"${output_file}"
if [ "$?" -ne 0 ]; then
>&2 echo "Error building test image:"
cat "${output_file}"
cleanup
exit 1
fi
docker run --rm "${tag_name}" &>"${output_file}"
if [ "$?" -ne 0 ]; then
>&2 echo "Error running test image:"
cat "${output_file}"
cleanup
exit 1
fi
cat "${output_file}"
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment