错误-迁移正在进行中。要解决此问题,请运行:bin / rake db:migrate RAILS_ENV = development

时间:2019-03-14 10:57:25

标签: ruby-on-rails docker docker-compose dockerfile

我正在为docker配置Rails现有应用程序,并且在运行localhost:3000时遇到错误。

迁移正在进行中。要解决此问题,请运行:bin / rake db:migrate RAILS_ENV = development

如何在Docker中设置rake db:migrate  docker-compose yml文件

 version: '3'
volumes:  
postgres_data: {} 
services:
 redis:
  image: redis
 command: redis-server 
  ports:
  - "6379:6379"

app:    
 build:      
   context: .      
  dockerfile: /Users/admin/git/generic/deviceapp/docker/app/Dockerfile    
  depends_on:      
   - db  
db:    
image: postgres    
volumes:      
  - postgres_data:/var/lib/postgresql/data  
web:    
build:      
  context: .      
  dockerfile: /Users/admin/git/generic/deviceapp/docker/web/Dockerfile    
depends_on:      
  - app    
ports:      
  - 80:80

数据库yml文件

development:
adapter: postgresql
encoding: unicode
database: Myapp
pool: 5
username: san
password: test@123
host: db

DockerFile

FROM ruby:2.5.0
ENV RAILS_ROOT /Users/admin/git/generic/Myapplication
ENV REDIS_URL=redis://redis:6379/0
RUN mkdir -p $RAILS_ROOT 
# Set working directory
WORKDIR $RAILS_ROOT
# Setting env up
ENV RAILS_ENV='development'
ENV RACK_ENV='development'
# Adding gems
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install
# Adding project files
COPY . .
RUN gem install bundler
RUN bundle install

COPY . .
EXPOSE 3000

CMD ["bundle", "exec", "puma", "-C", "config/puma.rb", "development", "- 
 b", "tcp://0.0.0.0:3000"] 

1 个答案:

答案 0 :(得分:1)

对,对于我来说,我解决了这个问题,告诉docker在docker-compose.yml中执行一些bash命令。喜欢:

在您的docker-compose中:

# your_app_path/docker-compose.yml
...
web:
  build: 
  ...
  command: bash -c "build-scripts/container/web"
...

当docker'ups'时,将执行build-scripts/container/web

您的Dockerfile将在docker-compose build上被调用,您不应在此文件中执行命令。

创建build-scripts/container/web,内容如下:

#!/bin/bash

bundle check || bundle install

bundle exec rake db:setup && \
bundle exec rails s -p 3000 -b '0.0.0.0' -P /tmp/rails.pid

您可以选择将rake db:setup交换为rake db:migrate或类似的内容。