skip to Main Content

in my docker-compose i have such config:

services:
  postgres_db:
    image: postgres:14.1-alpine
    restart: always
    cap_add:
      - SYS_NICE
    volumes:
      - "./setup.sql:/docker-entrypoint-initdb.d/setup.sql"
    ports:
      - "9906:5432"
    environment:
      <<: *common-variables
      POSTGRES_USER: postgres
      POSTGRES_HOST: localhost

from what i understand after running docker-compose up i should get postgres server listening on postgresql://postgres:postgres@localhost:9906 however in the logs what i see is this

hess-postgres_db-1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
chess-postgres_db-1  | 
chess-postgres_db-1  | 2023-04-13 21:31:17.726 UTC [1] LOG:  starting PostgreSQL 14.1 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
chess-postgres_db-1  | 2023-04-13 21:31:17.726 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
chess-postgres_db-1  | 2023-04-13 21:31:17.726 UTC [1] LOG:  listening on IPv6 address "::", port 5432
chess-postgres_db-1  | 2023-04-13 21:31:17.730 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
chess-postgres_db-1  | 2023-04-13 21:31:17.734 UTC [21] LOG:  database system was shut down at 2023-04-13 21:31:12 UTC
chess-postgres_db-1  | 2023-04-13 21:31:17.738 UTC [1] LOG:  database system is ready to accept connections

postgres still listens on 5432 which is the port that my local postgres is also using. when my app calls localhost:9906 absolutely nothing is there
How can i spin up the container and get the db under postgresql://postgres:postgres@localhost:9906 ?

2

Answers


  1. That is correct. It seems for Postgres, that it is running on 5432 but inside of the docker-container.

    With 9906:5432 you setup a rule which redirects all traffic of your docker-host (normally your PC) on port 9906 to the postgres-containers port 5432.
    This will only work, when you try to call your database from your localhost (not from a docker container).

    If you want to change that port, then search for "postgres change port". Here is one result: https://portal.perforce.com/s/article/691

    But … I think you it is not a good idea to change that port. With docker, all is perfect isolated and you can have multiple postgres-instances on the same port. They are isolated.

    I guess your app is also a docker-container. From there you can access your database just with postgresql://postgres_db:5432.
    And you have to remove the POSTGRES_HOST: localhost line. The default-setting is fine.

    Login or Signup to reply.
  2. This depends on your network mode in docker compose.

    ports:
      - "9906:5432"
    
    • this is port mapping, mean for outer network 9906 it is 5432 inside of the postgres_db container

    If your app also in this docker compose then you can directly use postgres_db:5432 like in the example below

    version: "3.8"
    
    services:
      postgres:
        image: ankane/pgvector
        container_name: example-postgres
        ports:
        - 5555:5432
        networks:
          - example_net
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_HOST_AUTH_METHOD=trust
          - POSTGRES_PASSWORD=pswrd
          - POSTGRES_DB=example_bd
        volumes:
          - db:/var/lib/postgresql/data
    
      search_service:
        build: ./services/search_service
        image: example/search_service
        container_name: search_service
        environment:
          - SERVER_HOSTNAME=0.0.0.0
          - SERVER_PORT=3020
          - DB_URL=jdbc:postgresql://example-postgres:5432/example_bd
          - DB_USER=postgres
          - DB_PASSWORD=
        ports:
          - 3020:3020
        networks:
          - example_net
        depends_on:
          - postgres
    
    networks:
      example_net:
        driver: bridge
    volumes:
      db:
        driver: local
    

    But if your app outside of network postgres_db – it can access the db like that

    db {
      url = "jdbc:postgresql://localhost:5555/example_bd"
      user = "postgres"
      password = ""
    }
    

    BTW network mode: host in docker compose will ignore host and port mapping. Postgres will be available from initial postgres port "5432" on docker-compose host

    And still there possible of troubles if your app in some network which can’t access docker network by some reason (like app on windows host, when docker inside of the WSL) then here are exists other workarounds

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search