DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Kubernetes Installation in RedHat/CentOS
  • Buildpacks: An Open-Source Alternative to Chainguard
  • Ansible and the Pre-Container Arts
  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023

Trending

  • How Trustworthy Is Big Data?
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Creating a Docker Image with Ubuntu and Java

Creating a Docker Image with Ubuntu and Java

Learn how to create a Docker image with Ubuntu 15.04 and Java 7 or 8.

By 
Ivan K user avatar
Ivan K
·
Aug. 11, 15 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
106.4K Views

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.

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:

  •  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 RUNcommand 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:

# 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:
     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:
  • 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.

    Docker (software) Java (programming language) ubuntu Command (computing)

    Published at DZone with permission of Ivan K. See the original article here.

    Opinions expressed by DZone contributors are their own.

    Related

    • Kubernetes Installation in RedHat/CentOS
    • Buildpacks: An Open-Source Alternative to Chainguard
    • Ansible and the Pre-Container Arts
    • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023

    Partner Resources

    ×

    Comments

    The likes didn't load as expected. Please refresh the page and try again.

    ABOUT US

    • About DZone
    • Support and feedback
    • Community research
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • Become a Contributor
    • Core Program
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 3343 Perimeter Hill Drive
    • Suite 100
    • Nashville, TN 37211
    • support@dzone.com

    Let's be friends: