[英]trying to connect to postgres (in a docker container) from Ruby on Rails
I am playing around with Docker and wanted to setup postgreSQL in a docker container and then connect to it from a (non dockerized) Ruby on Rails app. I am playing around with Docker and wanted to setup postgreSQL in a docker container and then connect to it from a (non dockerized) Ruby on Rails app. Here's the docker compose file - I am following this approach here .这是 docker 撰写文件 - 我在这里遵循这种方法。
version: "3"
services:
db:
image: "postgres:13"
container_name: "postgres_dev"
environment:
POSTGRES_PASSWORD: "pgdev2021"
ports:
- "54320:5432"
volumes:
- pgdata://Users/thomas/Documents/Production/PostgreSQL/dbstorage
volumes:
pgdata:
But when I then try to eg run "Rails db:prepare", I get this error:但是当我尝试运行“Rails db:prepare”时,我得到了这个错误:
rails aborted!
ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.54320"?
/Users/thomas/Documents/Websites/rails-frontend/bin/rails:5:in `<top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:10:in `block in <top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:7:in `<top (required)>'
Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.54320"?
/Users/thomas/Documents/Websites/rails-frontend/bin/rails:5:in `<top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:10:in `block in <top (required)>'
/Users/thomas/Documents/Websites/rails-frontend/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:prepare
(See full trace by running task with --trace)
When I check, the container IS running当我检查时,容器正在运行
EDIT: database.yml编辑:database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
port: 54320
user: postgres
development:
<<: *default
database: db_development
test:
<<: *default
database: db_test
production:
<<: *default
database: db_production
username: postgres
password: <%= ENV['MY_DB_PASSWORD'] %>
If I want to start Postgres I always provide a user, password and initial db.如果我想启动 Postgres,我总是提供用户、密码和初始数据库。
Are you trying to do inter-container networking or do you access from the host system?您是在尝试进行容器间网络还是从主机系统访问? If it is intern-container networking, keep in mind by default the hostname of a service equals the service name.如果是内部容器网络,请记住默认情况下服务的主机名等于服务名称。
Example compose file:撰写文件示例:
version: "3.9"
services:
postgres:
image: "postgres:13"
container_name: "postgres_dev"
ports:
- 5432:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: my-database
You can now connect with postgres:5432
from any other service
specified in your docker-compose.yml
.您现在可以从docker-compose.yml
中指定的任何其他service
连接postgres:5432
。 Or connect with http://localhost:5432
from your host machine.或者从您的主机连接http://localhost:5432
。 More about docker compose networking .更多关于docker 组成网络。
Verify by access the container and run something:通过访问容器进行验证并运行一些东西:
docker exec -it postgres_dev bash
psql -h localhost -p 5432 -U root -P password -d my-database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.