Hi,
I'm trying to retrieve some json from a server in our network.
The problem I'm having is that I get an error: 'Could not resolve host: server.company.com'
I'm using the 'swift:5.6.1-slim' image as the source for my container.
So my question is, Is there any special I need to do to my container to get access to servers on the network?
(If I spin up a simple busybox container, I can ping devices on the network and even Internet just fine. 'docker run -dit --name alpine1 alpine ash')
func fetch(url: URL, headers: [String: String] = [:]) {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
URLSession.shared.dataTask(with: request) { (data, response, error) in
print(error?.localizedDescription ?? "")
guard let data = data else {
print("No Data")
return
}
print(String(data: data, encoding: .utf8)!)
print("Hello, World, It's \(Date())")
}.resume()
}
I started my container as follows:
docker run -dit --rm --name NowBoarding now-boarding:0.1.0.RC1
P.s. These are my first steps at using swift in a docker container...
lukasa
(Cory Benfield)
2
I'd avoid the Swift question for now and just establish a shell within that container and see if you can use ping to hit the server. If it fails the DNS lookup, try taking a look at the contents of /etc/hosts and see how you're doing your DNS.
This seems highly likely to be relying on a corporate DNS server that is not getting propagated into the docker container.
I tried that, using "docker run -it --rm swift /bin/bash" and then do a ping, but the image doesn't have the ping command.
root@cadff2ae8c3e:/# ping www.google.com
bash: ping: command not found
Can it be that this image wasn't meant to do outgoing traffic?
P.s. the hosts file:
root@cadff2ae8c3e:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 cadff2ae8c3e
lukasa
(Cory Benfield)
4
Sorry, I meant /etc/resolv.conf. Lazy morning.
As for ping, no, it just means it's not installed. You can do an apt-get update and an apt-get install iputils-ping to get it installed.
Installed ping to test and you were correct. The container has networking capabilities.
I modified my code to download a json from a server on the internet and that succeeds. Downloading it from the server in our company's network still fails....
So I guess I have to put our company's DNS server address in /etc/resolv.conf I guess?
docker run -it --rm now-boarding:0.1.0.RC1 cat /etc/resolv.conf ─╯
# DNS requests are forwarded to the host. DHCP DNS options are ignored.
nameserver 192.168.65.5
%
Or do I have to do something in docker itself (because the 192.168.65.5 address is something from docker)?
lukasa
(Cory Benfield)
6
I suspect docker is attempting to populate that with something from the host but it's not sufficient. 192.168.65.5 is going to be a local network connection of some kind. Are you running Docker for Mac, Docker for Windows, or Docker on Linux?
Working on a mac here.
You've been a great help. I got it working now.
Container networking | Docker Documentation says I should pass the DNS server address using the --dns argument and that works. I can now access the resource I wanted.
This was the trick:
docker run -it --dns x.x.x.x --name NowBoarding now-boarding:0.1.0.RC1
Thanks for your swift help here ;-)