Creating a Docker Image with Ubuntu and Java

By | August 8, 2015

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:

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:

  • FROM ubuntu:15.04
    States which Docker image this Docker image is to be based upon. Compare to parent class in object-oriented programming.
  • MAINTAINER Ivan Krizsan, https://github.com/krizsan
    Tells us who is maintaining this Docker image.
  • The Docker RUN command executes shell commands to install programs, configure the Docker image etc.
    Details follow below on the purpose of each shell command.

  • apt-get update
    Updates the list of available packages and their versions.
  • apt-get upgrade
    Installs any newer versions of currently installed packages.
  • apt-get install -y software-properties-common
    Installs software in order to allow for independent vendor software sources, in particular the “add-apt-repository” command that is used in the next line.
  • add-apt-repository ppa:webupd8team/java -y
    Adds the PPA repository
    that contains Oracle Java 8, assuming yes to all queries.

  • apt-get update
    Again, updates the list of available packages and their versions. The reason for running an update again is that a new repository was just added and we want to refresh the list of available packages and their versions, including those in the new repository.
  • echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
    The Java installer require you to accept a license before the installation begins. This line automates accepting the license.
  • apt-get install -y oracle-java8-installer
    Finally Java 8 can be installed! The -y flag runs the installation in a non-interactive mode, assuming that all questions are to be answered “yes”.
  • apt-get clean
    Cleans the local repository from retrieved package files.

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:

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:

  • Open a terminal window.
  • If you are not using a Linux operating system, create a Docker-Machine (if you don’t already have one):
    docker-machine create dev
  • If you are not using a Linux operating system, start the Docker-Machine:
    docker-machine start dev
  • If you are not using a Linux operating system, ssh into the Docker-Machine:
    docker-machine ssh dev
  • Go to the directory containing the Docker-file which describes the image you want to build.
    In this example, I will build the Java 8 image.
  • Build the Docker image:
    docker build -t krizsan/ubuntu1504java8:v1 .
    Note the period at the end of the line!
    Linux users will need to add the sudo command before the docker command:
    sudo docker build -t krizsan/ubuntu1504java8:v1 .
    You will see quite some console output that should end with something like this:

  • Start a Docker container using the newly created Docker image:
    docker run -it krizsan/ubuntu1504java8:v1 bash
    Again, Linux users need to insert the sudo command before the docker command.
  • Verify the Java version installed:
    java -version
    In my case I see that Java version 1.8.0_51 is installed.
  • Exit the Docker container:
    exit

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:

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.

 

 

 

 

2 thoughts on “Creating a Docker Image with Ubuntu and Java

    1. Ivan Krizsan Post author

      Hi and thanks!
      An experience I have made since writing this article is that it may be a good idea to download an archive containing a specific version of the Java runtime you want to install in the Docker image, for instance 1.7.0_72. This archive is then used every time you build the Docker image and shall be checked into your source code repository.
      If you are installing the Java runtime from some repository, you do not know exactly which version you get. One day it may for instance be 1.7.0_72 and the next 1.7.0_94.
      In addition, you may want to install the server JRE, which is a separate distribution.
      This is of course only if you have very stringent requirements on your Docker images.
      Happy coding!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *