0

WIthin a Docker container, I would like to connect to a MySQL database that resides on the local network. However, I get errors because it can not find the host name, so my current hot fix is to hardcode the IP (which is bound to change at some time).

Hence; is it possible to forward a hostname from the host machine to the Docker container at docker run?

casparjespersen
  • 3,460
  • 5
  • 38
  • 63

3 Answers3

1

Yes, it is possible. Just inject hostname variable when run docker run command:

$ hostname
np-laptop

$ docker run -ti -e HOSTNAME=$(hostname) alpine:3.7
/ # env
HOSTNAME=np-laptop
SHLVL=1
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

Update:

I think you can do two things with docker run for your particular case:

1. Bind /etc/hosts file from the host to a container.

2. Define any dns server you want inside a container with --dns flag.

So, finally the command is:

docker run -ti -v /etc/hosts:/etc/hosts --dns=<IP_of_DNS> alpine:3.7
nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • That is not exactly what I'm after. My host machine (let's call it `host`) is on a network with another machine (let's call it `remote`). Now. I'd like to access `remote` through a docker container on `host`, but the "hostname" `remote` is not visible to the docker container. – casparjespersen Apr 06 '18 at 12:33
  • This would actually be possible: `docker run -e REMOTE=$(getent hosts remote_hostname | awk '{ print $1 }') -d hello-world` Then I would be able to use the IP within the Docker container. – casparjespersen Apr 06 '18 at 12:34
  • But it would be easier to.. kind of.. forward all of the DNS setups from host to container. – casparjespersen Apr 06 '18 at 12:35
  • 1
    @c.jespersen added some info to the answer. I think it could be helpful for you. – nickgryg Apr 06 '18 at 12:45
1

Docker containers by default has access to the host network, and they're able to resolve DNS names using DNS servers configured on the host, so it should work out of the box.

I remember having similar problem in my corporate network, I solved it by referencing in the app the remote server with FQDN - our-database.mycompany.com instad just using our-database.

Hope this helps.

Miq
  • 3,931
  • 2
  • 18
  • 32
  • No this did not solve it. However, I just realized that spinning up a container using docker-compose, it is able to find it, but when using docker run it is not. – casparjespersen Apr 16 '18 at 11:19
  • Odd... Perhaps the problem is not the name, but collision between docker networks' IP addresses and your company's networks. Did you checked it? You could also try attaching to the running container (`docker exec -it container_name /bin/bash`) and executing `nslookup` to see, which DNS is trying to resolve the hostname and to what address. – Miq Apr 16 '18 at 11:48
0

People has asked similar questions and got good answers:

How do I pass environment variables to Docker containers?

Alternatively you can configure the DHCP/DNS server that serves the docker machines to resolve the hostnames properly. DDNS is another option that can simplify configuration as well.

eniacz
  • 127
  • 3