Postgres在Docker Compose中拒绝来自我的服务器的连接。

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

Postgres inside docker compose refusing connection from my server

问题

我有一个运行在Docker Compose中的Golang服务器和Postgres实例。由于某种原因,Postgres拒绝连接。根据我之前的搜索,通常问题是拼写错误、未暴露端口、使用了SSL等等,但是我没有遇到这些问题,仍然存在这个问题。

这是我遇到的错误:

auth-service    | Retrying connection to database...
auth-service    | failed to connect to `host=auth-db user=postgres database=auth-dev`: dial error (dial tcp 172.23.0.3:5435: connect: connection refused)

这是我用于连接数据库的Golang代码(使用pgx):

dbUrl := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
		os.Getenv("POSTGRES_USER"),
		os.Getenv("POSTGRES_PASSWORD"),
		os.Getenv("POSTGRES_HOST"),
		os.Getenv("POSTGRES_PORT"),
		os.Getenv("POSTGRES_DB"))

这就是我感到困惑的原因:

1)端口是匹配的,我将5435端口从Postgres暴露出来,并且我连接的也是5435端口。
2)主机应该是正确的,因为我引用了auth-db服务名称,它们都在同一个默认网络上,所以应该没问题。
3)密码和用户名是匹配的。
4)POSTGRES_DB也是匹配的,默认数据库应该是auth-dev。

POSTGRES_DB
这个可选的环境变量可以用来定义在首次启动镜像时创建的默认数据库的名称。如果没有指定,那么将使用POSTGRES_USER的值。

5)我也禁用了sslmode。

还有其他什么原因会导致连接被拒绝吗?

尝试将数据库更改为template1和postgres,因为它们是默认创建的,但都不起作用。

54511e50369c   postgres:14.1-alpine           "docker-entrypoint.s…"   16 minutes ago   Up 16 seconds   0.0.0.0:5435->5432/tcp, :::5435->5432/tcp                                                                                                             auth-db


docker exec -it 54511e50369c psql -U postgres
psql (14.1)
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

当我尝试连接时,数据库是可用的(我重试了20次,并在服务崩溃时重新启动,所以它应该是可用的)。

英文:

I have a Golang server alongside Postgres instance running inside docker compose. For some reason the Postgres is refusing connection. From all of my previous searches, usually the problem is typo, not exposing the port, having SSL and so on, but I don't have anything like that going on and still having this issue

version: "3.2"
services:
  ingress:
    image: jwilder/nginx-proxy
    ports:
      - "3000:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
  auth-service:
    depends_on:
      - rabbitmq
      - auth-db
      - ingress
    build: ./auth
    container_name: "auth-service"
    ports:
      - 3001:3000
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_HOST=auth-db
      - POSTGRES_DB=auth-dev
      - POSTGRES_PORT=5435
      - PORT=3000
      - RABBITMQ_USER=guest
      - RABBITMQ_PASSWORD=guest
      - RABBITMQ_HOST=rabbitmq
      - RABBITMQ_PORT=5672
      - VIRTUAL_HOST=api.twitchy.dev
      - VIRTUAL_PATH=/v1/auth/
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    # networks:
    #   - rabbitmq_net
    #   - default
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: "rabbitmq"
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq/
      - rabbitmq_log:/var/log/rabbitmq/
    # networks:
    #   - rabbitmq_net
  auth-db:
    image: postgres:14.1-alpine
    restart: always
    container_name: "auth-db"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=auth-dev
    ports:
      - "5435:5432"
    volumes:
      - db:/var/lib/postgresql/data
  chat-db:
    image: postgres:14.1-alpine
    restart: always
    container_name: "chat-db"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=chat-dev
    ports:
      - "5434:5432"
    volumes:
      - db:/var/lib/postgresql/data

# networks:
#   rabbitmq_net:
#     driver: bridge

volumes:
  db:
    driver: local
  rabbitmq_data:
  rabbitmq_log:

This is the error I am getting

auth-service    | Retrying connection to database...
auth-service    | failed to connect to `host=auth-db user=postgres database=auth-dev`: dial error (dial tcp 172.23.0.3:5435: connect: connection refused)

And my Golang code used to connect to the DB (using pgx)

	dbUrl := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
		os.Getenv("POSTGRES_USER"),
		os.Getenv("POSTGRES_PASSWORD"),
		os.Getenv("POSTGRES_HOST"),
		os.Getenv("POSTGRES_PORT"),
		os.Getenv("POSTGRES_DB"))

This is why I am confused

  1. The ports match up, I expose 5435 from postgres, and I connect to 5435
  2. The host should be correct as I am referencing the auth-db service name and they are on the same default network so that should be fine
  3. The password and username match up
  4. The POSTGRES_DB also match up, the default database should be auth-dev
POSTGRES_DB
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.
  1. I have sslmode disable as well

Is there anything else that can cause the connection to be refused?

Tried changing db to template1 and postgres as they are created by default but both aren't working either

54511e50369c   postgres:14.1-alpine           "docker-entrypoint.s…"   16 minutes ago   Up 16 seconds   0.0.0.0:5435->5432/tcp, :::5435->5432/tcp                                                                                                             auth-db


docker exec -it 54511e50369c psql -U postgres
psql (14.1)
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

The database is ready when I am trying to connect (I am retrying 20 times so, and restarting the service if it crashes, so it should be available)

答案1

得分: 2

当你在docker-compose中映射端口时,比如"5435:5432",你实际上是将主机上的端口5435映射到容器上的端口5432。然而,在auth-service定义中,你的数据库URL使用的是服务的名称auth-db,所以你实际上是直接连接到数据库容器,而不是通过主机。因为数据库容器没有暴露端口5435,所以你无法使用端口5435进行连接。
如果你尝试从主机上连接数据库,你可能会成功地使用端口5435localhost

英文:

When you map ports in docker-compose, say like "5435:5432", you are mapping port 5435 on the HOST machine to 5432 on the CONTAINER. However, your db url in the auth-service definition is using the name of the service, auth-db, so you are actually hitting the db container directly, not going through the host machine. Because the db container does not expose 5435, you are unable to connect using port 5435.
If you were to try to connect to the database from your host machine for example, you would probably be successful using port 5435 and localhost.

huangapple
  • 本文由 发表于 2022年4月24日 01:20:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/71981986.html
匿名

发表评论

匿名网友

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

确定