ruby-on-rails - 无法运行 rake db :create in Dockerfile

我有一个 Dockerfiledocker-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 then docker-compose run web rake db:create 等等。

为什么我不能在 Dockerfile 中创建 db-creation/migration。会清楚很多。我能做到吗?

最佳答案

当您的 web 镜像构建时(按照 Dockerfile 的说明),它没有与 db 容器的连接.

网络服务器和数据库图像是独立的容器在启动时链接(遵循 docker-compose.yml 的定义> 文件)。

您不能在镜像构建期间链接到容器,因为它会破坏镜像构建必须是可重现的原则。同样,您也不能在镜像构建期间从主机装载卷。

您使用的 docker-compose run web rake db:create 命令是初始化数据库的正确方法。

或者,您可以使用 docker-compose 正常启动容器,然后使用 docker exec 命令执行 rake db:createweb 容器中。

关于ruby-on-rails - 无法运行 rake db :create in Dockerfile with docker-compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535979/

相关文章:

docker - 拉取访问被拒绝的存储库不存在或可能需要 docker login

docker - 根据子域将流量路由到 docker 容器

docker - Kubernetes多节点上的Kafka

docker - 停止的 Docker 容器的资源使用情况

docker - Jenkins Docker 管道退出代码 -1

docker - 如何将文件作为 docker 命令的参数发送?

postgresql - Docker 错误 : standard_init_linux. go:1

docker - "Empty continuation lines will become err

macos - docker 的 `net=host` 设置如何工作,我该如何做类似的事情,例如虚拟

docker - 如何理解 "/bin/true"命令在 "docker run ..."命令中的作