简体   繁体   English

在 Docker Rails 中更改 Postgres 端口

[英]Changing Postgres Port in Docker Rails

I would like to run the postgres on a different port from my local machine pointing to 5432 in docker container.我想在本地机器的不同端口上运行 postgres,指向 docker 容器中的 5432。

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 9000database.yml我将开发环境指向端口 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但是当我在localhost:3002运行 rails 应用程序时,我得到了

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?

you need to set your port to 5432 docker network will use the internal ports:您需要将端口设置为5432 docker network 将使用内部端口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM