Docker container communication - “Could not translate host name \”mydbalias\“ to address: Temporary failure in name resolution”

不打扰是莪最后的温柔 提交于 2019-12-13 04:06:57

问题


I have a PostgreSQL container and a Swift server container. I need to pass the DB IP to the Server to start it. So I created an alias for DB in my custom bridge network. Have a look at my docker-compose.yml

version: '3'

services:
    db:
      build: database
      image: postgres
      networks:
        mybridgenet:
          aliases:
            - mydbalias
    web:
      image: mywebserver:latest
      ports: 
        - "8000:8000"
      depends_on: 
        - db
      networks:
        - mybridgenet
      environment:
        WAIT_HOSTS: db:5432

networks:
  mybridgenet:
    driver: bridge

Dockerfile to build webserver.

FROM swift:4.2.1

RUN apt-get update && apt-get install --no-install-recommends -y libpq-dev uuid-dev  && rm -rf /var/lib/apt/lists/*
EXPOSE 8000
WORKDIR /app

COPY client ./client
COPY Package.swift ./
COPY Package.resolved ./

COPY Sources ./Sources
RUN swift build

COPY pkg-swift-deps.sh ./
RUN chmod +x ./pkg-swift-deps.sh
RUN ./pkg-swift-deps.sh ./.build/debug/bridgeOS

FROM busybox:glibc

COPY --from=0 /app/swift_libs.tar.gz /tmp/swift_libs.tar.gz
COPY --from=0 /app/.build/debug/bridgeOS /usr/bin/

RUN tar -xzvf /tmp/swift_libs.tar.gz && \
    rm -rf /tmp/*

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait

CMD /wait && mywebserver db "10.0.2.2"


Database Dockerfile

FROM postgres

COPY init.sql /docker-entrypoint-initdb.d/

The server is started using mybinary mydbalias. Like I said earlier, I pass the alias to start the server. While doing this, I get the following error.

message: "could not translate host name \"mydbalias\" to address: Temporary failure in name resolution\n"

What could be the problem?


UPDATE

After 4 days of a grueling raid, I finally found the rat. He is busybox container. I changed it to ubuntu:16.04 and it's a breeze. Feeling so good about this whole conundrum. Thanks, everyone who helped.


回答1:


Simplify. There is no need in your explicit network declaration (it is done automatically by docker-compose, nor aliases (services get their host names based on service names)

docker-compose.yml

version: '3'
services:
    db:
      build: database
      image: postgres
    web:
      image: mywebserver:latest
      ports: 
        - "8000:8000"
      depends_on: 
        - db
      environment:
        WAIT_HOSTS: db:5432

Then just use db as a hostname to connect to database from web



来源:https://stackoverflow.com/questions/54324599/docker-container-communication-could-not-translate-host-name-mydbalias-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!