phusion / passenger-docker

Docker base images for Ruby, Python, Node.js and Meteor web apps
MIT License
2.78k stars 408 forks source link

Environment variables are not visible in Rails app #341

Open HanguChoi opened 2 years ago

HanguChoi commented 2 years ago

I coudn't set environment variables properly even after adding nginx-env.conf to the /etc/nginx/main.d directory.

When I start app directly with rails server command, everything works as expected. And when I set value in the nginx-env.conf file as shown below, it works as well.

env SOME_ENV_KEY=some_env_value;
env RAILS_SERVE_STATIC_FILES=true;

But if I remove the Env value as described in the documentation, the environment variables are not visible in the rails app.

Current Setting

nginx-env.conf

env SOME_ENV_KEY;
env RAILS_SERVE_STATIC_FILES;

.env

SOME_ENV_KEY=some_env_value
RAILS_SERVE_STATIC_FILES=true

Dockerfile

FROM phusion/passenger-ruby30:2.0.1

RUN rm /etc/nginx/sites-enabled/default
ADD nginx-env.conf /etc/nginx/main.d/nginx-env.conf
ADD nginx.conf /etc/nginx/sites-enabled/nginx.conf
RUN rm -f /etc/service/nginx/down

RUN bash -lc 'rvm install ruby-3.0.3'
RUN bash -lc 'rvm --default use ruby-3.0.3'

CMD ["/sbin/my_init"]

RUN apt-get update \
    && apt-get -y install build-essential \
    && apt-get -y install default-libmysqlclient-dev \
    && apt-get -y install shared-mime-info \
    && apt-get -y install snapd

RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs
RUN curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null
RUN echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn

WORKDIR /home/app/passenger-docker-env-test
ENV BUNDLE_PATH=/home/app/passenger-docker-env-test/vendor/bundle

COPY --chown=app:app . /home/app/passenger-docker-env-test

RUN yarn --prod   

RUN gem install bundler -v 2.3.9

RUN bundle config set cache_all true
RUN bundle package
RUN bundle install --local
RUN bundle exec rake RAILS_ENV=production assets:precompile

docker-compose.yml

version: '3'  
services:
  web:
    container_name: 'web_container'
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/log/passenger_docker_env_test_nginx:/var/log/nginx
    command: service nginx start
    env_file:
      - .env

nginx.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/app/passenger-docker-env-test/public;
    passenger_app_root /home/app/passenger-docker-env-test;
    passenger_enabled on;
    passenger_user app;
    passenger_app_env production;
    passenger_ruby /usr/local/rvm/gems/ruby-3.0.3/wrappers/ruby;
}

Starting command

docker-compose up --build -d