在Docker上连接Rails和PostgreSQL,你可以按照以下步骤进行操作:
version: '3'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_USER: your_postgres_username
POSTGRES_PASSWORD: your_postgres_password
请将your_postgres_username
和your_postgres_password
替换为你自己的PostgreSQL用户名和密码。
docker-compose up --build
这将构建并启动包含Rails应用程序和PostgreSQL数据库的Docker容器。
config/database.yml
文件中配置数据库连接。将以下内容添加到database.yml
文件中:default: &default
adapter: postgresql
encoding: unicode
host: db
username: your_postgres_username
password: your_postgres_password
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: your_app_name_development
test:
<<: *default
database: your_app_name_test
请将your_postgres_username
和your_postgres_password
替换为你在docker-compose.yml文件中设置的PostgreSQL用户名和密码。将your_app_name
替换为你的应用程序名称。
docker-compose run web bundle exec rails db:create
docker-compose run web bundle exec rails db:migrate
这将在PostgreSQL数据库中创建和迁移你的Rails应用程序的数据库。
现在,你可以通过访问http://localhost:3000
来访问你的Rails应用程序,并且它将连接到Docker容器中的PostgreSQL数据库。
推荐的腾讯云相关产品:腾讯云容器服务(Tencent Kubernetes Engine,TKE),它是一种高度可扩展的容器管理服务,可帮助您轻松运行和管理Docker容器化应用程序。了解更多信息,请访问TKE产品介绍。
领取专属 10元无门槛券
手把手带您无忧上云