简体   繁体   中英

Run `rails db:setup` in Docker with schema in structure.sql

I have a new Rails app that I am planning to deploy in a Docker container. We are using structure.sql instead of schema.rb . In my local setup (using a docker-compose file, with Postgres in a separate container), when I run rails db:setup , I get the following error:

rails aborted!
failed to execute:
psql -q -f /rails/db/structure.sql cappy_dev

Please check the output above for any errors and make sure that `psql` is installed in your PATH and has proper permissions.

/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/postgresql_database_tasks.rb:99:in `run_cmd'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/postgresql_database_tasks.rb:71:in `structure_load'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:213:in `structure_load'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:226:in `load_schema'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:253:in `block in load_schema_current'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:292:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:291:in `each'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:291:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:252:in `load_schema_current'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/railties/databases.rake:306:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/railties/databases.rake:310:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:9:in `require'
bin/rails:9:in `<main>'
Tasks: TOP => db:structure:load
(See full trace by running task with --trace)

This makes sense because my Rails container only has Ruby and Rails-related tools, and does not have Postgres or pqsl, which are in the Postgres container.

How do I run rails db:setup (or db:migrate or other tasks) if I don't have psql, pg_dump, and other Postgres tools in my Rails container?

UPDATE: Here is my docker-compose file:

version: '3'
services:
  postgres:
    image: postgres:9.6
    volumes:
      - cappy-data:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    environment:
      POSTGRES_PASSWORD: postgres

  cappy:
    image: REDACTED.dkr.ecr.us-east-1.amazonaws.com/cappy:latest
    build:
      context: .
    command: rails s -p 3000 -b '0.0.0.0'
    depends_on:
      - postgres
    volumes:
      - .:/rails
    ports:
      - "3000:3000"
    links:
      - "postgres:postgres"
    environment:
      RAILS_ENV: development

volumes:
  cappy-data:

I don't think this file is the problem though-- db:setup can create the database, it just can't load the structure.sql because it can't seem to find psql .

I tried a couple of things--running rails db:create and then db:migrate would have worked to get the local database set up, but it would not have worked in the long run because when you run rails db:migrate , at the end of the task it dumps the schema. With Postgres, and with schema_format set to :sql (which is what generates the structure.sql file), the rake task relies on Postgres tools like psql and pg_dump.

In the end, the best way to handle it was to add postgresql-client (don't need the whole server) to the Rails Docker image. It has the tools it needs right at hand and everything works fine.

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