如何将 Docker Web 应用程序容器连接到 Docker PostgreSQL 容器? [英] How to connect Docker web app container to Docker PostgreSQL container?

查看:19
本文介绍了如何将 Docker Web 应用程序容器连接到 Docker PostgreSQL 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm practicing making a Golang web app that interacts with a PostgreSQL database, each running on their own container.

我正在使用 docker-compose up

但我似乎未能正确设置 postgres 容器.

But I seem to be failing on getting the postgres container properly set up.

为简洁起见,Dockerfiles 和其他设置文件的链接位于此要点上(如果你想要它,请告诉我).

For brevity, links to Dockerfiles and other settings files are on this gist (let me know if you want it here instead).

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 工作正常.但是当应用程序尝试打开一个数据库连接时:

docker-compose up works fine. But when the application tries to open a database connection with:

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

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

我从 docker compose 收到以下错误:

I get the following error from docker compose:

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

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

What can I do to make both containers talk to each other?

提前致谢.

推荐答案

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

When using the docker-compose v2, it's not needed to create links between services. Docker 1.9 and 1.10 allows you to connect to other containers on the same (custom) network through their name.

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

You should be able to connect using either the name of the service or the name of the container as a hostname. Given that the name of the container is generated by docker-compose, this is not really convenient to use, so for that reason, docker-compose also adds an alias with the service name to each container.

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

Take this very simple example. I've used an Nginx container for convenience, but the same should apply to your situation;

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容器内pingdb"服务:

Then ping the "db" service from inside the test_web_app_1 container:

$ 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"别名;

If you inspect the test_db_1 container, you can see that docker-compose automatically added a "db" alias for the test_db_1 container;

$ docker inspect test_db_1

给出:(只是NetworkSettings.Networks部分)

"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"
    }
}

这篇关于如何将 Docker Web 应用程序容器连接到 Docker PostgreSQL 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆