How do I open the Rails console in Discourse Docker in production mode?

I have tried to use the following commands to get to the console, but I am encountering a lot of errors when I run the command. Here is the input:

  docker run -i -t local_discourse/app:latest bash
  cd /var/www/discourse/
  RAILS_ENV=production bundle exec rails c

And here is the shortened output:

  root@d19b0a20422c:/var/www/discourse# RAILS_ENV=production bundle exec rails c
  Rails Error: Unable to access log file. Please ensure that /var/www/discourse/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
  Error connecting to Redis on localhost:6379 (ECONNREFUSED) subscribe failed, reconnecting in 1 second. Call stack [
  Error connecting to Redis on localhost:6379 (ECONNREFUSED) subscribe failed, reconnecting in 1 second. Call stack [
  Error connecting to Redis on localhost:6379 (ECONNREFUSED) subscribe failed, reconnecting in 1 second. Call stack [
  /var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:881:in `initialize': could not connect to server: No such file or directory (PG::Error)
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

  root@d19b0a20422c:/var/www/discourse#

Run rails c instead - this will open the console as the correct user in the correct mode.

You can also do this with rake and discourse (for /var/www/discourse/script/discourse).

I didn’t know about the discourse command. That’s cool. But I still cannot get it to work. I get the same giant error complaining about the lack of the log file, and not being able to connect to the database server.

You’re launching the container running a single command only: bash

That’s analogous to booting a VM in single user mode.

As Sam says, launch it normally and then ssh in.

1 Like

Just a correction:

cd /var/discourse/
./launcher enter app
rails c
10 Likes

I added the bellow in app. yml, but it fails, how can I run rails c with out interacting I need to run it once. (I am using ansible to automate installation)

run:
  - exec: echo "Beginning of custom commands"
  # If you want to set the 'From' email address for your first registration, uncomment and change:
  # After getting the first signup email, re-comment the line. It only needs to run once.
  - exec:   rails r "u = User.create!(username: '{{ discourse_developer_username }}', email: '{{ discourse_developer_emails }}', password: '{{ discourse_developer_password }}', admin: true)"