1

I am just a beginner to Docker and was exploring the various features. I have successfully installed java inside docker

**OS version**:Windows Server 2016

PS C:\testing> docker version
Client:
 Version:      17.03.1-ee-3
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   3fcee33
 Built:        Thu Mar 30 19:31:22 2017
 OS/Arch:      windows/amd64
Server:
   Version:      17.03.1-ee-3

 API version:  1.27 (minimum version 1.24)
 Go version:   go1.7.5
 Git commit:   3fcee33
 Built:        Thu Mar 30 19:31:22 2017
 OS/Arch:      windows/amd64
 Experimental: false
PS C:\testing>

Created a Dockerfile in the path C:\java-windows-docker\Dockerfile, and put the following lines inside:

 FROM windowsservercore  
  RUN powershell (new-object     
System.Net.WebClient).Downloadfile('http://javadl.oracle.com/webapps/download/AutoDL?
BundleId=210185', 'C:\jre-8u91-windows-x64.exe')
RUN powershell start-process -filepath C:\jre-8u91-windows-x64.exe -passthru -wait -argumentlist"/s,INSTALLDIR=c:\Java\jre1.8.0_91,/L,install64.log"
RUN del C:\jre-8u91-windows-x64.exe
CMD [ "c:\\Java\\jre1.8.0_91\\bin\\java.exe", "-version"]
It downloads the Java 8 Update 91 Windows installer and silently installs it to c:\Java\jre1.8.0_91.

docker build -t java-windows-docker C:\java-windows-docker And if we run it, after start, the container launches Java and prints out its version.

PS C:\Windows\system32> docker run java-windows-docker
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)
We have Java running. inside a Windows docker container which is hosted on a Windows server 2016

I want to run a simple java program. The code within java files are as below:

    class Hello{  
public static void main(String[] args){  
System.out.println("This is java app \n by using Docker");  
} 
}

Then as the next step I built the image for the above program. Dockerfile for building java program

FROM testing
WORKDIR C:\\testing
RUN javac Hello.java
CMD ["java","Hello"]

When I build the image, I get shown the below error:

PS C:\testing> docker build -t testing C:\testing Sending build context to Docker daemon 11.78 kB Step 1/5 : FROM java-windows-docker ---> 534f46817a18 Step 2/5 : WORKDIR C:\testing ---> fc954983c8b0 Removing intermediate container 5e30071d9159 Step 3/5 : RUN javac Hello.java ---> Running in c91c62ba707c javac' is not recognized as an internal or external command, operable program or batch file. The command 'cmd /S /C javac Hello.java' returned a non-zero code: 1

Any advise on this would be helpful. If possible Kindly help me with the sample Dockerfile to run this java program. I have been working on this for the past two days i can't get this to work. I'm wondering how to set the path inside the docker image and how this actually works.i have also tried to set the path

ENV JAVA_HOME C:\Java\jre1.8.0_91\bin;
RUN setx PATH %PATH%;%JAVA_HOME%\bin
RUN setx CLASSPATH %CLASSPATH%;C:\Java\jre1.8.0_91;C:\testing;.;

But it doesn't work. I dont know when to set this path while building the java image or running the java program. Kindly advise.Thanks in advance

veben
  • 19,637
  • 14
  • 60
  • 80
syndy1989
  • 403
  • 10
  • 25
  • Docker images are like VM frozen in the time. Let's review: - windowsservercore >> java-windows-docker - testing (from which image? itself?) I think that you should have create testing image from java-windows-docker. And when you are creating the java-windows-docker you have to create the `JAVA_HOME`, and add your java installation to `PATH` environment variable. – Anderson Marques Jun 09 '17 at 13:07

2 Answers2

0

I thing u have not set environment Variable in system. Set the java path as:

  1. MY Computer -> System Properties ->Advance System Settings ->Click on Environment Variables.

  2. Click On New: Enter Variable name as : JAVA_HOME and Variable Value as :C:\Program Files\Java\jdk1.8.0_121\bin //Your java bin path.

Vikas Suryawanshi
  • 522
  • 1
  • 5
  • 12
0

Images have layers. Each layer is composed of: an image base + commands.

Let's review, you create java-windows-docker:

 FROM windowsservercore  
 RUN powershell...
 RUN powershell...
 RUN del C:\jre-8u91-windows-x64.exe
 CMD [ "c:\\Java\\jre1.8.0_91\\bin\\java.exe", "-version"]

You have to add this directory to PATH and define your JAVA_HOME.

 FROM windowsservercore  
 RUN powershell...
 RUN powershell...
 RUN del C:\jre-8u91-windows-x64.exe
 RUN set JAVA_HOME=""c:\\Java\\jre1.8.0_91\\"
 RUN set PATH=%PATH%,%JAVA_HOME%\bin
 CMD [ "c:\\Java\\jre1.8.0_91\\bin\\java.exe", "-version"]

So, every application that runs on this container (java-windows-docker) will have access to Java independently of the directory.

Then your testing image will use as image base the java-windows-docker image:

FROM java-windows-docker
WORKDIR C:\\testing
RUN javac Hello.java
CMD ["java","Hello"]

PS: I wasn't focused on syntax. Only on the concept. I'm not in front of my dev machine right now. ;)

Anderson Marques
  • 808
  • 8
  • 13
  • Hi Anderson Still it gives me the same error PS C:\Users\Administrator> docker build -t testing C:\testing Sending build context to Docker daemon 3.072 kB Step 1/4 : FROM final ---> 6dbb140824c1 Step 2/4 : WORKDIR C:\\testing ---> Using cache ---> a6b874bdd543 Step 3/4 : RUN javac Hello.java ---> Running in a28e929b64f1 'javac' is not recognized as an internal or external command, operable program or batch file. The command 'cmd /S /C javac Hello.java' returned a non-zero code: 1 Am i missing out something – syndy1989 Jun 12 '17 at 04:23
  • Hi, After the creation of each container put it on execution and enter on it. (docker exec -it container_name cmd/bash). Then inspect if the folders and if the expected commands run properly. If the container is not as you wish, your can run the correct commands inside the container to correct it. Then detach with cmd+q+cmd+p and commit the changes with docker commit creating a new image. You will be able to see the commands history with docker history image_name and compare it with your Dockerfile. – Anderson Marques Jun 12 '17 at 12:42
  • It is like a reverse engineering. Sometimes, I observe the docker history of some containers built by big players and I take it as guidelines to build my own Dockerfiles. This helps a lot when you are not so familiarized with all commands or scripts. This process could help you to achieve your goal. – Anderson Marques Jun 12 '17 at 12:46