You need to set the Rails environment:
RAILS_ENV=production sudo -E -u discourse ruby phpbb3.rb
Also, you can avoid the repetitive sudo
cruft by becoming the discourse
user instead of editing and running these scripts as root
. Just run su - discourse
after entering the container.