Creating a Docker Image with Ubuntu and Java

This short article shows how to create a Docker image with Ubuntu 15.04 and that has Java 7 or 8 installed. It will act as a base for some future articles that I have planned to write.

Yes, I am aware that there are several Docker images with Java 8 on Docker Hub.

Prerequisites

When writing this article, I have used Docker 1.7.1 and Docker-Machine 0.3.0. Instructions on how to install these can be found here.

Docker File

The Docker documentation recommends that Docker files are to be located in a dedicated directory which only contains the files necessary for the creation of the Docker image.

I have thus created a directory named “krizsan-ubuntu1504java8” for the Docker image with Java 8 and another directory named “krizsan-ubuntu1504java7” for the image with Java 7.
Each of these directories are to contain a file named “Dockerfile” (note: no file extension!).

The Docker file for Java 8 on Ubuntu 15.04 has the following contents:

# Ubuntu 15.04 with Java 8 installed.
# Build image with:  docker build -t krizsan/ubuntu1504java8:v1 .

FROM ubuntu:15.04
MAINTAINER Ivan Krizsan, https://github.com/krizsan
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y  software-properties-common && \
    add-apt-repository ppa:webupd8team/java -y && \
    apt-get update && \
    echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections && \
    apt-get install -y oracle-java8-installer && \
    apt-get clean

The short version is that this Docker file will create a Docker image based on the ubuntu:15.04 Docker image in which Oracle’s Java 8 is installed.

Long version, row-by-row:

The reason for having one single Docker RUN  command and chaining the shell commands using &&   is that Docker will create one additional layer on the image for each RUN   command. The fewer RUN   commands that a Docker file contains, the smaller will the resulting image be.

The Docker file that will create an image with Java 7 on Ubuntu 15.04 looks like this:

# Ubuntu 15.04 with Java 7 installed.
# Build image with:  docker build -t krizsan/ubuntu1504java7:v1 .

FROM ubuntu:15.04
MAINTAINER Ivan Krizsan, https://github.com/krizsan
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y  software-properties-common && \
    add-apt-repository ppa:webupd8team/java -y && \
    apt-get update && \
    echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections && \
    apt-get install -y oracle-java7-installer && \
    apt-get clean

The only difference compared to the Java 8 Docker file, apart from the comments, is the line second-to-last in which the Java 7 installer is used.

Build the Java 8 Docker image using the following steps:

...
update-alternatives: using /usr/lib/jvm/java-8-oracle/bin/xjc to provide /usr/bin/xjc (xjc) in auto mode
Oracle JDK 8 installed
...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up xfonts-encodings (1:1.0.4-2) ...
Setting up xfonts-utils (1:7.7+2) ...
Setting up gsfonts-x11 (0.22) ...
Processing triggers for libc-bin (2.21-0ubuntu4) ...
Processing triggers for systemd (219-7ubuntu6) ...
 ---> d3c43c2de29e
Removing intermediate container f447de6f01c6
Successfully built d3c43c2de29e

Finally, while still being let’s examine the size of the image that was created:

  • List the locally available Docker images:
    docker images
    I see the following information about the Docker image we just created listed:
  • REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    krizsan/ubuntu1504java8   v1                  3f1e02768a70        16 seconds ago      802.6 MB

    The size of the image is on the right, in the  VIRTUAL SIZE column, and, as you can see is around 803MB.

    This concludes this article and I am now ready to create further Docker images containing software that runs on Java.

     

     

     

     

    Top