Docker Community Forums

Share and learn in the Docker community.

Running java application on docker


(Katkol) #1

I have two dockerfile’s.

Dockerfile1:

FROM java:8
WORKDIR /
ADD Client/target/Client-0.0.1-SNAPSHOT-jar-with-dependencies.jar Client.jar
ADD Client/resources resources
EXPOSE 35399 35400
CMD “java” “-jar” “Client.jar”

Dockerfile2:

FROM java:8
FROM mysql/mysql-server:5.7
ENV MYSQL_ROOT_PASSWORD=root
VOLUME ./mysql_db /var/lib/mysql
WORKDIR /
ADD Server/target/Server-0.0.1-SNAPSHOT-jar-with-dependencies.jar server.jar
ADD Server/resources resources
ADD Server Server
EXPOSE 5684 3306
COPY Server/accountingdb.sql /docker-entrypoint-initdb.d/accountingdb.sql
COPY Server/policydb.sql /docker-entrypoint-initdb.d/policydb.sql
CMD “java” “-jar” “server.jar”

When I run the first dockerfile, the java application starts without any errors, but when I run the second dockerfile, it gives the following error:

[Entrypoint] MySQL Docker Image 5.7.21-1.1.4
/bin/sh: java: command not found

Can someone help me find the problem.


(David Maze) #2

Right, it’s a MySQL server image and won’t contain a JVM.

As a general rule, if Docker gives an error message like “command not found” or “no such file or directory”, it means exactly what it says. It’s often helpful to do things like docker run --rm -it imagename /bin/sh to get a shell and see what exactly is inside the container.

(It sort of looks like you’re trying to squish two images together: Dockerfile2 builds two images, the first is just a copy of the standard java:8 container and the second is a MySQL image plus a jar file. You can’t do that. You should run the database server in a separate container from your application; Docker Compose is a good tool for running the two containers together.)

(Also, ADD will unpack things that look like tar or zip files. Java jar files are actually zip files, and ADD will unpack them. This often isn’t what you want. Usually, unless you really explicitly want the automatic unpacking, you should almost always use COPY instead of ADD.)


(Katkol) #4

Hi,

Thanks for your answer. Using docker-compose, I could run mysql server and java application. but now I ended up in new issue. The application is working fine on my computer, but when I port to docker and run, i face this error.

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Here is my docker-compose.yml file

version: '3'


services:


 mysql_server:
  image: mysql/mysql-server:5.7
  environment:
   MYSQL_ROOT_PASSWORD: root  
  ports:
   - "3306:3306"
  volumes:
   - "./mysql_db:/var/lib/mysql:rw"
   - "./Server:/docker-entrypoint-initdb.d/"

 java8:
  image: openjdk:8-jre
  ports:
   - "5684:5684"
  volumes:
   - "./Server:/Server:rw"
  entrypoint: ["java", "-jar", "/Server/target/Server-0.0.1-SNAPSHOT-jar-with-dependencies.jar"]

Any suggestions?