Search code examples
dockerjenkinsmkdirkaniko

mkdir in kaniko fails with can't create directory - File exists


I'm trying to execute the following Dockerfile with Kaniko on a Jenkins node

FROM docker.mycompany.io/adoptopenjdk/openjdk11:jre-11.0.11_9-alpine

COPY app.jar app.jar

RUN mkdir /some-directory

But I'm getting the error

mkdir: can't create directory '/some-directory': File exists

I've checked the base image (adoptopenjdk/openjdk11:jre-11.0.11_9-alpine) and I can see that /some-directory does not exist so I'm not sure why kaniko is telling me the directory already exists.

I'm using the following Kaniko command

/kaniko/executor /
  --context dir://myproject/src/main/docker /
  --dockerfile myproject/src/main/docker/Dockerfile /
  --destination docker-staging.docker.mycompany.io/foo/myproject:111.222.333 /
  --registry-certificate docker-staging.docker.mycompany.io=/etc/ssl/certs/mycompany.crt /
  --verbosity=debug /
  --cache=true

Solution

  • It turns out that the Kaniko command actually succeeded the first time on a clean Jenkins node, it was only second / subsequent executions on the same Jenkins node where the mkdir would fail so it seems I had a dirty kaniko filesystem when the error occurs.

    I solved this by adding the --cleanup and --cache=false flags to the Kaniko command.

    I believe that in newer versions of Kaniko I could use the --cache-run-layers=false flag but I'm using an older version of Kaniko which does not have this option