Docker Compose主机名DNS查找失败

时间:2019-06-27 12:24:12

标签: docker networking docker-compose localhost

我有一个简单的Docker撰写文件,如下所示:

version: '3'
services:
  server:
     build: ./server
     networks:
        - common
     ports:
        - "4840:4840"
     container_name: open62541_server
     hostname: open62541-server
  client:
    build: ./client
    depends_on:
       - server
    container_name: open62541_client
    networks:
        - common

networks:
    common:

运行它时,出现以下错误:

Attaching to open62541_server, open62541_client
open62541_server | [2019-06-27 12:19:54.864 (UTC+0000)] info/network    TCP network layer listening on opc.tcp://open62541-server:4840/
open62541_client | [2019-06-27 12:19:55.590 (UTC+0000)] warn/network    DNS lookup of open62541-server failed with error Name or service not known
open62541_client exited with code 0

即使指定了容器属于公共网络,为什么仍会收到此错误?我有什么想念的吗?

2 个答案:

答案 0 :(得分:1)

您应该使用server(服务名称)作为在docker-compose网络内连接的主机,而不是open62541-server,因为docker文件中的container_name和hostname部分都不会影响docker-compose的内部DNS。 / p>

答案 1 :(得分:1)

您可能在配置文件中有错字。 msg1 db 'enter 4 characters' hostname,而open62541-servercontainer_name(带下划线)。您应该能够使用服务名称(即open62541_server)或container_name(即server)连接到服务器