Rails不会替换database.yml中的ENV值(Rails not replace ENV's value in database.yml)

33 IT屋

I'm running a Rails application in docker container, but when I access, the application returns the error below

enter image description here

When I run the command

erb config/database.yml

the env variables are replaced

docker-compose:

version: '3'
networks:
  banco:
  web:
  fila:
services:
  db:
    image: postgres:9.6
    env_file:
      - './docker/.env.db'
    networks:
      - banco
  app:
    build: .
    links:
      - db
    env_file:
      - './docker/.env.web'
    networks:
      - banco
      - web
      - fila
    depends_on:
      - db
    expose:
      - "3000"
  frontend:
    image: nginx:1.13
    volumes:
      - ./docker/nginx/default:/etc/nginx/nginx.conf
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8081:80
    networks:
      - web
    depends_on:
      - app

dockerfile:

FROM centos:7.4.1708
LABEL maintainer 'Blabla'

# Pre install
RUN yum -y install initscripts; \
    yum clean all; \
    yum -y update;  \
    yum -y install svn git git-svn telnet; \
    yum -y install postgresql-devel gcc-c++ patch readline readline-devel zlib zlib-devel libcurl-devel ImageMagick ImageMagick-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison;

# Ferramentas uteis
RUN yum -y install bash-completion; \
    yum -y install yum-plugin-priorities; \
    yum -y install epel-release; \
    yum -y install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm; \
    yum -y install vim-enhanced; \
    yum -y install htop;

RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
RUN \curl -L https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.3"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"

RUN mkdir /usr/app
WORKDIR /usr/app

COPY . /usr/app

RUN /bin/bash -l -c "bundle check || bundle install"

RUN /bin/bash -l -c "RAILS_ENV=production bundle exec rake generate_secret_token"

RUN /bin/bash -l -c "foreman export systemd /etc/systemd/system -u root -a redmine_visagio"

CMD ["/sbin/init"]

database.yml

default: &default
  adapter: postgresql
  encoding: utf8
  host: db
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
env DB_NAME;
env DB_USER;
env DB_PASSWORD;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

.env.db:

POSTGRES_DB=redmine
POSTGRES_USER=redmine
POSTGRES_USER_PASSWORD=some_password

.env.web:

DB_NAME=redmine
DB_USER=redmine
DB_PASSWORD=some_password

container inspect result:

...
"Env": [
                "DB_PASSWORD=redmine_db",
                "DB_NAME=redmine",
                "DB_USER=some_password",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.13.7-1~stretch",
                "NJS_VERSION=1.13.7.0.1.15-1~stretch"
            ],
...

I tried with foreman start and rails s but the error happened in both ways. When the variables are replaced by the real values, the application works normally

Any idea? Thanks!

解决方案

The problem was solved using the answer of @user155995 from this other link: rails database.yml not accepting ERB

I just experienced the same thing, and came across your post. I had been following a tutorial that had me create a puma.conf file that contained the code below:

ActiveRecord::Base.establish_connection( YAML.load_file( "#{app_dir}/config/database.yml" )[rails_env])

I modified to the following, and everything worked as expected:

require 'erb'
ActiveRecord::Base.establish_connection( YAML.load( ERB.new( File.read( "#{app_dir}/config/database.yml" )).result)[rails_env])

Thanks to @Pavel Oganesyan and @AndrewSwerlick

我正在docker容器中运行Rails应用程序,但是当我访问该应用程序时,该应用程序返回以下错误


在此处输入图片描述 < / p>

当我运行命令


  erb config / database.yml 

环境变量被替换


docker-compose:


 版本:" 3" 
网络:
banco:
网络:
fila:
服务:
db:
图片:postgres:9.6
env_file:
-'./docker/.env.db'
网络:
-banco
应用程序:
构建:。
链接:
-db
env_file:
-'./docker/.env.web'
网络:
-banco
-网络
-费用
取决于:
-db
暴露:
-" 3000"
前端:
图像:nginx:1.13
数量:
-./docker/nginx/default:/etc/nginx/nginx.conf
-./docker /nginx/default.conf:/etc/nginx/conf.d/default.conf
端口:
-8081:80
网络:
-网络
取决于_on :
-应用程序

dockerfile:


  FROM centos:7.4.1708 
LABEL维护程序'Blabla'

#预安装
运行yum -y install initscripts; \
yum clean all; \
yum -y更新; \
yum -y install svn git git-svn telnet; b
yum -y install postgresql-devel gcc-c ++补丁readline readline-devel zlib zlib-devel libcurl-devel ImageMagick ImageMagick-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison;

#Ferramentas uteis
运行yum -y install bash-completion; \
yum -y install yum-plugin-priorities; \
yum -y install epel-release; \
yum -y安装http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm; \
yum -y install vim-enhanced; \
yum -y install htop;

RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
RUN \curl -L https:// get。 bash -s稳定
RUN / bin / bash -l -c" rvm要求";
RUN / bin / bash -l -c" rvm install 2.3";
RUN / bin / bash -l -c" gem install bundler --no-ri --no-rdoc"

运行mkdir / usr / app
WORKDIR / usr / app

COPY。 / usr / app

RUN / bin / bash -l -c" bundle check ||捆绑安装"

RUN / bin / bash -l -c" RAILS_ENV =生产捆绑包执行rake generate_secret_token"

RUN / bin / bash -l -c" foreman export systemd / etc / systemd / system -u root -a redmine_visagio"

CMD [" / sbin / init"]

database.yml


 默认值:&默认
适配器:PostgreSQL
编码:utf8
主机:db
数据库:< %= ENV ['DB_NAME']%>
用户名:<%= ENV ['DB_USER']%>
密码:<%= ENV ['DB_PASSWORD']%>

开发:
< <:: *默认

测试:
<< ;: * *默认

生产:
<< ;: *默认

nginx.conf


< pre> user nginx;
worker_processes自动;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
env DB_NAME;
env DB_USER;
env DB_PASSWORD;

#加载动态模块。参见/usr/share/nginx/README.dynamic。
include /usr/share/nginx/modules/*.conf;

个事件{
worker_connections 1024;
}

http {
log_format main’$ remote_addr-$ remote_user [$ time_local]" $ request" ’
’$ status $ body_bytes_sent" $ http_referer" ’
’"" $ http_user_agent" " $ http_x_forwarded_for"’;

access_log /var/log/nginx/access.log main;

发送文件;
tcp_nopush;
tcp_nodelay;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type应用程序/字节流;

#从/etc/nginx/conf.d目录加载模块化配置文件。
#有关更多信息,请参见http://nginx.org/en/docs/ngx_core_module.html#include
#。
include /etc/nginx/conf.d/*.conf;
}

.env.db:


 < code> POSTGRES_DB = redmine 
POSTGRES_USER = redmine
POSTGRES_USER_PASSWORD = some_password

.env。网络:


  DB_NAME = redmine 
DB_USER = redmine
DB_PASSWORD = some_password

容器检查结果:


  ... 
" Env": [
" DB_PASSWORD = redmine_db",
" DB_NAME = redmine",
" DB_USER = some_password",
" PATH = / usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin",
" NGINX_VERSION = 1.13.7-1〜stretch",
" NJS_VERSION = 1.13"。 7.0.1.15-1〜拉伸"。
],
...

我尝试了开工开始 rails ,但错误均以两种方式发生。当变量被实数值替换时,应用程序可以正常工作


有什么想法吗?谢谢!


解决方案

问题是通过使用@ user155995的其他链接来解决的:
rails database.yml不接受ERB



< blockquote>

我刚刚遇到了同样的事情,并且偶然发现了您的帖子。我有
在遵循一个教程,该教程让我创建了一个puma.conf文件,其中
包含以下代码:



  ActiveRecord :: Base。Establishment_connection(YAML.load_file("#{app_dir} /config/database.yml")[rails_env])


我修改为以下内容,并且一切都按预期工作:



 需要'erb' 
ActiveRecord :: Base。Establishment_connection(YAML.load(ERB.new(File.read("#{app_dir} /config/database.yml"))。result)[rails_env])



感谢@Pavel Oganesyan和@AndrewSwerlick


本文地址:IT屋 » Rails不会替换database.yml中的ENV值