Search code examples
javamavendockerdocker-build

Files not getting added in docker workspace


I have docker file

FROM java:8
# Install maven
RUN apt-get update
RUN apt-get install -y maven


WORKDIR /code/

# Prepare by downloading dependencies
#ADD pom.xml /mmt/CouchBaseClient/CB-RestAPI/CacheService/pom.xml
#RUN ["mvn", "dependency:resolve"]
#RUN ["mvn", "verify"]
ADD cacheService-0.0.1-SNAPSHOT.jar /code/cacheService-0.0.1-SNAPSHOT.jar 
ADD couchclient-0.0.1-SNAPSHOT.jar  /code/couchclient-0.0.1-SNAPSHOT.jar

EXPOSE 4567
CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml" ]

When I build this file I get the following output

Sending build context to Docker daemon 35.46 MB
Step 1 : FROM java:8
 ---> 736600fd4ae5
Step 2 : RUN apt-get update
 ---> Using cache
 ---> a3466698c29d
Step 3 : RUN apt-get install -y maven
 ---> Using cache
 ---> d0fb8e77f89a
Step 4 : WORKDIR /code/
 ---> Using cache
 ---> 197735d2da02
Step 5 : ADD cacheService-0.0.1-SNAPSHOT.jar /code/cacheService-0.0.1-SNAPSHOT.jar
 ---> 9ba30f5a2144
Removing intermediate container bd3c072ebbc6
Step 6 : ADD couchclient-0.0.1-SNAPSHOT.jar /code/couchclient-0.0.1-SNAPSHOT.jar
 ---> ef59315ed7fe
Removing intermediate container 0da1a69bdb51
Step 7 : EXPOSE 4567
 ---> Running in a2b32799dd6c
 ---> 3fb2b534d7c5
Removing intermediate container a2b32799dd6c
Step 8 : CMD /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml
 ---> Running in efb44e2bcdb3
 ---> 56637dfacc0d
Removing intermediate container efb44e2bcdb3
Successfully built 56637dfacc0d

But no directory named code is being made and so no files are being added even though it is giving no compilation error

Used method suggested by @VonC

ENTRYPOINT ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar" ]

and then used this command to run the image

docker run <image> -d <arguments>

Solution

  • First, don't forget that ADD <src>... <dest> can invalidate the cache for all following instructions from the Dockerfile if the contents of <src> have changed. See "Best practices" and use COPY instead of ADD.

    In both cases (ADD or COPY), if <dest> doesn’t exist, it is created along with all missing directories in its path.
    So no mkdir necessary.

    COPY cacheService-0.0.1-SNAPSHOT.jar /code/ 
    COPY couchclient-0.0.1-SNAPSHOT.jar  /code/
    

    Otherwise, the file cacheService-0.0.1-SNAPSHOT.jar in the folder /code/cacheService-0.0.1-SNAPSHOT.jar/!

    Finally, to be sure that the files are where they should be, open a bash:

    docker run --rm -it <yourImage> bash
    

    Or, if you have a running container:

    docker exec -it <yourContainer> bash
    

    And check what ls /code returns.

    Also:

    docker run --rm -it --entrypoint /bin/sh <yourImage> -c "ls -alrt"
    

    The OP Legendary Hunter confirms in the comments the files are there.

    The issue comes from the CMD which is not fully in exec form.
    Each parameter should be in its own quotes:

    CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar", "server", "cacheService.yml" ]
    

    If the last parameters are grouped together, CMD tries to access the jar file named "couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml", which obvioulsy does not exist.
    Hence the error message:

    "Error: Unable to access jarfile couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml"
    

    Instead of using CMD, use ENTRYPOINT (with the sa me exec form, each arg in its own double-quotes), and leave CMD undefined.
    That way, the arguments you will add to your docker run command will be passed to the ENTRYPOINT (which runs java -jar ...)

    Since "server", "cacheService.yml" are the two arguments to be passed to the running container:

    ENTRYPOINT ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar" ]
    

    Build and then:

    docker run --rm -it <image> server cacheService.yml
    

    Once you know it is working, launch it in detached mode:

    docker run -d <image> server cacheService.yml