我是docker rails的新手,并按照以下说明尝试在docker容器中实现现有的rails项目:https://hackernoon.com/dockerizing-an-existing-rails-postgresql-app-with-docker-compose-a30a7e1b3f40
docker-compose.yml
@functions
Database.yml
version: '3'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
Dockerfile
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
pool: 5
development:
<<: *default
database: myapp_development
所有容器均已创建并旋转良好。运行docker-compose后,我可以从控制台输出中看到这一点。我什至可以连接到Rails服务器。但是,rails服务器无法连接到postgres服务器:
我的docker配置中是否缺少某些内容,或者我需要更改docker的rails配置吗?
答案 0 :(得分:1)
该错误似乎不是为postgres转发端口,请尝试更改docker-compose以转发端口
我没有大量使用Docker的经验,但是我认为您将发现在重新启动Docker容器时您的数据库没有持久化。我建议为数据库使用外部卷
您的Dockerfile看起来不错
如果有更多经验的人看到此内容,请在评论中确认
docker-compose.yml
version: '3'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres
volumes:
- pg-db:/var/lib/postgresql/data
- ../db:/db
ports:
- "5432:5432"
volumes:
pg-db:
external: true
Database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
pool: 5
development:
<<: *default
database: myapp_development
Dockerfile
FROM ruby:2.5.3
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp