Docker容器通信-“无法翻译主机名\“ mydbalias\”解决:名称解析暂时失败 [英] Docker container communication - "Could not translate host name \"mydbalias\" to address: Temporary failure in name resolution"
问题描述
我有一个PostgreSQL容器和一个Swift服务器容器。我需要将DB IP传递给服务器以启动它。因此,我在自定义网桥网络中为数据库创建了别名。看看我的 docker-compose.yml
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来构建网络服务器。
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"
数据库Dockerfile
Database Dockerfile
FROM postgres
COPY init.sql /docker-entrypoint-initdb.d/
服务器使用 mybinary mydbalias
启动。就像我之前说的,我通过别名来启动服务器。这样做时,出现以下错误。
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.
消息:无法将主机名\ mydbalias\转换为地址:名称解析\n暂时失败
可能是什么问题?
更新
之后经过4天的艰苦突袭,我终于找到了老鼠。他是 busybox
容器。我将其更改为 ubuntu:16.04
,这很容易。对整个难题感到非常高兴。谢谢大家的帮助。
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.
推荐答案
简化。不需要在您的显式网络声明(它由 docker-compose
自动完成,也无需别名(服务根据服务名称获取其主机名)
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
然后只需使用 db
作为主机名即可从 web $ c连接到数据库$ c>
Then just use db
as a hostname to connect to database from web
这篇关于Docker容器通信-“无法翻译主机名\“ mydbalias\”解决:名称解析暂时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!