Trevor
Trevor

Reputation: 11

Building a docker image from dockerfile, can't get java to run correctly, but can run java from image as command line parameters in docker run command

I am new to docker and dockerfile files, having just started trying to write them. I have built a simple java console application and can successfully build a docker image from a dockerfile, but if I include

CMD ["java","-jar","app.jar"]

when I try to run the image I always get a bin/sh error, typically "java not found" or the like.

However, when I don't include the CMD line and just use this Dockerfile to build my image

FROM openjdk:8-jre-alpine
COPY app.jar /app.jar

and then run

docker run -it --rm my-container:tag

I can then run

java -jar app.jar

and the application runs as expected.

I can also run

docker run -it --rm my-container:tag java -jar app.jar

and the application runs as expected.

Every guide I read says I should be able to use CMD or ENTRYPOINT as written above, but nothing ever works.

What might I be missing in this simple example?

Thank you, Trevor

EDIT: I am running docker version 18.06.1-ce-mac73 (26764) on MacOS Sierra. I am not positive that docker works this way, but I have two image versions in my public docker hub. The dockerfile for v1 is:

FROM openjdk:8-jre-alpine
COPY 454calendar.jar app.jar

The dockerfile for v2 is:

FROM openjdk:8-jre-alpine
ENV PROJECT_DIR=/app
WORKDIR $PROJECT_DIR
COPY 454calendar.jar $PROJECT_DIR

If I add

CMD [“java”,”-jar”,”454calendar.jar”]

to the v2 dockerfile and rebuild, I get this error with the docker run command.

/bin/sh: [“java”,”-jar”,”454calendar.jar”]: not found

Without the CMD line, I can run container and it starts right into the /app working directory where I can run the java command and execute the program.

The two versions of the container in my public docker repository do not have the CMD line in their respective dockerfiles.

Upvotes: 0

Views: 7889

Answers (2)

Trevor
Trevor

Reputation: 11

The solution was maddeningly simple. Thanks to @Rakesh, I checked the configuration for TextEdit on MacOS and saw that Smart Quotes was turned on. Once I turned off that option and retyped the double quotes, then rebuilt and ran the docker container, the application started up just as expected.

Upvotes: 1

Rakesh
Rakesh

Reputation: 39

I don't see any problem with your approach. I was able to make a HelloWorld application to run with the below Dockerfile.

FROM openjdk:8-jre-alpine
RUN mkdir /app
RUN cd /app
COPY HelloWorld.jar /app/HelloWorld.jar
WORKDIR /app
CMD ["java","-jar", "HelloWorld.jar"]

I'm on the following docker version

docker -v
Docker version 18.06.1-ce, build e68fc7a

docker-compose -v
docker-compose version 1.22.0, build f46880f

Upvotes: 0

Related Questions