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:

  • Boot2Docker: can't get ports forwarding to work
  • Docker containers on travis-ci
  • gitlab can not change gitlab url ,i edit config,yml , gitlab.yml
  • Understanding Docker
  • Docker: Cannot pull an image that was pushed to a private registry
  • Oracle impdp running way too long
  • 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?

  • How can I make other machines on my network access my Docker containers (using port mapping)?
  • Nodejs dev environnement with docker volumes
  • Testing multiple docker images with RSpec
  • How to set image stored path in docker registry?
  • artifactory oss docker 5.0.0 upgraded to 5.1.0 now it won't start
  • Docker and node_modules - put them in a layer, or a volume?
  • 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.