我正在尝试用docker设置ruby on rails一切都很好,但我希望动态域作为环境变量传递到nginx.conf文件,在构建图像期间通过docker-compose命令,但我不知道如何做。我正在尝试使用此命令Docker-compose构建dcoker-compose
Docker文件
FROM ruby:2.7.2
ENV RAILS_ROOT /var/www/quickcard
ENV BUNDLE_VERSION 2.1.4
ENV BUNDLE_PATH usr/local/bundle/gems
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 5000
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y build-essential \
git \
libxml2-dev \
libpq-dev \
libxslt-dev \
nodejs \
yarn \
imagemagick \
tzdata \
less \
cron \
&& rm -rf /var/cache/apk/*
RUN gem install bundler --version "$BUNDLE_VERSION"
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
COPY yarn.lock yarn.lock
RUN bundle install
EXPOSE $RAILS_PORT
RUN ln -s $RAILS_ROOT/config/systemd/puma.service /etc/systemd/system/quickcard
COPY . .
RUN crontab -l | { cat; echo ""; } | crontab -
RUN yarn install
RUN yarn install --check-files
RUN ls /var/www/quickcard/public
ENTRYPOINT ["entrypoint.sh"]
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
Nginx Docker文件
FROM nginx
RUN apt-get update -qq && apt-get -y install apache2-utils
ENV RAILS_ROOT /var/www/quickcard
WORKDIR $RAILS_ROOT
RUN mkdir log
COPY public public/
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
COPY ./multi_quickcard.key /etc/nginx/multi_quickcard.key
COPY ./quickcard-ssl-test.pem /etc/nginx/quickcard-ssl-test.pem
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
Nginx.配置文件,例如
upstream puma {
# Path to Puma SOCK file, as defined previously
server app:5000 fail_timeout=0;
}
server {
listen 80;
server_name default_server;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
location / {
root /var/www/quickcard/public/;
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /api {
root /var/www/quickcard/public/;
proxy_pass http://puma;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location ^~ /assets/ {
root /var/www/quickcard/public/;
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Docker合成文件
version: '2.2'
services:
app:
build:
context: .
dockerfile: ./Dockerfile
command: bash -c "bundle exec rails s -p 5000 -e production -b 0.0.0.0 && RAILS_ENV=production bundle exec rake assets:precompile"
environment:
RAILS_ENV: production
volumes:
- /var/wwww/quickcard
- /var/wwww/quickcard/public
ports:
- 5000:5000
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
environment:
RAILS_ENV: production
volumes:
- /var/wwww/quickcard/tmp
cron_job:
build: .
command: cron -f
nginx:
build:
context: .
dockerfile: ./nginx.Dockerfile
volumes:
- ./log-nginx:/var/log/nginx/
restart: always
ports:
- 80:80
- 443:443
2条答案
按热度按时间41zrol4v1#
Nginx推荐的方法似乎是使用envsubst实用程序。您需要创建一个模板文件,其中的变量为
$Variable
或{Variable}
。然后,您可以将模板文件传递到envsubst,该文件将从环境中呈现变量。这有一些缺点,因为它可能会无意中替换nginx变量,所以我会确保传入您想要替换的特定变量。
有关更多详细信息,请参阅解决类似问题的此问题:https://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf
1qczuiv02#
nginx Docker image可以在启动之前提取环境变量,但这有点棘手。一个解决方案是:
1.将env变量添加到
nginx.conf
文件中。1.在构建步骤中将其复制到容器中的
/etc/nginx/templates/nginx.conf.template
(与常规的/etc/nginx
相反)或作为卷复制。1.在
docker-compose.yml
中设置NGINX_ENVSUBST_OUTPUT_DIR: /etc/nginx
环境变量。这将导致
nginx.conf.template
文件作为nginx.conf
复制到/etc/nginx
,并且环境变量将替换为它们的值。有一点需要注意:在
docker-compose.yml
中使用command
属性似乎会禁用提取功能。如果需要运行自定义命令来启动nginx,可以使用Dockerfile版本。我创建了一个repo with the full setup,但如果它不可用:
运行无Dockerfile版本:
docker compose up nginx-no-dockerfile
运行Dockerfile版本:
确保站点文件夹中也有index.html文件。