Reputation: 2722
what is expected
docker should build the image and run the container out of it
Dockerfile
FROM centos:7
ENV JAVA_VERSION 8u191
ENV BUILD_VERSION b12
RUN yum -y install wget; wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/$JAVA_VERSION-$BUILD_VERSION/2787e4a523244c269598db4e85c51e0c/jdk-$JAVA_VERSION-linux-x64.rpm" -O /tmp/jdk-8-linux-x64.rpm; yum -y install /tmp/jdk-8-linux-x64.rpm
# JDK stripping
RUN rm -f /usr/java/jdk1.8.0_77/src.zip /usr/java/jdk1.8.0_77/javafx-src.zip
RUN rm -rf /usr/java/jdk1.8.0_77/lib/missioncontrol/ /usr/java/jdk1.8.0_77/lib/visualvm/ /usr/java/jdk1.8.0_77/db/
RUN alternatives --install /usr/bin/java java /usr/java/latest/bin/java 1
RUN alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 1
ENV JAVA_HOME /usr/java/latest
ENV PATH=$PATH:/usr/java/latest/bin/java
RUN echo "$PATH"
RUN rm -f /tmp/jdk-8-linux-x64.rpm; yum -y remove wget; yum -y clean all
COPY target/gs-handling-form-submission-0.1.0.jar /tmp/gs-handling-form-submission-0.1.0.jar
CMD [ "java -jar /tmp/gs-handling-form-submission-0.1.0.jar" ]
build-context is the current directory where Dockerfile resides. following is the directory structure
total 8
-rw-r--r--. 1 root root 1079 Oct 29 03:42 Dockerfile
-rw-rw-r--. 1 ec2-user ec2-user 1917 Oct 27 09:17 pom.xml
drwxrwxr-x. 4 ec2-user ec2-user 30 Oct 27 10:15 src
drwxr-xr-x. 9 root root 261 Oct 27 10:19 target
i am executing following command to build the image :
docker build --no-cache -f Dockerfile -t cpa:latest .
but when running the container , getting following message
docker run -d -p 8081:8081/tcp cpa
b2ba9831f1b74c73669843658727ca0d64fcd9de915739b96e17dcfd6e63c7db
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"java -jar /tmp/gs-handling-form-submission-0.1.0.jar\": stat java -jar /tmp/gs-handling-form-submission-0.1.0.jar: no such file or directory": unknown.
I am not sure why the built jar is not available in the /tmp dir of the container file system
in the docker documentation COPY
is the right keyword to be used to copy files from host machine to container file system ...
but then where is the issue ..?
please suggest
EDIT 1 : fixed the type mistake from 77 to 191 here is the latest Dockerfile....
FROM centos:7
ENV JAVA_VERSION 8u191
ENV BUILD_VERSION b12
RUN yum -y install wget; wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/$JAVA_VERSION-$BUILD_VERSION/2787e4a523244c269598db4e85c51e0c/jdk-$JAVA_VERSION-linux-x64.rpm" -O /tmp/jdk-8-linux-x64.rpm; yum -y install /tmp/jdk-8-linux-x64.rpm
# JDK stripping
RUN rm -f /usr/java/jdk1.8.0_191/src.zip /usr/java/jdk1.8.0_191/javafx-src.zip
RUN rm -rf /usr/java/jdk1.8.0_191/lib/missioncontrol/ /usr/java/jdk1.8.0_191/lib/visualvm/ /usr/java/jdk1.8.0_191/db/
RUN alternatives --install /usr/bin/java java /usr/java/latest/bin/java 1
RUN alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 1
ENV JAVA_HOME /usr/java/latest
ENV PATH=$PATH:/usr/java/latest/bin/java
RUN echo "$PATH"
RUN rm -f /tmp/jdk-8-linux-x64.rpm; yum -y remove wget; yum -y clean all
COPY target/gs-handling-form-submission-0.1.0.jar /tmp/gs-handling-form-submission-0.1.0.jar
CMD [ "java -jar /tmp/gs-handling-form-submission-0.1.0.jar" ]
further, the target
dir is part of the build context
please refer below:
[root@ip-172-31-14-242 cpa_final]# pwd
/tmp/cpa_final
[root@ip-172-31-14-242 cpa_final]# ll
total 8
-rw-r--r--. 1 root root 1084 Oct 29 05:16 Dockerfile
-rw-rw-r--. 1 ec2-user ec2-user 1917 Oct 27 09:17 pom.xml
drwxrwxr-x. 4 ec2-user ec2-user 30 Oct 27 10:15 src
drwxr-xr-x. 9 root root 261 Oct 27 10:19 target
[root@ip-172-31-14-242 cpa_final]# cd target/
[root@ip-172-31-14-242 target]# ll
total 17304
drwxr-xr-x. 4 root root 36 Oct 27 10:18 classes
drwxr-xr-x. 3 root root 25 Oct 27 10:18 generated-sources
drwxr-xr-x. 3 root root 30 Oct 27 10:18 generated-test-sources
-rw-r--r--. 1 root root 17708542 Oct 27 10:19 gs-handling-form-submission-0.1.0.jar
-rw-r--r--. 1 root root 4858 Oct 27 10:18 gs-handling-form-submission-0.1.0.jar.original
drwxr-xr-x. 2 root root 28 Oct 27 10:18 maven-archiver
drwxr-xr-x. 3 root root 35 Oct 27 10:18 maven-status
drwxr-xr-x. 2 root root 121 Oct 27 10:18 surefire-reports
drwxr-xr-x. 3 root root 19 Oct 27 10:18 test-classes
[root@ip-172-31-14-242 target]#
EDIT 2:
the mentioned jar file is there in /tmp
dir of the container file system. PSB
[root@ip-172-31-14-242 ~]# docker run -it cpa /bin/bash
[root@4018a6c2df8e /]# cd /tmp
[root@4018a6c2df8e tmp]# ll
total 17300
-rw-r--r--. 1 root root 17708542 Oct 27 10:19 gs-handling-form-submission-0.1.0.jar
-rwx------. 1 root root 836 Oct 6 19:15 ks-script-7RxiSx
-rw-------. 1 root root 0 Oct 6 19:14 yum.log
[root@4018a6c2df8e tmp]#
Upvotes: 13
Views: 28907
Reputation: 3885
The CMD command has three forms. Each of them can do the job for you.
CMD ["param1","param2"]
: "parameter" formCMD ["executable","param1","param2"]
: "exec" formCMD command param1 param2
: "shell" formThe first two look the same but their meaning depends on whether there is an ENTRYPOINT
in the Dockerfile
or not.
The parameter form is used when you have an ENTRYPOINT
in your Dockerfile
. In this case, the strings from the CMD command are the arguments of the executable defined in the ENTRYPOINT
. In your case:
ENTRYPOINT ["/<path-to-your-java-binary>/java"]
CMD ["-jar", "myjar.jar"]
NB. The parameters listed in CMD
must be different strings. CMD ["a", "b"]
means you pass two arguments to the command while CMD ["a b"]
means only one parameter.
The exec form is used if there is no ENTRYPOINT
in the Dockerfile
. In this case, the first string is the command and the subsequent ones are the individual arguments. In your case:
CMD ["/<path-to-your-java-binary>/java", "-jar", "myjar.jar"]
Be aware of that in the exec form there is no shell processing, ie. shell variables are not defined.
In the shell form, however, you have access the shell variables and the first argument (command
) will be executed by the shell /bin/sh
with the given parameters. In your case:
CMD /<path-to-your-java-binary>/java -jar myjar.jar
Take-away: the simplest is the best in this case.
Upvotes: 8
Reputation: 183
For me below change worked that is changing from
CMD ["java -jar myjar.jar"]
to
CMD java -jar myjar.jar
Try removing square brackets and double quotes
Upvotes: 2
Reputation: 8163
ENV PATH=$PATH:/usr/java/latest/bin/java
should probably be
ENV PATH=$PATH:/usr/java/latest/bin
I assume what is not being found is the java executable. Please check that java is really on the PATH.
What is ALSO wrong is this line:
CMD [ "java -jar /tmp/gs-handling-form-submission-0.1.0.jar" ]
This should read:
CMD [ "java", "-jar", "/tmp/gs-handling-form-submission-0.1.0.jar" ]
Right now it is looking for a file called "java -jar /tmp/gs-handling-form-submission-0.1.0.jar" and tries to execute that.
Upvotes: 13