How to run a Java program in a Docker container

作者:Rui 发布时间:September 9, 2015 分类:JAVA,Docker,CLOUD 浏览:676

In this post I’m going to show how to create a new Docker image from scratch, how to add the JDK, and how we can use this image to compile and run a Java program.

Creating the Image with the JDK

The first step is to create a new Docker image from the base Docker image ubuntu:14.04 and to install a JDK on top of that. We use a Dockerfile to provide the instructions to build such an image.
There is a link explaining how to Install Oracle Java 8 In Ubuntu Via PPA Repository [JDK8]. Basically we want to execute these commands in a Dockerfile.
The Dockerfile is simply as follows:

RUN apt-get update 
RUN apt-get install software-properties-common -y 
RUN add-apt-repository ppa:webupd8team/java -y 
RUN apt-get update 
RUN echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections 
RUN apt-get install oracle-java8-installer -y 
RUN apt-get install oracle-java8-set-default 

It’s worth noting that we want the JDK installation to be automated (auto accept license) because the image is built in a not-interactive way and it wouldn’t be possible for us to manually accept the license during the JDK installation. This is done by the debconf-set-selections command. Now we just need to copy the Dockerfile above into a directory (in this article I’m using ~/dockertest) and run the docker build command:

~/dockertest$ docker build -t massimo/javatest:v1 .

This command will take some time to pull the base ubuntu image and then run the commands in the Dockerfile to download and install the Oracle JDK8, but at the end of the process the image is built and ready to be used:

~/dockertest docker images 
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 
massimo/javatest v1 6b817a6aebb1 5 minutes ago 786.6 MB 
ubuntu latest 2d24f826cb16 5 days ago 188.3 MB 
ubuntu trusty 2d24f826cb16 5 days ago 188.3 MB 
ubuntu trusty-20150218.1 2d24f826cb16 5 days ago 188.3 MB 
ubuntu 14.04 2d24f826cb16 5 days ago 188.3 MB
ubuntu 14.04.2 2d24f826cb16 5 days ago 188.3 MB 

We can run the newly built image and prove the Java is indeed installed:

~/dockertest$ docker run -i -t 6b817a6aebb1 /bin/bash 
root@37c786f440ff:/# java -version java version "1.8.0_31" 
Java(TM) SE Runtime Environment (build 1.8.0_31-b13) 
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode) 

Compiling and running a Java program in the Container

So far, so good. With the Dockerfile provided above and with a single docker buildcommand we have created an image with JDK8 installed. Now we want to compile and run a simple Java program which, alas, is yet another Hello World program, but the point of the exercise is to work with the JDK on the Docker image so we don’t really care about what the Java class does.
An interesting point here is actually how to make the Java source on the host available to the Docker container. Since I’m working on a Mac OS X, I’m using Boot2Docker and there are some quirks with shared directories on Mac OS X. I have seen many questions related to volumes mounting on Mac OS X, but essentially the following command, provided that we’re sharing directories under /Users, will work as expected and we can see that the dockertest directory is available in the container.

~/dockertest docker run -it -v ~/dockertest:/dockertest massimo/javatest:v1 
root@5ff4f27692dd:/# ls
bin boot dev dockertest etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var 

So, we now create the HelloWorld.java class in the dockertest folder:

public class HelloWorld { 
    public static void main(String[] args) { 
        System.out.println("Hello World!"); 
    } 
} 

Finally, we run again the container, compile and run the Java class:

 ~/dockertest$ docker run -it -v ~/dockertest:/dockertest massimo/javatest:v1 
root@001e586b218d:/# cd dockertest/ 
root@001e586b218d:/dockertest# javac HelloWorld.java 
root@001e586b218d:/dockertest# java -cp . HelloWorld Hello World!   

Another way to achieve the same result, without opening an interactive shell in the container, is to run this command from the host:

~/dockertest$ docker run -v ~/dockertest:/dockertest massimo/javatest:v1 sh -c 'cd dockertest;java -cp . HelloWorld' Hello World!

That’s it. The whole process, starting from scratch with only Docker (and Boot2Docker) installed, will take only a few minutes, mostly because it has to download all the required components.

标签: none

添加新评论 »