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

前端 未结 4 375
南笙
南笙 2020-12-29 22:31

I am using a Jenkins pipeline to build a Dockerfile.

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

As shown:

<         


        
相关标签:
4条回答
  • 2020-12-29 23:12

    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.

    0 讨论(0)
  • 2020-12-29 23:16

    None of the above ideas works for my case, and I finally got it work as below pipeline without chaning anything on Dockerfile:

    1. Hardcode the build tag like "latest",
    2. Get your own tag via sh,
    3. Push your image with the hardcoded tag first,
    4. Push the image again with your customized tag.
    def my_own_tag = "unknown"
    my_own_tag = sh ( script: 'git tag | tail -n1', returnStdout: true ).trim()
    docker_img = docker.build("latest")
    docker_img.push("latest")
    docker_img.push("${my_own_tag}")
    
    0 讨论(0)
  • 2020-12-29 23:19

    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

    • https://issues.jenkins-ci.org/browse/JENKINS-44789
    • https://issues.jenkins-ci.org/browse/JENKINS-44609
    • https://issues.jenkins-ci.org/browse/JENKINS-31507

    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 .)

    0 讨论(0)
  • 2020-12-29 23:21

    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

    0 讨论(0)
提交回复
热议问题