2017-12-29 22 views
1

Ich versuche, mit Docker Compose zwei Container zu erstellen: einen, der PostgreSQL (eigentlich PostGIS) enthält, und einen anderen, der eine Webanwendung enthält, die PostgreSQL abfragt.PostgreSQL-Client kann Hostnamen im Docker-Container nicht auflösen

Mein Docker-compose.yaml ist:

version: '3.1' 

services: 
    db: 
    build: 
     context: . 
     dockerfile: Dockerfile-db 
    restart: always 
    ports: 
     - 5432:5432 
    web: 
    build: 
     context: . 
     dockerfile: Dockerfile-web 
     args: 
     PGHOSTADDR: db 
    restart: always 
    ports: 
     - 80:80 
    depends_on: 
     - db 

Die Anwendung Dockerfile ist:

FROM php:apache 
ARG PGHOSTADDR 
ENV PGHOSTADDR=$PGHOSTADDR 
COPY . /var/www/html/ 
RUN apt-get update && mkdir -p /usr/share/man/man1 /usr/share/man/man7 && apt-get install -y postgresql-client strace ltrace iputils-ping bind9-host dnsutils 
CMD until psql -c '\q' ; do sleep 1 ; done ; /usr/sbin/apache2 

Die PostgreSQL Dockerfile ist einfach:

FROM mdillon/postgis 

Die Container startet PostgreSQL und erscheint um perfekt zu arbeiten - ich kann es vom Gastgeber abfragen, und ich kann es von der Anwendung conta abfragen Solange ich die IP-Adresse des PostgreSQL-Containers verwende.

jedoch 'psql' im App-Container ausgeführt wird, nicht in der Lage PostgreSQL zu erreichen:

$ docker-compose up -d && echo MARK && docker-compose logs --follow web 
WARNING: The Docker Engine you're using is running in swarm mode. 

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. 

To deploy your application across the swarm, use `docker stack deploy`. 

Starting myapp_db_1 ... 
Starting myapp_db_1 ... done 
Starting myapp_web_1 ... 
Starting myapp_web_1 ... done 
MARK 
Attaching to myapp_web_1 
web_1 | psql: could not translate host name "db" to address: Name or service not known 
web_1 | psql: could not translate host name "db" to address: Name or service not known 
web_1 | psql: could not translate host name "db" to address: Name or service not known 
web_1 | psql: could not translate host name "db" to address: Name or service not known 

Normalerweise 'psql' gibt diese Diagnose, wenn es nicht gegeben den Hostnamen aufzulösen. Allerdings scheint Auflösung von Hostnamen in Ordnung in dem App Container zu arbeiten:

$ docker exec -it myapp_web_1 bash 
[email protected]:/var/www/html# dig db 

; <<>> DiG 9.10.3-P4-Debian <<>> db 
;; global options: +cmd 
;; Got answer: 
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21082 
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 

;; QUESTION SECTION: 
;db.    IN A 

;; ANSWER SECTION: 
db.   600 IN A 172.22.0.2 

;; Query time: 0 msec 
;; SERVER: 127.0.0.11#53(127.0.0.11) 
;; WHEN: Fri Dec 29 04:18:32 UTC 2017 
;; MSG SIZE rcvd: 38 

[email protected]:/var/www/html# ping -c 2 db 
PING db (172.22.0.2) 56(84) bytes of data. 
64 bytes from myapp_db_1.myapp_default (172.22.0.2): icmp_seq=1 ttl=64 time=0.036 ms 
64 bytes from myapp_db_1.myapp_default (172.22.0.2): icmp_seq=2 ttl=64 time=0.045 ms 

--- db ping statistics --- 
2 packets transmitted, 2 received, 0% packet loss, time 1020ms 
rtt min/avg/max/mdev = 0.036/0.040/0.045/0.007 ms 
[email protected]:/var/www/html# 

Auch der PostgreSQL-Client und Server erscheinen perfekt abgesehen von diesem Host-Problem zu arbeiten:

[email protected]:/var/www/html# unset PGHOSTADDR 
[email protected]:/var/www/html# psql -h 172.22.0.2 -U postgres -c 'SELECT 1;' 
?column? 
---------- 
     1 
(1 row) 

[email protected]:/var/www/html# 

Warum isn‘ t 'psql' Auflösung von 'db' zu 172.22.0.2, wie 'dig' tun kann und damit funktioniert?

Antwort

1

Ich habe PGHOSTADDR, die eine IP-Adresse mit geteilter Dezimalstelle sein muss, gesetzt, während ich PGHOST hätte einstellen sollen, was ein Hostname sein kann, wie in the PostgreSQL documentation klar erklärt wird.

Entschärfen PGHOSTADDR und stattdessen Einstellung PGHOST macht 'psql' wie erwartet:

[email protected]:/var/www/html# psql -U postgres -c 'SELECT 1;' 
psql: could not translate host name "db" to address: Name or service not known 
[email protected]:/var/www/html# unset PGHOSTADDR 
[email protected]:/var/www/html# PGHOST=db 
[email protected]:/var/www/html# export PGHOST 
[email protected]:/var/www/html# psql -U postgres -c 'SELECT 1;' 
?column? 
---------- 
     1 
(1 row) 

[email protected]:/var/www/html# 

Ein weiterer Fehlschlag RTFM.

+0

Ich finde es unnötig verwirrend und redundant, sowohl PGHOST als auch PGHOSTADDR zu haben. Ich sollte einen Patch senden, damit beide entweder einen Hostnamen oder eine IP-Adresse akzeptieren. – AnotherSmellyGeek

Verwandte Themen