Could not resolve host in Docker

Setting up a simple Docker image I came across the type of error that happens quite often at the start of projects. An issue revolving around resolving hosts. These are the steps I took to fix the exit status 128: could not resolve host.

package main

import (
	"fmt"
	"log"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello World")
}

func main() {
	http.HandleFunc("/", handler)
	log.Println("listening on 8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
FROM golang:1.9

WORKDIR /go/src/app
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]
version: "3.7"
services:
  golang_env:
    build:
      context: ./app
    ports:
      - "8080:8080"

Initially when building I had no issues at all. But as soon as I included a dependency from github.com, the container would no longer build as expected.

// Web Toolkit gorilla/mux is a powerful URL router and dispatcher http://www.gorillatoolkit.org/
import (
	"github.com/gorilla/mux"
)
$  docker-compose build --no-cache
Building golang_env
Step 1/7 : FROM golang:1.9
 ---> ef89ef5c42a9
Step 2/7 : WORKDIR /go/src/app
 ---> Running in 8dd5273bf584
Removing intermediate container 8dd5273bf584
 ---> dc131ead0c22
Step 3/7 : COPY . .
 ---> 07850de847c8
Step 4/7 : RUN go get -d -v ./...
 ---> Running in 042c62d6d378
github.com/gorilla/mux (download)
# cd .; git clone https://github.com/gorilla/mux /go/src/github.com/gorilla/mux
Cloning into '/go/src/github.com/gorilla/mux'...
fatal: unable to access 'https://github.com/gorilla/mux/': Could not resolve host: github.com
package github.com/gorilla/mux: exit status 128
ERROR: Service 'golang_env' failed to build: The command '/bin/sh -c go get -d -v ./...' returned a non-zero code: 1

We made it to the 4th build step, then because we added the additional dependency causing the RUN go get in the Dockerfile to fail it’s git clone

Online resources provided some debugging steps.
First I checked the DNS on the local machine with dig github.com or nslookup github.com

$ nslookup github.com
Server:  mynetwork.home
Address:  192.168.2.1

DNS request timed out.
    timeout was 2 seconds.
DNS request timed out.
    timeout was 2 seconds.
Non-authoritative answer:
Name:    github.com
Address:  192.30.253.113

Next I looked at the status of git on my local machine

# On your local machine clone the repo manually
git clone https://github.com/gorilla/mux
# If unsuccessful try to unset any proxy settings that may have been set
# https://github.com/Homebrew/brew/issues/1414#issuecomment-259034930
git config --global --unset http.proxy 
git config --global --unset https.proxy

I’ve had similar issues while working with private repos and followed the helpful steps on https://divan.dev/posts/go_get_private/. To see if I was having issues with specifically https.

git config --global url."git@github.com:".insteadOf "https://github.com/"
[url "git@github.com:"]
	insteadOf = https://github.com/
FROM golang:1.9

WORKDIR /go/src/app
COPY . .

RUN echo "[url \"git@github.com:\"]\n\tinsteadOf = https://github.com/" >> /root/.gitconfig
RUN mkdir /root/.ssh && echo "StrictHostKeyChecking no " > /root/.ssh/config

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]
# cd .; git clone https://github.com/gorilla/mux /go/src/github.com/gorilla/mux
Cloning into '/go/src/github.com/gorilla/mux'...                                                      Warning: Permanently added 'github.com,x.x.x.x' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.                                                                                              package github.com/githubnemo/CompileDaemon: exit status 128

After making the changes to my Dockerfile, I could still see issues resolving the host. If after updating the dockerfile you’re seeing an error about Permission denied (publickey) failing, check out the link for Divan’s solution.

Finally I decided to restart my docker instance. And that was the solution this time.

# Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command. 
docker-machine restart

# Check that docker can resolve domains
docker run busybox nslookup github.com
docker - healthy again

Thankfully that resolved my issues. Had they not I would have next checked other search results; like developer Robin Winslow’s Fix Docker’s networking DNS config

Leave a Reply

Your email address will not be published. Required fields are marked *