How to resolve "Cannot retrieve .Id from docker" when building Docker image using Jenkins pipeline

10,393

Solution 1

It seems that there is a bug in that Jenkins plugin.

You can try removing multi stage build name ("AS final" as you don't need it):

FROM base
(....)

But if you really need to reference a previous built image (multi stage), a workaround can be using --copy-from 0 (0,1,2 as it corresponds, instead of the alias name)

Related issues in Jenkins


Edit

Documenting here the solution found by the OP:

I got this working by not using the Jenkinsfile pipeline file, but instead executing a Shell within the Jenkins job to execute the Docker build command. (docker build -t latest-build .)

Solution 2

I'm getting this problem because I'm using --target=<foo> to build my image only up to a certain point.

So my Dockerfile looks like this

FROM maven:3.6-jdk-8 AS BUILD

.. do build ..

FROM openjdk:8

COPY --from=BUILD /myapp/bin my_jar_file

And my build with docker.build(<tag>, "--target=BUILD .") fails with:

java.io.IOException: Cannot retrieve .Id from 'docker inspect openjdk:8'

This is because Jenkins is trying to inspect the second FROM openjdk:8 in the Dockerfile, and because that target didn't run Docker didn't pull down that image and it's unavailable to docker inspect

I've got a bunch of workarounds available to me:

  1. Ensure that image is available anyway by running docker pull openjdk:8 before building
  2. Remove --target=BUILD from my docker.build command and let it build the whole thing (not too big a deal for me as the build is the most expensive part)
  3. Avoid using docker.build and just sh "docker build --target=BUILD .

At the moment I'm not sure which one I'll go with

Solution 3

I ran into this same problem with Docker 18.09, but I wasn't using a multi stage build. In my case, I was getting:

java.io.IOException: Cannot retrieve .Id from 'docker inspect centos:7'

The first step in my Dockerfile was:

FROM centos:7

I was able to fix the problem with docker pull centos:7; after that, the Jenkins build was able to complete successfully.

Share:
10,393

Related videos on Youtube

fuzzi
Author by

fuzzi

Updated on September 27, 2022

Comments

  • fuzzi
    fuzzi over 1 year

    I am using a Jenkins pipeline to build a Dockerfile.

    The dockerfile successfully goes through all steps, and creates the docker image.

    As shown:

    Step 16/19 : FROM base AS final
     ---> <id>
    Step 17/19 : WORKDIR /app
     ---> Using cache
     ---> <id>
    Step 18/19 : COPY --from=publish /app .
     ---> Using cache
     ---> <id>
    Step 19/19 : ENTRYPOINT ["", "myapp.dll"]
     ---> Using cache
     ---> <id>
    Successfully built cb3y81938e88
    Successfully tagged myapp:latest
    

    However, after this, the shell Fails with the following error:

    java.io.IOException: Cannot retrieve .Id from 'docker inspect base AS final'
    

    Why does it throw this error despite the docker image successfully built? When I execute this on my local machine, the command exits on "Successfully tagged myapp:latest"

    My docker version is 18.03.1-ce.

    Any help on this issue would be greatly appreciated!

    • mkobit
      mkobit over 5 years
      My guess would be that the Jenkins Docker plugin(s) are not properly supporting multi-stage Docker builds.
  • fuzzi
    fuzzi over 5 years
    Thanks @Robert I removed the AS final to have just FROM base, however now the error has changed to Cannot retrieve .Id from 'docker inspect base' I don't have inspect in my Dockerfile, but suspect there is still an issue with running FROM base ?? I'll take a look at the related issues.
  • Robert
    Robert over 5 years
    One of those tickets mention a really weird workaround which is adding this "FROM scratch" at the very top of the Dockerfile, then prepend a space before each following "FROM". Can you please try it?
  • fuzzi
    fuzzi over 5 years
    thanks, I tried this, however I still got the error Cannot retrieve .Id from 'docker inspect base AS final' , with both potential solutions (the FROM scratch, with the FROM indents, and the removal of AS final) , I get the error Cannot retrieve .Id from 'docker inspect base'
  • fuzzi
    fuzzi over 5 years
    I got this working by not using the Jenkinsfile pipeline file, but instead executing a Shell within the Jenkins job to execute the Docker build command.
  • Arnold Balliu
    Arnold Balliu about 5 years
    This worked. To clarify as this answer is not as complete: You need to run 'docker pull...' in a different stage so that it runs before the agent directive if you are using different agents in stages. If you are using only one agent then I'd recommend you then look into using multiple agents to solve the issue by coupling it with this answer here.
  • olricson
    olricson about 5 years
    This issue has been fixed in Docker Plugin version 1.1.6 and Docker pipeline version 1.18
  • Romain
    Romain almost 5 years
    Reproduced with Docker Pipeline 1.18 and fixed by simply removing AS final. Thanks.