Creating a Docker Image with Ubuntu and Java
Learn how to create a Docker image with Ubuntu 15.04 and Java 7 or 8.
Join the DZone community and get the full member experience.Join For Free
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.
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.
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:
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
RUNcommand executes shell commands to install programs, configure the Docker image etc.
Details follow below on the purpose of each shell command.
Updates the list of available packages and their versions.
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.
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.”
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:
# 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:
- 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:
... 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
- 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:
In my case I see that Java version 1.8.0_51 is installed.
- Exit the Docker container:
Finally, while still being let’s examine the size of the image that was created:
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.
Published at DZone with permission of Ivan K. See the original article here.
Opinions expressed by DZone contributors are their own.