I have a very basic question about FROM line in dockerfile for a java application, like a spring boot app.
So we write it like:
FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
In line 3 we are basically telling docker to use the application jar inside target folder for running the app inside container. Now when a jar is build for a java application, build process takes into account all the needed dependencies like those from jdk or those defined in POM etc.
So then why do we need FROM line? We already have all in our jar, isn't?
The FROM argument describes a base image to use. https://docs.docker.com/engine/reference/builder/#from
An analogy in a non containerized world would be sitting down to a computer freshly installed with some OS and various dependencies already setup for you. Handy!
In this specific case, you are getting an image from OpenJDK. https://hub.docker.com/_/openjdk.
Edit to add detail: As a number of comments state, Java needs the JVM installed on some OS to run a Java program. If you look down at image variants in the docker hub link, you'll see that you are requesting an image with Debian Linux and OpenJDK 11.
Related
I have to bundle up my system using Docker. But my system uses Java (JAR file to run) and python with PyTorch. I initially tried to use openjdk:buster base Docker image and then installed python3 on top of it. So both JAR and PyTorch worked, but PyTorch is only CPU supportive. But now I have to speed-up my PyTorch code using GPU, and for that I need NVIDIA-Cuda. In a separate Docker, I found nvidia/cuda:10.2-base-ubuntu18.04 works for my PyTorch. But this Docker can't run JAR file.
So I am stuck in combining these 2. I either want to
install NVIDIA-Cuda dependencies to openjdk Docker base image
install openjdk (openjdk-14) dependencies to NVIDIA-Cuda Docker base image
Anyone has any suggestions on how I can do that or any alternative hacks ?
You can have a single image, instead of two by creating your own docker image that uses the nvidia image, and install java on it. I.e. have a Dockerfile as below
FROM nvidia/cuda:10.2-base-ubuntu18.04
RUN apt-get update
RUN apt-get install openjdk-14-jdk
COPY <your jar file> <a path>
CMD [ "java" "other java flags/args>" "-jar" "<path to your jar file>"]
run docker build on that Dockerfile, and docker run as you normally would, and your java code should have access to NVIDIA-Cuda. (Also note, some prefer ENTRYPOINT to CMD)
I am working on a little application that I want to run in a docker container on a Raspberry Pi (Model 4B, 32Bit, 4GB RAM). I am learning Docker currently, so far I have only built and run stuff on my dev machine ( a mac). The container setup and jar work without problems on that machine.
This is my very basic Dockerfile:
FROM java:8
WORKDIR /
ADD my_jar.jar my_jar.jar
EXPOSE 8080
CMD java -jar my_jar.jar
As you can see there is not much going on. I would like to just be able to Docker build . -t myDockerImage on the pi and then run my container. Building succeeds, according to the output, but running fails with an error:
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm/v7) and no specific platform was requested
standard_init_linux.go:219: exec user process caused: exec format error
I did some research, and all I could find was the suggestion to use OpenJDK. Did that, but can't build, it says:
no matching manifest for linux/arm/v7 in the manifest list entries
I looked around on Docker Hub, but it doesn't seem like there is an image for java that fits this architecture. Or maybe I don't know what to look for...?
So how do I get java in a docker container onty this machine (preferrable in a fairly easy, convenient way, but if that doesn't exist, I am happy to do it the hard way, what ever that might entail)?
Btw: This is what lshw states about the RPi:
raspberrypi
description: ARMv7 Processor rev 3 (v7l)
product: Raspberry Pi 4 Model B Rev 1.1
serial: 10000000b3320840
width: 32 bits
capabilities: smp
The problem is that the JRE still depends on the operating system and that depends on the platform architecture. The standard docker image for Java will most likely use a amd64/x64 based Linux OS with the appropriate JDK installed.
Raspberry PI, however, uses the ARM architecture, hence the output ARMv7 Processor rev 3 (v7l).
So you need an ARM-compatible image such as one of those: https://hub.docker.com/r/arm32v7/adoptopenjdk
I'm not sure how compatible ARM v7 and v8 are so I'd suggest you stick to the v7 images :)
Looks like it's getting built for another platform, by default. Did you try passing --platform=linux/arm/v7 while building the image?
See if this helps.
I am trying to run java with docker image in gitlab.
Here is my docker file.
FROM java:latest
FROM perl
COPY . /
ENTRYPOINT ["/usr/bin/perl", "/myapp_entrypoint.pl"]
I was able to build docker image successfully and run perl commands but java commands are not working.
My application is a linux application and I am running 'java -version'. I am not getting any output completely blank output for version command.
What would be the issue? Do I need to add anything related linux as I am running 'java -version' as linux command?
You don't specify what OS you're running in your container, but the main issue is that you're blowing away your Java layer with another FROM directive.
From the documentation, emphasis mine:
Each FROM instruction clears any state created by previous instructions.
So I'd espouse a solution in which I install Perl (if I really needed to) after having my base Java image.
However, if you use the base OpenJDK images, Perl comes preinstalled, so that will simplify your Dockerfile significantly.
FROM openjdk:latest
COPY . /
ENTRYPOINT ["/usr/bin/perl", "/myapp_entrypoint.pl"]
I am new to containerizing apps using Docker. I could deploy a container including a war file. The war is basically a JAVA web application/servlet that sends back a video file upon receiving the request from end-user. The app deployment using docker was a success and app works fine. However I have some issues regarding its boot time.
From the moment that I create the container by hitting command docker run -it -d -p 8080:8080 surrogate, it takes about 5-6 minutes for the container to become operational, meaning that the first 5-6 minute of the container lifetime, it is not responding to end-user requests, and after that it works fine. Is there any way to accelerate this boot time?
Dockerfile includes:
FROM tomcat:7.0.72-jre7
ADD surrogate-V1.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
WORKDIR "/usr/local/tomcat/"
RUN wget https://www.dropbox.com/s/1r8awydc05ra8wh/a.mp4?dl=1
RUN cp a.mp4\?dl\=1 lego.mp4
(Posted on behalf of the OP).
First get rid of -d in the "docker run" command to see what is going on in the background. I noticed the war deployment phase is taking so long (around 15-20 minutes!!!)
The reason in my case was that the tomcat version in the Dockerfile was different from the Tomcat version in the environment from which I exported the web application as WAR. (How to check JRE version: in terminal enter: JAVA -version, checking the Tomcat version: from eclipse, when you are exporting, it shows the version).
In my case in Dockerfile, I had :
FROM tomcat:7.0.72-jre7
I changed it to:
FROM tomcat:6.0-jre7
It now takes less than 10 seconds!
In a nutshell, make sure that the Tomcat version and JRE versions in the Dockerfile are the same as the environment from which you exported the Java web application as WAR.
It's probably too soon to ask this question but I hope someone has already had the same problem as me.
I have to build a jar that run on a centos7, openjdk-7 machine.
I created a docker machine in a ridiculously short time :) but my issue is more a maven one.
My questions is :
How to generate a jar on remote machine (container powered by kitematic on mac for example :p) with the jdk and environment of this machine ?
Any help we'll be welcome.
--- EDIT
I develop on my mac and I would like to launch the build on a "remote" machine which is, in local, my docker container on the kitematic vm.
I think you're just asking how to compile source on your local Mac using a container. If you have boot2docker installed, the following works:
$ docker run -v $(pwd)/java_proj:/java_proj java:openjdk-7u65-jdk javac /java_proj/Test.java
Assuming you have a folder java_proj in your current directory with the file Test.java, this will compile the test file and should place the output in the same folder. We can do this as the boot2docker VM shares your home directory and we can then mount the folder as a volume inside the container. The code will be compiled using the compiler and environment of the java:openjdk-7u65-jdk image.