1

I would like to run the postgres on a different port from my local machine pointing to 5432 in docker container.

version: '3'
services:
 db:
  image: postgres:alpine
  restart: always
  volumes:
   - ./tmp/db:/var/lib/postgresql/data
  ports:
    - "9000:5432"
 web:
  build: .
  restart: always
  command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
  volumes:
   - .:/myapp
  ports:
   - "3002:3000"
  depends_on:
   - db

and in database.yml I'm pointing the development environment to port 9000

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  host: db
  username: postgres
  database: myapp_development
  port: 9000

test:
  <<: *default
  database: myapp_test

But when I run the rails app in localhost:3002 I'm getting

Could not connect to server: Connection refused Is the server running on host "db" (172.21.0.2) and accepting TCP/IP connections on port 9000?
2
  • Have you tried connecting to the database from your host at port 9000? Does that work? Commented Jan 28, 2020 at 6:11
  • Yes, in my docker compose the db ports is connected from 9000 host port to 5432 default port of postgres. and in my database.yml I change the port of development to 9000 Commented Jan 28, 2020 at 6:35

1 Answer 1

3

you need to set your port to 5432 docker network will use the internal ports:

development:
  <<: *default
  host: db
  username: postgres
  database: myapp_development
  port: 5432
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.