Docker – misunderstanding about the execution and volume

I have a following Dockerfile in my pet project:

FROM java:8
ADD target/sources-registry-0.0.1-SNAPSHOT.jar sources-registry.jar
RUN bash -c 'touch /sources-registry.jar'
ENTRYPOINT ["java","","-jar","/sources-registry.jar"]

And everything perfectly works – image is created and can be run. Now a bit of description about the project: it is a maven based project and before actually executing the Dockerfile I have to manually run mvn package.

  • Difference between docker run --user and --group-add parameters
  • Why are there so many docker images with vulnerabilities? [closed]
  • Jenkins Docker container not copying pre-installed plugins to JENKINS_HOME on start
  • Is there a way to setup bluemix as $DOCKER_HOST and use it with the docker binary?
  • Don't have permission to access / on this server (apache2) in docker
  • Setting up Text editor like Vim,Spacemacs to work with docker containers without installing tools on the host machine
  • However, if I change the Dokerfile to (because I do not want manually to run mvn package and want to automate it)

    FROM java:8
    RUN ls target
    RUN ./mvnw package
    ADD target/sources-registry-0.0.1-SNAPSHOT.jar sources-registry.jar
    RUN bash -c 'touch /sources-registry.jar'
    #ENTRYPOINT ["java","","-jar","/sources-registry.jar"]
    EXPOSE 8761

    then while execution I get /bin/sh: 1: ./mvnw: not found. However, mvnw is in my project files near the pom.xml.

    Even more if I just do following Dockerfile

    FROM java:8
    RUN ls target/

    then I get ls: cannot access target/: No such file or directory.

    Can someone please explain this behaviour?

    1. I mean why I can actually do something with target folder (first
      Dockerfile) even if does not exist (third Dockerfile)?
    2. How project files (and what files) get copied into a created
    3. The main question: Why second Dockerfile is not working? And how I can make it work?

  • does the FROM directive in a dockerfile allways pull the latest version of an image
  • Copying file from host to container
  • Docker docker-compose not picking up relevant cached image
  • How to execute the Entrypoint of a Docker images at each “exec” command?
  • Why do containers fail to fetch URIs in DC/OS?
  • Docker push: Image depends on an unauthorized parent
  • One Solution collect form web for “Docker – misunderstanding about the execution and volume”

    The ADD command copies over the sources-registry.jar file into the Docker image, so that the first example is able to execute it. If you want to use any other files inside the container, you need to include them in the image as well (using ADD or COPY). See the reference docs for more information

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.