2

I am trying to learn my way around Docker. I want to build a container/image for my java application. The Docker file looks like this:

FROM openjdk:7

COPY . C:/Users/name/Documents/NetBeansProjects/project1/src/mainpckg

WORKDIR C:/Users/name/Documents/NetBeansProjects/project1/src/mainpckg

RUN javac Main.java

CMD java Main

And i am calling it like this:

docker build -t my-java-app .

But it gives the following error:

$ docker build -t my-java-app .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM openjdk:7
 ---> 4a42f05dc422
Step 2 : COPY . C:/Users/name/Documents/NetBeansProjects/project1
/src/mainpckg
 ---> Using cache
 ---> 301de702fad9
Step 3 : WORKDIR C:/Users/name/Documents/NetBeansProjects/project1
s/src/mainpckg
 ---> Using cache
 ---> 1868e40b329e
Step 4 : RUN javac Main.java
 ---> Running in 66d7d769f425
javac: file not found: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
The command '/bin/sh -c javac Main.java' returned a non-zero code:
 2

I also tried the solution given here:

docker run --rm -v /mypathhere/mycode java:7 sh -c "cd mycode; javac mycode.java"

but without any results, i still got this error:

javac: file not found: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
1
  • Openjdk container is based on debian Linux distribution, I don't think C:/... is a valid path. Do you want to try it with something like /app and see if that works better? Commented Nov 20, 2016 at 4:30

2 Answers 2

1

There is something fishy about C:/. Try following, it must work

FROM java:8

# add the container directory from the host
RUN mkdir /opt/mainpckg

# copy the app to container directory
ADD . /opt/mainpckg

WORKDIR /opt/mainpckg

RUN javac Main.java

CMD java Main
Sign up to request clarification or add additional context in comments.

2 Comments

I am confused as to what your directories are suppose to point at. If set them exactly as you have how does Docker know where to look for the Main.java?
@Angelo Uknown As you see in the Dockefile, you have copied all your app files from host system to docker directory /opt/mainpckg at step 3 and set this directory as your present working directory at 4th step. So you are set to issue java commands being at present working directory /opt/mainpckg where your Main.Java resides. Hope it makes more sense now.
0

I don't know why you run with -v /mypathhere/mycode, as it creates a data volume which would overlay (temporarily overwrite) anything that was /mypathhere/mycode from the Dockerfile.

So try running without the -v part, in case /mypathhere/mycode is C:/Users/name/Documents/NetBeansProjects/project1 (assuming you are using Docker on Windows with Windows containers)

1 Comment

Now i got the following is not a valid repository/tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.