Add Java to an NGINX Docker or add NGINX to a Java Docker on Alpine? Add Java to an NGINX Docker or add NGINX to a Java Docker on Alpine? nginx nginx

Add Java to an NGINX Docker or add NGINX to a Java Docker on Alpine?


For creating a combined image, you could follow either of the suggested paths:

  • Creating a merged Dockerfile with the setup steps for both images, and building your own custom image.
  • Creating a Dockerfile pulling from image 1 (the more "complex" one), and adding the commands needed for image 2.

The second approach is preferred, since you start off a known-good image, not having to start from scrach. Plus, you may need only minimal changes. For this to work, both images should share a common base image, such as alpine for example.

Examining both Nginx and Java OpenJDK Dockerfiles, you could see that Nginx Dockerfile is fairly more complex, with many prerequisite packages and setup steps, so it would make a better fit for the base image. My suggestion is, start from the Nginx base image, and add Java on top.

OpenJDK Java 7, 8 (stable) and also 9 and 10 (experimental) are already available in Alpine repositories. If there's no hard requirement for a particular Java implementation, and you're happy with say OpenJDK 8, your combined Dockerfile may be as simple as:

FROM nginx:alpineRUN apk add openjdk8

If you need a specific Java version, which isn't available in Alpine repositories, it's usually a matter of downloading the zipped Alpine Java distribution and unpacking it and setting JAVA_HOME accordingly. For example, see the OpenJDK 13 Alpine Dockerfile.