且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Docker容器中的服务器无法连接到另一个Docker容器中的Postgres数据库

更新时间:2022-11-30 08:37:48

之所以不起作用,是因为数据库实际上是在默认的postgres端口5432上启动的.在docker-compose.yml中可以找到需要添加命令以更改默认端口.

The reason this wasn't working is because the DB was actually being started on the default postgres port which is 5432. Turns out in docker-compose.yml you need to add a command to change the default port.

pg-development:
    image: postgres
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: helloworld
      POSTGRES_DB: test_dev
    ports:
      - "3308:3306"
    volumes:
      - dbdata:/data/db
    networks:
      - dev-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U root -d test_dev"]
      interval: 10s
      timeout: 2s
      retries: 10
    command: -p 3306 // this fixes the issue