Search code examples
dockerdocker-desktop

docker build uses wrong Dockerfile content (bug?)


I have been using docker for years and now I have found a very strange behavior: the docker build command uses a Dockerfile which is not the one I have.

See next log:

PS D:\reposNovabase\charmander\charmanderWarehouse> type Dockerfile  
FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.11_9_openj9-0.26.0-alpine
WORKDIR /app
COPY build/libs/charmanderWarehouse-1.0.0-SNAPSHOT.jar /app
EXPOSE 8083
CMD ["java", "-Xmx70m", "-jar", "charmanderWarehouse-1.0.0-SNAPSHOT.jar"]
PS D:\reposNovabase\charmander\charmanderWarehouse> docker build -f Dockerfile -t javiersedano/charmander-warehouse:test1 .
[+] Building 0.2s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                  0.0s 
 => => transferring dockerfile: 32B                                                                                                                                   0.0s 
 => [internal] load .dockerignore                                                                                                                                     0.0s 
 => => transferring context: 2B                                                                                                                                       0.0s 
 => [internal] load metadata for docker.io/adoptopenjdk/openjdk11-openj9:jdk-11.0.11_9_openj9-0.26.0-alpine                                                           0.0s 
 => [1/3] FROM docker.io/adoptopenjdk/openjdk11-openj9:jdk-11.0.11_9_openj9-0.26.0-alpine                                                                             0.0s 
 => [internal] load build context                                                                                                                                     0.0s 
 => => transferring context: 57B                                                                                                                                      0.0s 
 => CACHED [2/3] WORKDIR /app                                                                                                                                         0.0s 
 => ERROR [3/3] COPY build/libs/charmanderPurchases-1.0.0-SNAPSHOT.jar /app                                                                                           0.0s 
------
 > [3/3] COPY build/libs/charmanderPurchases-1.0.0-SNAPSHOT.jar /app:
------
failed to compute cache key: "/build/libs/charmanderPurchases-1.0.0-SNAPSHOT.jar" not found: not found
PS D:\reposNovabase\charmander\charmanderWarehouse> 

See how the Dockerfile uses

COPY build/libs/charmanderWarehouse-1.0.0-SNAPSHOT.jar /app

but the docker build fails with

ERROR [3/3] COPY build/libs/charmanderPurchases-1.0.0-SNAPSHOT.jar /app

Why is it trying to copy charmanderPurchases?!!

More hints: the file is failing (charmanderPurchases) is used in a previous Dockerfile in a brother project which is built a second before (all of this is part of a pseudo-CI .cmd process that builds a few images).

More hints: I have a workaround. If I use

type Dockerfile | build -f - -t javiersedano/charmander-warehouse:test1 .

then it works as expected; furthermore, after this workaround, the first command also works as expected... but then, the build that fails is the build of the brother project, because charmanderPurchases tries to COPY charmanderWarehouse... WTF?!

Looks like the Dockerfile content itself is being cached or something, and it is reusing a previous Dockerfile.

Environment: Docker Desktop for Windows 3.6.0 on Windows 10 using the WSL2 backend.

Edit1: tested with 4.0.1 and still fails.

Any hint? Any idea?


Solution

  • (See edit2 below for the final solution)

    Regarding your error, it could be coming from a .dockerignore file excluding charmanderWarehouse-1.0.0-SNAPSHOT.jar or one of its parent (maybe build)

    If it is not coming from .dockerignore, you could try option --no-cache=true to build your image without cache reusing.

    docker build --no-cache=true -f Dockerfile -t javiersedano/charmander-warehouse:test1 .

    More information here : https://docs.docker.com/engine/reference/commandline/build/#options

    EDIT:

    Dockerfile content not being updated seems to be due to the context not being updated by the builder for files with same size/timestamp : https://github.com/moby/buildkit/issues/1368

    Here is a possible workaround : docker builder prune between each build.

    EDIT 2, by Javier Sedano:

    After reading the github thread, looks like the problem is the following: Docker caches the Dockerfile and uses the timestamp and filesize as key for the chache. In my case, the Dockerfiles only differ on purchases/warehouse... which are both 9 characters-long... so the Dockerfiles have the same size. The timestamp are the same because they were downloaded as .zip files from GitLab.

    Workarounds (I have tested them and all of them worked for me, so the choice depends on the rest of constraints):

    A.-Use cat Dockerfile | docker build -f - -t xxxx:tttt

    B.-Modify the files not to have the same size (I do not like this solution because it may conflict again in the future).

    C.-Use docker builder prune -f before building.