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

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

CC BY-SA 4.0
2019-09-14T10:08:40.260Z
2019-09-14T10:08:40.260Z
Abdennour TOUMI
<p>Have you considered not using DinD? I would look to tools like <a href="https://github.com/GoogleContainerTools/kaniko" rel="nofollow noreferrer">Kaniko</a>, <a href="https://github.com/containers/buildah" rel="nofollow noreferrer">Buildah</a>, <a href="https://github.com/genuinetools/img" rel="nofollow noreferrer">Img</a>, <a href="https://github.com/openSUSE/umoci" rel="nofollow noreferrer">Umochi</a>, <a href="https://github.com/GoogleCloudPlatform/runtimes-common/tree/master/ftl" rel="nofollow noreferrer">FTL</a>, or <a href="https://github.com/bazelbuild/rules_docker" rel="nofollow noreferrer">Bazel</a>.</p>

<p>Here's a good example from the Jenkins Kubernetes plugin that builds containers without using Docker or DinD in Kubernetes.</p>

<p><a href="https://github.com/jenkinsci/kubernetes-plugin/blob/master/examples/kaniko.groovy" rel="nofollow noreferrer">https://github.com/jenkinsci/kubernetes-plugin/blob/master/examples/kaniko.groovy</a></p>

CC BY-SA 4.0
2019-09-15T23:22:21.990Z
2019-09-15T23:34:39.887Z
2019-09-15T23:34:39.887Z
casey vega
<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>

<jenkins><amazon-web-services><kubernetes><dns><docker-in-docker>
docker build throws UnknownHostException with jenkins dind slave + kubernetes plugin
Abdennour TOUMI
# docker build throws UnknownHostException with jenkins dind slave + kubernetes plugin

(Asked by: username_0 on 2019-09-11)

So Jenkins is installed inside the cluster with this official helm chart. And this is my installed plugins as per helm release values: 
  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

my Jenkinsfile relies on the following pod template to spin up slaves:
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

Slaves (pod /dind container) starts nicely as expected whenever there is new Build. 
However, it broke at the step of "docker build" in ( Jenkinsfile pipeline
 docker build -t ... ) and it breaks there : 
Step 16/24 : RUN      ../gradlew clean bootJar

 ---> 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.(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:

At the first galance, I thought it's DNS resolution issue with the Slave container (docker:19-dind) since it is alpine.
That's why I debug its /etc/resolv.conf by adding sh "cat /etc/resolv.conf" in the Jenkinsfile. 
I got : 
nameserver 172.20.0.10
search cicd.svc.cluster.local svc.cluster.local cluster.local ap-southeast-1.compute.internal
options ndots:5

I removed the last line options ndots:5 as per recommendation of many thread on the internet.
But it does not fix the issue. 
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 docker build. 
As consequence,  I added RUN cat /etc/resolv.conf as another layer in the Dockerfile (which starts by FROM gradle:5.5-jdk11). 
Now, the resolv.conf is different : 
Step 15/24 : RUN cat /etc/resolv.conf

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

 ---> abf33839df9a

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

 ---> Running in f14b6418b3dd

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

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

Basically, it is a different nameserver 10.0.0.2 than the nameserver of the slave container 172.20.0.10. There is NO ndots:5 in resolv.conf this intermediate container.
I was confused after all these debugging steps and a lot of attempts. 
Architecture
Jenkins Server (Container )
     ||
(spin up slaves)
     ||__ SlaveA (Container, image: docker:19-dind)
             ||
       ( run "docker build" )
             ||
             ||_ intermediate (container, image: gradle:5.5-jdk11 )

**Tags:** jenkins, amazon-web-services, kubernetes, dns, docker-in-docker


---

## 2 answers

### Answer by username_0 on 2019-09-14. Score: 2 | Accepted answer

Found the answer here
Just add --network=host to docker build or docker run.
 docker build --network=host foo/bar:latest .


---

### Answer by username_1 on 2019-09-15. Score: 0

Have you considered not using DinD? I would look to tools like [Kaniko](https://github.com/GoogleContainerTools/kaniko), [Buildah](https://github.com/containers/buildah), [Img](https://github.com/genuinetools/img), [Umochi](https://github.com/openSUSE/umoci), [FTL](https://github.com/GoogleCloudPlatform/runtimes-common/tree/master/ftl), or [Bazel](https://github.com/bazelbuild/rules_docker).

Here's a good example from the Jenkins Kubernetes plugin that builds containers without using Docker or DinD in Kubernetes.

<https://github.com/jenkinsci/kubernetes-plugin/blob/master/examples/kaniko.groovy>


---

