简体   繁体   中英

error running db migrate with docker for rails

I'm trying to set up an dev environment for Vue.js and rails API following a tutorial.

Eventually I hit a hurdle when trying to run the following command:

docker-compose run backend rails db:create

Here is the error:

$ 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

Here is my file tree

App

autheg-backend

autheg-frontend

docker-compose.yml

Here is my 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

Result of 'docker-compose run backend 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

Thanks!

This is caused because the path is not set in the Docker container, it's set on your development/local machine. Use the following command:

docker-compose run backend bundle exec rails db:create

Where is your backened dockerfile for rails development like this example:-

# 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" ]

to do db migration use:

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

Then

docker-compose up

Now you should be able to see the rails site using: localhost:8080 (instead of 3000)

Hope that helps you get started

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM