When the container does not know the host name?

Asked

Viewed 109 times

1

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

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

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

Is there any place in the containers I should set the names of the hosts?

1 answer

2


The containers have no knowledge of hosts which are outside the networks in which the container is. This is your problem, namely the Docker host - your machine - know the pgmaster, but not the container. See in this answer how is the functioning of Networks

For your problem, when creating the container you can add an entry to /etc/hosts of container, as below:

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

If you are using commie, 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 (docker run --rm -it -p 90:80 --add-host pgmaster:10.0.0.3 nginx:alpine cat /etc/hosts or docker-compose exec nginx 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.