When the container does not know the host name?

1

I created a container in docker and it tries to access a host called pgmaster. On the machine where docker is installed I added the following host: 10.0.0.3 pgmaster to /etc/hosts . So much when I try to make an ssh works perfectly: ssh vini@pgmaster . But when my container attempts to access the master pg:

psql: could not translate host name "pgmaster" to address: Name or service not known

Yes, my container is trying to access a postgresql in the pgmaster machine. So much so that this command works perfectly on the main machine: psql mydb -h pgmaster -p 5432

Is there a place in the containers that I should name the hosts?

    
asked by anonymous 03.05.2018 / 17:30

1 answer

2

Containers are not aware of hosts that are outside of the networks where the container is. This is your problem, ie the docker host - your machine - knows the pgmaster , but not the container . See in this answer how the networks operation works.

For your problem, when you create the container you can add an entry to the% container as :

docker run -d -p 90:80 --add-host pgmaster:10.0.0.3 nginx:alpine

If you are using compose , the equivalent of the above would be this:

version: '3.6'
services:
  nginx:
    image: nginx:alpine
    container_name: nginx_sopt
    ports:
      - "90:80"
    extra_hosts:
      - "pgmaster:10.0.0.3"

This will add an entry like the one below:

10.0.0.3        pgmaster

The result in /etc/hosts will be something like the below ( /etc/hosts or docker run --rm -it -p 90:80 --add-host pgmaster:10.0.0.3 nginx:alpine cat /etc/hosts , for example):

::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.0.0.3        pgmaster
172.17.0.2      ee6160c61d4d
    
04.05.2018 / 16:13