<p>So Jenkins is installed inside the cluster with this <a href="https://github.com/helm/charts/tree/master/stable/jenkins" rel="nofollow noreferrer">official helm chart</a>. And this is my installed plugins as per helm release values: </p>

<pre><code>  installPlugins:
  - kubernetes:1.18.1
  - workflow-job:2.33
  - workflow-aggregator:2.6
  - credentials-binding:1.19
  - git:3.11.0
  - blueocean:1.19.0
</code></pre>

<p>my Jenkinsfile relies on the following pod template to spin up slaves:</p>

<pre><code>kind: Pod
spec:
  # dnsConfig:
  #   options:
  #     - name: ndots
  #       value: "1"
  containers:
  - name: dind
    image: docker:19-dind
    command:
    - cat
    tty: true
    volumeMounts:
    - name: dockersock
      readOnly: true
      mountPath: /var/run/docker.sock
    resources:
      limits:
        cpu: 500m
        memory: 512Mi
  volumes:
  - name: dockersock
    hostPath: 
      path: /var/run/docker.sock
</code></pre>

<p>Slaves (pod /dind container) starts nicely as expected whenever there is new Build. </p>

<p>However, it broke at the step of "docker build" in ( Jenkinsfile pipeline
 <code>docker build -t ...</code> ) and it breaks there : </p>

<pre><code>Step 16/24 : RUN      ../gradlew clean bootJar

 ---&gt; Running in f14b6418b3dd

Downloading https://services.gradle.org/distributions/gradle-5.5-all.zip


Exception in thread "main" java.net.UnknownHostException: services.gradle.org

    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)

    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)

    at java.base/java.net.Socket.connect(Socket.java:591)

    at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:285)

    at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)

    at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182)

    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)

    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)

    at java.base/sun.net.www.protocol.https.HttpsClient.&lt;init&gt;(HttpsClient.java:265)

    at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)

    at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)

    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)

    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)

    at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)

    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)

    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)

    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)

    at org.gradle.wrapper.Download.downloadInternal(Download.java:67)

    at org.gradle.wrapper.Download.download(Download.java:52)

    at org.gradle.wrapper.Install$1.call(Install.java:62)

    at org.gradle.wrapper.Install$1.call(Install.java:48)

    at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:69)

    at org.gradle.wrapper.Install.createDist(Install.java:48)

    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107)

    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:63)

The command '/bin/sh -c ../gradlew clean bootJar' returned a non-zero code:
</code></pre>

<p>At the first galance, I thought it's DNS resolution issue with the Slave container (<code>docker:19-dind</code>) since it is alpine.
That's why I debug its <code>/etc/resolv.conf</code> by adding <code>sh "cat /etc/resolv.conf"</code> in the Jenkinsfile. </p>

<p>I got : </p>

<pre><code>nameserver 172.20.0.10
search cicd.svc.cluster.local svc.cluster.local cluster.local ap-southeast-1.compute.internal
options ndots:5
</code></pre>

<p>I removed the last line <code>options ndots:5</code> as per recommendation of many thread on the internet.</p>

<p>But it does not fix the issue. 😔</p>

<p>I thought again and again and I realized that the container responsible for this error is not the Slave (docker:19-dind), instead, it is the intermediate containers that are opened to satisfy <code>docker build</code>. </p>

<p>As consequence,  I added <code>RUN cat /etc/resolv.conf</code> as another layer in the Dockerfile (which starts by <code>FROM gradle:5.5-jdk11</code>). </p>

<p>Now, the <code>resolv.conf</code> is different : </p>

<pre><code>Step 15/24 : RUN cat /etc/resolv.conf

 ---&gt; Running in 91377c9dd519

; generated by /usr/sbin/dhclient-script

search ap-southeast-1.compute.internal

options timeout:2 attempts:5

nameserver 10.0.0.2

Removing intermediate container 91377c9dd519

 ---&gt; abf33839df9a

Step 16/24 : RUN      ../gradlew clean bootJar

 ---&gt; Running in f14b6418b3dd

Downloading https://services.gradle.org/distributions/gradle-5.5-all.zip

Exception in thread "main" java.net.UnknownHostException: services.gradle.org
</code></pre>

<p>Basically, it is a different nameserver <code>10.0.0.2</code> than the nameserver of the slave container <code>172.20.0.10</code>. There is NO <code>ndots:5</code> in resolv.conf this intermediate container.</p>

<p>I was confused after all these debugging steps and a lot of attempts. </p>

<h2>Architecture</h2>

<pre><code>Jenkins Server (Container )
     ||
(spin up slaves)
     ||__ SlaveA (Container, image: docker:19-dind)
             ||
       ( run "docker build" )
             ||
             ||_ intermediate (container, image: gradle:5.5-jdk11 )
</code></pre>

Abdennour TOUMI
<p>Just add <code>--network=host</code> to <code>docker build</code> or <code>docker run</code>.</p>

<pre><code> docker build --network=host foo/bar:latest .
</code></pre>

<p>Found the answer <a href="https://github.com/awslabs/amazon-eks-ami/issues/183#issuecomment-463687956" rel="nofollow noreferrer">here</a></p>
