dou4121
2018-12-12 04:09
浏览 194

无法获得容器之间的连接

update resolution

The issue was a typo in the docker_compose file. I had the postgres container labeled as postgre. That fixed the network lookup issue. Please advice if I should delete this post.

I have included the code here.. https://github.com/vinceyoumans/achal

PROBLEM: Two containers in a docker compose file. one is standard Postgress container. The second is a goLang Scratch container. The GoLang main.go file panics when the postgres connection fails. you can see the code on github.

The error is:

dial tcp: lookup postgres on 127.0.0.11:53: no such host

REQUEST: look over the main.go and dockercompose file to see what is wrong with this network.. what I am missing.

main.go

db, err := gorm.Open("postgres", "host='postgres' port=5432 user=docker dbname='docker' password='password'")

if err != nil {
    fmt.Println("============   exiting ==========")
    fmt.Println(err)
    panic("failed to connect database e")
    //  the error i get... dial tcp: lookup postgres on 127.0.0.11:53: no such host
    // panic("failed to connect database: " + err)
}

The docker-compose.yml

version: '3.6'
services:
  postgre:
    image: postgres:11.1-alpine
    ports:
      - '5432:5432'
    #network_mode: bridge
    #container_name: postgres

    environment:
      POSTGRES_USER: 'user'
      POSTGRES_PASSWORD: 'password'
      POSTGRESS_DB: 'db_amex01'
    volumes:
      - ./init:/docker-entrypoint-initdb.d/

  todo:
    build: ./go_amex/
    # ports:
    #   - "8000:8080"

# sudo docker-compose -f docker-compose.yml up

#  HELP:  how would I add the goLang service from this point?

图片转代码服务由CSDN问答提供 功能建议

更新分辨率

问题是docker_compose文件中的拼写错误。 我将postgres容器标记为postgre。 这解决了网络查找问题。 请咨询我是否应该删除此帖子。

我已在此处添加了代码。. https://github.com/vinceyoumans/achal

问题:Docker中的两个容器组成文件。 一种是标准的Postgress容器。 第二个是goLang Scratch容器。 当postgres连接失败时,GoLang main.go文件会出现混乱。 您可以在github上查看代码。

错误是:

拨号tcp:在127.0.0.11:53上查找postgres:没有此类主机 < p> 请求: 查看main.go和dockercompose文件,以查看此网络出了什么问题。我所缺少的内容。

main.go
  db,err:= gorm.Open(“ postgres”,“ host ='postgres'port = 5432 user = docker dbname ='docker'password ='password'”  )
 
if err!= nil {
 fmt.Println(“ =============退出==========)
 fmt.Println(err)  
 panic(“无法连接数据库e”)
 //我得到的错误...拨打tcp:在127.0.0.11:53上查找postgres:无此类主机
 // panic(“无法连接数据库:  “ + err)
} 
   
 
 

docker-compose.yml

 版本:'3.6'\  nservices:
 postgre:
图像:postgres:11.1-alpine 
端口:
-'5432:5432'
 #network_mode:bridge 
 #container_name:postgres 
 
环境:
 POSTGRES_USER:' 用户'
 POSTGRES_PASSWORD:'密码'
 POSTGRESS_DB:'db_  amex01'
卷:
-./init:/docker-entrypoint-initdb.d/

待办事项:
构建:./ go_amex / 
#端口:
#-“ 8000:8080”  
 
#sudo docker-compose -f docker-compose.yml up 
 
#帮助:从这一点开始我该如何添加goLang服务?
   
 

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhou7656 2018-12-12 10:19
    已采纳

    You have a typo in your compose file. A DNS alias is automatically configured for the service name, and a default docker network is also created for your project. So all that should be needed is to connect to the service name, which in your case was "postgre" instead of "postgres". To fix that, try this compose file:

    version: '3.6'
    services:
      postgres:
        image: postgres:11.1-alpine
        ports:
          - '5432:5432'
        environment:
          POSTGRES_USER: 'user'
          POSTGRES_PASSWORD: 'password'
          POSTGRESS_DB: 'db_amex01'
        volumes:
          - ./init:/docker-entrypoint-initdb.d/
    
      todo:
        build: ./go_amex/
        # ports:
        #   - "8000:8080"
    

    For more details on compose file networking, see: https://docs.docker.com/compose/networking/

    Note that the next error you will likely see is a connection refused. Compose will start both containers simultaneously, and your application will likely be running before the database finishes starting. To solve that, you will want a retry loop with a short delay between retries, and a timeout or retry limit, in your application code.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongren1977 2018-12-12 04:41

    You're missing the networking configuration. This is required if you want to communicate between containers.

    version: '3.6'
    services:
      postgres: # you were missing the 's'
        image: postgres:11.1-alpine
        ports:
          - '5432:5432'
        networks:
          - mynet
    
        environment:
          POSTGRES_USER: 'user'
          POSTGRES_PASSWORD: 'password'
          POSTGRESS_DB: 'db_amex01'
        volumes:
          - ./init:/docker-entrypoint-initdb.d/
    
      todo:
        build: ./go_amex/
        ports:
          - "8000:8000" # I looked at your main.go file, it's port 8000!
        networks:
          - mynet
    
    networks:
      mynet:
        driver: bridge
    

    Now you can see (via dns resolution) each service from within the containers by using the service name as the hostname.

    评论
    解决 无用
    打赏 举报
  • duanmu1736 2018-12-13 13:34

    The issue was a typo in the docker_compose file. I had the postgres container labeled as postgre. That fixed the network lookup issue. Please advice if I should delete this post.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题