使用docker for rails运行数据库迁移时出错

时间:2018-11-26 05:19:52

标签: ruby-on-rails macos docker

我正在尝试根据教程为Vue.js和Rails API建立开发环境。

最终我在尝试运行以下命令时遇到障碍:

docker-compose run backend rails db:create

这是错误:

$ docker-compose run backend rails db:create
Starting am-full-stack_db_1_b7f6ee37d2e4 ... done
Error response from daemon: OCI runtime create failed: 
container_linux.go:348: starting container process caused "exec: 
\"rails\": executable file not found in $PATH": unknown

这是我的文件树

  

应用

     
    

autheg-backend

         

autheg-frontend

         

docker-compose.yml

  

这是我的docker-compose.yml

version: '3'
services:
  db:
    image: postgres
    ports:
      - "5432"
  backend:
    build:
      context: autheg-backend
      args:
        UID: ${UID:-1001}
    volumes:
      - ./autheg-backend:/usr/src/app
    ports:
      - "8080:8080"
    depends_on:
      - db
    user: rails
  frontend:
    build:
      context: autheg-frontend
      args:
        UID: ${UID:-1001}
    volumes:
      - ./autheg-frontend:/usr/src/app
    ports:
      - "3000:3000"
    user: frontend

“ docker-compose run back env”的结果

PATH=/usr/local/bundle/bin:/usr/local/bundle/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=c88e0c72c584
TERM=xterm
RUBY_MAJOR=2.5
RUBY_VERSION=2.5.3
RUBY_DOWNLOAD_SHA256=1cc9d0359a8ea35fc6111ec830d12e60168f3b9b305a3c2578357d360fcf306f
RUBYGEMS_VERSION=2.7.8
BUNDLER_VERSION=1.17.1
GEM_HOME=/usr/local/bundle
BUNDLE_PATH=/usr/local/bundle
BUNDLE_SILENCE_ROOT_WARNING=1
BUNDLE_APP_CONFIG=/usr/local/bundle
APP=/usr/src/app
HOME=/home/rails

谢谢!

3 个答案:

答案 0 :(得分:0)

这是由于未在Docker容器中设置路径,而是在您的开发/本地机器上设置了路径引起的。使用以下命令:

docker-compose run backend bundle exec rails db:create

答案 1 :(得分:0)

您支持的用于rails开发的dockerfile如下所示:-

# Base image:
FROM ruby:2.5

# Install dependencies
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

# create application directory
RUN mkdir /myapp

# Set our working directory inside the image
WORKDIR /myapp

# Setting env up
ENV RAILS_ENV='development'
ENV RACK_ENV='development'

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

EXPOSE 3000

CMD [ "bundle", "exec", "puma", "-C", "config/puma.rb" ]

答案 2 :(得分:0)

使用数据库进行迁移:

docker-compose run backend bin/rails db:create
# or
docker-compose run backend bundle exec rails db:create

/ backend / Dockerfile

FROM ruby:2.5

ARG UID
RUN adduser rails --uid $UID --disabled-password --gecos ""

ENV APP /usr/src/app
RUN mkdir $APP
WORKDIR $APP

COPY Gemfile* $APP/
RUN bundle install -j3 --path vendor/bundle

COPY . $APP/

# Setting env up
ENV RAILS_ENV='development'
ENV RACK_ENV='development'

CMD [ "bundle", "exec", "rails", "server", "-p", "8080", "-b", "0.0.0.0"]

./ docker-compose.yml

version: '3'
services:
  db:
    image: postgres
    ports:
      - "5432"
  backend:
    build:
      context: autheg-backend
      args:
        UID: ${UID:-1001}
    command: bundle exec rails s -p 8080 -b '0.0.0.0'
    volumes:
      - ./autheg-backend:/usr/src/app
    ports:
      - "8080:8080"
    depends_on:
      - db
    user: rails
  frontend:
    build:
      context: autheg-frontend
      args:
        UID: ${UID:-1001}
    volumes:
      - ./autheg-frontend:/usr/src/app
    ports:
      - "3000:3000"
    user: frontend

然后

docker-compose up

现在,您应该可以使用以下方法查看rails网站: 本地主机:8080(而不是3000)

希望可以帮助您入门