Lightweight docker images for Java apps

There’s a number of posts (e.g. here) talking about the official Java docker images being on the large side – in Docker terms, the standard images at > 600MB before you even build your image containing you app are … pretty big.

Suggestions are to use a smaller image as a starting point, like Alpine Linux. It looks like at some point the official Java images have been updates to include a number of Alpine based images too.

Pulling down an OpenJDK 8 image, java:openjdk-8-jdk and then java:openjdk-8-alpine, there’s a massive size difference between the size of the two:

If you’re planning on running something lightweight like a Spring Boot app, looks like the provided Alpine images are a good starting point.

Inline text replacement with sed

Replacing values in files is incredibly easy with sed. Here’s some examples:

 

sed 's/match/replace/g' file.txt

Find match, replace with replace, globally (all matches), in file.txt

 

sed 's/match/replace/g' file.txt > file2.txt

Same as before, but write results to new file, file2.txt

 

sed -n 's/match/replace/p' file.txt

-n suppresses output of the results, but /p prints out just the matching patterns that are replaced.

 

sed -i.old 's/match/replace/g' file.txt

Replace matches in the input file with inline replace (-i), renaming original file file.txt.old and writing inline replace results to file.txt