无法运行rake db:使用docker-compose在Dockerfile中创建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法运行rake db:使用docker-compose在Dockerfile中创建相关的知识,希望对你有一定的参考价值。
我有一个Dockerfile
和docker-compose.yml
就像在tutorial,除了我从一个现有的应用程序开始。
我的docker-compose.yml
看起来像:
db:
image: postgres
ports:
- "5432"
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3030:3030"
links:
- db
和Dockerfile
:
FROM ruby:2.1.4
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
RUN bundle install
ADD . /myapp
# RUN bundle exec rake db:create
# RUN bundle exec rake db:migrate
# RUN bundle exec rake db:seed
和database.yml
development:
adapter: postgresql
encoding: utf8
database: myapp_development
host: db
pool: 5
username: postgres
password:
如你所见,我评论RUN bundle exec rake db:create
,因为我收到一个错误:
could not translate host name "db" to address: Name or service not known
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `initialize'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `new'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `connect'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:324:in `initialize'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
/usr/local/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
/usr/local/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:144:in `rescue in create_database'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:85:in `create_database'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:62:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:62:in `each'
/usr/local/bundle/gems/activerecord-3.2.21/lib/active_record/railties/databases.rake:62:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/usr/local/bundle/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/usr/local/bundle/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/bundle/bin/rake:16:in `load'
/usr/local/bundle/bin/rake:16:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "database"=>"myapp_development", "host"=>"db", "pool"=>5, "username"=>"postgres", "password"=>nil}
相反,我必须docker-compose build
然后docker-compose run web rake db:create
等。
为什么我不能在Dockerfile中进行db-creation / migration。它会非常清楚。我能做到吗?
当您的web
图像构建时(遵循Dockerfile
的说明),它没有连接到db
容器。
Web服务器和数据库映像是独立的,并且在启动它们时链接容器(遵循docker-compose.yml
文件的定义)。
在映像构建期间无法链接到容器,因为它会破坏映像构建必须可重现的原则。同样,在映像构建期间也无法从主机安装卷。
您使用的docker-compose run web rake db:create
命令是初始化数据库的正确方法。
或者,您可以使用docker-compose
正常启动容器,然后使用docker exec
命令在rake db:create
容器中执行web
。
您可能想将/tmp/db
安装到postgres容器的/var/lib/postgresql/data
中。
这样,DB将同步到您的主机,下次重新启动/重建应用程序时,将自动初始化该DB。所以当你设置项目时,必须只运行一次docker-compose run web rake db:create
就像你本地一样,从那时起继续只使用docker-compose run web rake db:create
等。
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data # Here you go!
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
我真的很想知道为什么他们没有将这些内容写入您链接的文档中。我打开一个PR来添加它。
以上是关于无法运行rake db:使用docker-compose在Dockerfile中创建的主要内容,如果未能解决你的问题,请参考以下文章
$ bundle exec rake db:reset 命令提升无法删除 db/development.sqlite3
如何运行 db:migrate 从另一个带参数的 rake 任务?
rake db:migrate 在开发 AWS Beanstalk 中运行