I'm wondering why, with docker-compose, the container cannot resole host-name while it's working with docker ? The host is on different physical machine but on the same network.
My Dockerfile
FROM openjdk:8-jre-alpine
CMD ping -c 2 myhost
My docker-compose.yml
version: '3.3'
services:
net:
build: .
image: test/myimage:1.0
container_name: mycontainer
After a docker-compose build
I tried
docker run -it test/myimage:1.0
PING myhost (10.20.78.13): 56 data bytes
64 bytes from 10.20.78.13: seq=0 ttl=250 time=0.720 ms
64 bytes from 10.20.78.13: seq=1 ttl=250 time=0.515 ms
but
docker-compose up
Recreating mycontainer ...
Recreating mycontainer ... done
Attaching to mycontainer
mycontainer | ping: bad address 'myhost'
mycontainer exited with code 1
What can I do to have it working ?
Edit1
Using cat /etc/resolv.conf
instead of ping -c 2 myhost
docker-compose up
Recreating mycontainer ...
Recreating mycontainer ... done
Attaching to mycontainer
mycontainer | search myorg.intra
mycontainer | nameserver 127.0.0.11
mycontainer | options ndots:0
mycontainer exited with code 0
and
docker run -it test/myimage:1.0
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.19.128.55
nameserver 10.19.142.23
nameserver 10.19.128.56
search myorg.intra
You need use the dns entries in your compose also
version: '3.3'
services:
net:
build: .
image: test/myimage:1.0
container_name: mycontainer
dns:
- 10.19.128.55
- 10.19.142.23
- 10.19.128.56
dns_search: myorg.intra
If you don't want to specify them per container then you can even set them directly at the daemon level.
Create or edit /etc/docker/daemon.json
and below entries in the JSON file
{
"dns": ["10.19.128.55", "10.19.142.23", "10.19.128.56"],
"dns-search": ["myorg.intra"],
}
Restart the docker service using service docker restart
. Then you should not need the entries in the docker-compose
. Your original docker-compose
should work
There is a subtle difference in network settings when running docker commands directly vs. performing some docker-compose runs. With docker-compose the containers are attached to some user-defined network which is a bridged network
but not with the same configuration as the default bridged network
. The latter one does some special configuration to be backwards compatible to the versions where these networking features didn't exist like today.
I assume that when running from compose, you are missing some dns settings that are available on your host machine and thus you aren't able to resolve the other host on your network. Please have a look at the differences here and here . For a first check, you could compare the output when running cat /etc/resolv.conf
instead of your ping command for both ways and see what you need to add to make it work from compose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.