Docker – how to cache added directory?

Basically I have a bunch of thirdparty jar files from gradle build which are copied to external directory.
Those jars almost never change, but Docker somehow does not want to cache that layer.

My Docker file is pretty simplistic:

  • Strong-PM in Docker Container App Connect to Host MongoD
  • Docker: listen for .sql changes and re-provision database
  • can't connect to spotify kafka container, basic connection problems
  • Configure CodeSniffer on PhpStorm using Docker
  • Set up MySQL using Dockerfile
  • Does AWS ECS support per container dynamic scalability?
  • RUN mkdir -p /opt/app
    WORKDIR /opt/app
    COPY app.jar .
    COPY project /opt/app/project
    COPY external /opt/app/external
    EXPOSE 2550 7000
    

    The md5sum of all jar files and sizes in that external directory are identical. The stat only shows that inode is different on those files (expect for file times of course, but according to docker docs mtime is ignored when comparing) .

    Yet if I look at docker history, I see that the layer is not cached:

    tag: test-1

    1be1e9a7084b        17 hours ago        /bin/sh -c #(nop)  EXPOSE 2550/tcp 7000/tcp     0B
    e593cf804634        17 hours ago        /bin/sh -c #(nop) COPY dir:b2ed14f41affcee...   60.4MB
    a55be55e6e68        17 hours ago        /bin/sh -c #(nop) COPY dir:f1abbe11a6a64fe...   566kB
    e8823ec84dfb        17 hours ago        /bin/sh -c #(nop) COPY file:ba5ef09e42eda4...   523kB
    041f6f6957b5        17 hours ago        /bin/sh -c #(nop) WORKDIR /opt/app              0B
    15e4c78e545c        17 hours ago        /bin/sh -c mkdir -p /opt/app                    0B
    

    tag: test-2

    aacb13926d18        17 hours ago        /bin/sh -c #(nop)  EXPOSE 2550/tcp 7000/tcp     0B
    7ebe12849fad        17 hours ago        /bin/sh -c #(nop) COPY dir:b2ed14f41affcee...   60.4MB
    4bf898283d11        17 hours ago        /bin/sh -c #(nop) COPY dir:f1abbe11a6a64fe...   566kB
    886630b2a05a        17 hours ago        /bin/sh -c #(nop) COPY file:5ad60a31d9e72d...   523kB
    041f6f6957b5        17 hours ago        /bin/sh -c #(nop) WORKDIR /opt/app              0B
    15e4c78e545c        17 hours ago        /bin/sh -c mkdir -p /opt/app                    0B
    

    So how I can force docker to reuse a layer with those thirdparty jars that almost never change?
    Is it because the external directory is recreated everytime after build? Ar there any other ways?

  • automated docker hub builds won't trigger
  • Docker “config” Container / Docker image
  • docker node app always crashes on file change using nodemon
  • Docker Swarm - Can’t pull from private registry
  • Allowing Wordpress to be hosted on a subfolder within an already existing site
  • Mnesia doesn't recognize data after sys restart (no_exists error)
  • One Solution collect form web for “Docker – how to cache added directory?”

    Move the COPY external /opt/app/external layer to the top, as first COPY statement. If one Docker layer has changed, the follwoing layers will be invalidated. Move your most static COPYs and commands to the top and the frequently changing commands to the bottom of your Dockerfile.

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