无法连接到位于 Docker 容器内部的 PostgreSQL。

huangapple go评论100阅读模式
英文:

Can not connect to postgresql which is inside of a docker container

问题

我正在编写一个使用两个容器(db和app)进行Docker化的Go应用程序。

在启动容器时,执行'docker-compose up'命令时,我看到以下消息:dial tcp: lookup dbpgsql on 127.0.0.11:53: no such host

以下是我的docker-compose.yml文件内容:

version: '2'
services:
  server:
    hostname: app
    image: golang:1.7.3-alpine
    build: ./server/
    privileged: true
    container_name: server
    command: go run server.go
    volumes:
      - ../src/:/go/src/
      - ../server.go:/go/server.go
    links:
      - db:db
    ports:
      - '8080:8080'
    env_file: environment

  db:
    hostname: dbpgsql
    image: postgres:latest
    container_name: db
    environment:
      POSTGRES_USER: cldb
      POSTGRES_PASSWORD: cldb
      POSTGRES_DB: cldb
    ports:
      - '5432:5432'
    volumes:
      - ./data:/docker-entrypoint-initdb.d

当执行DB.Ping()时,应用程序在此处失败:

func InitDB(dataSourceName string) {
    var err error
    DB, err = sql.Open("postgres", dataSourceName)
    if err != nil {
        log.Panic(err)
    }

    if err = DB.Ping(); err != nil {
        log.Panic(err)
    }

    DB.SetMaxIdleConns(100)
}

希望这能帮到你!

英文:

I'm writing a go app which is dockerized in 2 containers: db and app.

while starting containers 'docker-compose up' I see the message: dial tcp: lookup dbpgsql on 127.0.0.11:53: no such host

DB_ENV_DB=cldb
DB_ENV_USER=cldb
DB_ENV_PASS=cldb
DB_PORT_5432_TCP_ADDR=dbpgsql
DB_PORT_5432_TCP_PORT=5432

here is my docker-compose.yml

version: '2'
services:
  server:
    hostname: app
    image: golang:1.7.3-alpine
    build: ./server/
    privileged: true
    container_name: server
    command: go run server.go
    volumes:
      - ../src/:/go/src/
      - ../server.go:/go/server.go
    links:
      - db:db
    ports:
     - '8080:8080'
   env_file: environment

 db:
   hostname: dbpgsql
   image: postgres:latest
   container_name: db
   environment:
      POSTGRES_USER: cldb
      POSTGRES_PASSWORD: cldb
      POSTGRES_DB: cldb
    ports:
      - '5432:5432'
   volumes:
     - ./data:/docker-entrypoint-initdb.d

The app fails here when DB.Ping() is executed:

func InitDB(dataSourceName string) {
    var err error
 	DB, err = sql.Open("postgres", dataSourceName)
    if err != nil {
	   log.Panic(err)
 	}

    if err = DB.Ping(); err != nil {
	   log.Panic(err)
 	}

    DB.SetMaxIdleConns(100)
}

答案1

得分: 4

你可以使用服务名称(或在链接选项中给出的别名)连接到数据库,对于你的情况,它是 db。主机名选项设置容器自身所知的主机名。但是它不会出现在docker ps命令的输出中,也不会出现在任何其他容器的/etc/hosts文件中。(https://docs.docker.com/engine/userguide/networking/default_network/configure-dns/)

英文:

You can connect to database with the service name (or alias name given in links options), in your case it's db. Hostname option sets the hostname by which the container knows itself. But it will not appear in docker ps nor in the /etc/hosts file of any other container. (https://docs.docker.com/engine/userguide/networking/default_network/configure-dns/)

答案2

得分: 0

在我的情况下,我在我的PostgreSQL服务中使用了container_name,这就是在应用程序连接到数据库时需要设置为主机名的内容。

英文:

In my case, I was using container_name in my post-gres service and thats what was required to be set as hostname while connecting to db in application

huangapple
  • 本文由 发表于 2016年12月19日 22:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/41224582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定