【问题标题】:How to connect Docker web app container to Docker PostgreSQL container?如何将 Docker Web 应用程序容器连接到 Docker PostgreSQL 容器?
【发布时间】:2016-07-14 14:29:19
【问题描述】:

我正在练习制作一个与 PostgreSQL 数据库交互的 Golang Web 应用程序,每个应用程序都在自己的容器上运行。

我正在使用docker-compose up 运行容器

但我似乎无法正确设置 postgres 容器。

为简洁起见,指向Dockerfiles 和其他设置文件的链接为on this gist(如果您需要,请告诉我)。

version: '2'
services:
  web_app:
    build: dockerfiles/web_app
    ports:
      - "9000:9000"
    volumes:
      - .:/go/src/gitlab.com/repo/web_app
    # links might be replaced by depends_on.
    # links:
    #   - db
    depends_on:
      - db
    # tty and stdin_open cause docker-compose to disconnect from docker-machine after 60sec.
    # A fix is on the way.
    # tty: true
    # stdin_open: true
  db:
    build: dockerfiles/db
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data: {}

docker-compose up 工作正常。但是当应用程序尝试打开数据库连接时:

var pgConf string = "user=web_app dbname=web_app sslmode=verify-full password=password"

db, err := sql.Open("postgres", pgConf)

我从 docker compose 收到以下错误:

Error creating new user:  dial tcp [::1]:5432: getsockopt: connection refused

如何让两个容器相互通信?

提前谢谢你。

【问题讨论】:

  • 原因是depends_on不能代替links。前者只决定启动的顺序,而后者也允许容器之间通过名称访问。

标签: postgresql go docker docker-compose


【解决方案1】:

使用 docker-compose v2 时,不需要在服务之间创建链接。 Docker 1.9 和 1.10 允许您通过它们的名称连接到同一(自定义)网络上的其他容器。

您应该能够使用 service 的名称或 container 的名称作为主机名进行连接。鉴于容器的名称是由 docker-compose 生成的,这样使用起来并不是很方便,因此 docker-compose 也为每个容器添加了一个带有服务名称的 alias

以这个非常简单的例子为例。为方便起见,我使用了 Nginx 容器,但同样适用于您的情况;

version: '2'
services:
  web_app:
    image: nginx
  db:
    image: nginx

首先启动项目(假设;

$ docker-compose --project-name=test up -d
Creating network "test_default" with the default driver
Creating test_db_1
Creating test_web_app_1

然后从 test_web_app_1 容器内 ping "db" 服务:

$ docker exec -it test_web_app_1 ping -c 2 db
PING db (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.108 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.243 ms
--- db ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.108/0.175/0.243/0.068 ms

如果您检查test_db_1 容器,您可以看到docker-compose 自动为test_db_1 容器添加了“db”别名;

$ docker inspect test_db_1

给出:(只是NetworkSettings.Networkspart)

"Networks": {
    "test_default": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": [
            "db",
            "002b1875e61f"
        ],
        "NetworkID": "0f9e2cddeca79e5a46c08294ed61dee273828607f99014f6410bda887626be70",
        "EndpointID": "a941ab95586a8fdafc5075f9c5c44d745f974e5790ef6048b9e90115a22fb31f",
        "Gateway": "172.18.0.1",
        "IPAddress": "172.18.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:12:00:02"
    }
}

【讨论】:

  • 感谢您提供的惊人见解。根据您的回答,我将user.go 第 20 行的连接代码更改为:var pgConf string = "user=web_app dbname=web_app sslmode=verify-full password=password host=172.18.0.2" and I got a successful connection. So in the end, docker-compose.yaml` 很好。我所需要的只是你给我的关于连接容器的内部 IP 的额外信息。
  • 你应该可以把它改成host=db,这就是Docker中嵌入DNS的想法,也是一种更好的方法,因为IP地址可以改变
猜你喜欢
  • 2022-11-05
  • 1970-01-01
  • 2019-04-14
  • 2017-11-10
  • 2022-11-01
  • 2019-05-24
  • 2019-01-25
  • 2022-01-12
相关资源
最近更新 更多