【发布时间】:2017-12-08 21:26:32
【问题描述】:
我在 docker 容器中运行 Rails 应用程序,但是当我访问时,应用程序返回以下错误
当我运行命令时
erb config/database.yml
环境变量被替换
码头工人撰写:
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
码头文件:
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"]
数据库.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
容器检查结果:
...
"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"
],
...
我尝试使用 foreman start 和 rails s,但两种方式都发生了错误。当变量替换为真实值时,应用程序正常工作
有什么想法吗?谢谢!
【问题讨论】:
-
buildtoship.com/how-i-deploy-a-rails-app-using-docker - 你需要一些额外的设置来传递这些变量,这里有一个手册。
-
@PavelOganesyan 感谢您的回复!在示例中,它似乎使用的是 Ubuntu,而 Ngix 文件夹在 Centos7 中有点不同。我尝试将 nginx.conf 设置放在 env 中,但问题仍然存在。
-
'./docker/.env.db' 的内容是什么?如果您在应用容器上运行
docker inspect,它会为环境显示什么? -
嗨@AndrewSwerlick,我根据您的要求更新了我的问题。感谢您的关心。
-
您的问题是否与此问题stackoverflow.com/questions/35786885/… 中所涵盖的内容相似。您是否有可能尝试手动加载数据库配置而不通过 ERB?
标签: ruby-on-rails docker activerecord docker-compose dockerfile