这是我关注的非常有用的文章:
Docker for an Existing Rails Application
我被问到了这个问题。应用程序可以运行没有错误,索引页面可以从网上冲浪看到。但索引页面看起来有线,看起来像scss不起作用,图像不会出现。
在Chrome控制台中,我得到了这个:
我做了很多搜索,发现它可能是资产:premcompile。我更改了docker-compose.yml,运行docker-compose run app rake db: assets:precompile
,甚至我将public/assets/*
复制到容器中,但没有运气。
主机:CentOS 7
任何想法都很棒。提前致谢。
1 app:
2 build: .
3
4 env_file: .env.production
5
6 environment:
7 RAILS_ENV: $RAILS_ENV
8
9 links:
10 - db
11
14 expose:
15 - "3000"
16
17 db:
18 image: postgres:9.4.5
19
20 volumes:
21 - eshop-postgres:/var/lib/postgresql/data
22
23 web:
24 build: .
25
26 dockerfile: config/containers/Dockerfile-nginx
27
28 volumes:
29 - ./public:/var/www/eshop/public
30
31 links:
32 - app
33 ports:
34 - "80:80"
2 FROM ruby:2.3.3-slim
12 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client libsqlite3-dev nodejs vim
15 ENV RAILS_ROOT /var/www/myapp
18 RUN mkdir -p $RAILS_ROOT/tmp/pids
19
21 WORKDIR $RAILS_ROOT
22
26 COPY Gemfile Gemfile
27
28 COPY Gemfile.lock Gemfile.lock
29
31 RUN gem install bundler
32
34 RUN bundle install
35
37 COPY . .
38
39 - RUN RAILS_ENV=production bundle exec rake assets:precompile --trace~
40 - RUN bundle exec rake assets:precompile --trace~
40 + RUN bundle exec rake RAILS_ENV=$RAILS_ENV DATABASE_URL=postgresql://$POSTGRES_USER:[email protected]/$POSTGRES_PRODUCTION_DB assets:precompile
41 + VOLUMES ["$RAILS_ROOT/public"]
46 + CMD [ "config/containers/app_cmd.sh" ]
2 FROM nginx
12 RUN apt-get update -qq && apt-get -y install apache2-utils vim
15 ENV RAILS_ROOT /var/www/myapp
18 WORKDIR $RAILS_ROOT
21 RUN mkdir log
25 COPY public public/
28 COPY config/containers/nginx.conf /tmp/myapp.nginx
32 RUN envsubst '$RAILS_ROOT' < /tmp/myappv.nginx > /etc/nginx/conf.d/default.conf
35 CMD [ "nginx", "-g", "daemon off;" ]
我根据@ bkunzi01建议做了一些更新。但没有运气。
40 RUN bundle exec rake RAILS_ENV=$RAILS_ENV DATABASE_URL=postgresql://$POSTGRES_USER:[email protected]/$POSTGRES_PRODUCTION_DB assets:precompile
41 VOLUME ["$RAILS_ROOT/public"]
46 CMD [ "config/containers/app_cmd.sh" ]
# *.env.production*
RAILS_ENV=production
RAILS_ROOT=/var/www/eshop
SECRET_KEY_BASE=the_long_code
POSTGRES_PRODUCTION_DB=production_db
POSTGRES_USER=postgres
POSTGRES_PASSWORD=keep_secret_ps
在docker-compose
的日志中还有另一个[警告]:
web_1 | 2017/03/02 05:45:14 [warn] 1#1: server name "/var/www/eshop/public" has suspicious symbols in /etc/nginx/conf.d/default.conf:16
web_1 | nginx: [warn] server name "/var/www/eshop/public" has suspicious symbols in /etc/nginx/conf.d/default.conf:16
大多数人都有这个错误,因为他们缺少node.js但是你有这个错误所以我认为下一个可能的候选者是添加一个卷指令,并考虑将dockerfile第39行的预编译命令更改为:
RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:[email protected]/dbname SECRET_TOKEN=makeupasecret assets:precompile
VOLUME ["$RAILS_ROOT/public"]
在此处添加此卷指令将通过将预编译资产保留在卷中来将您的资产公开给nginx。
此外,您可以为secret_token和数据库信息化妆。原因是当秘密令牌和用户没有提供时Rails会吓坏你但是你可以完全弥补它们,因为Rails不会在这里检查它们大声笑...
另外,请确保删除第二个预编译:assets命令,因为它不需要。
注意:我还会在Dockerfile的末尾添加一行来触发运行unicorn或者你要在nginx后面运行rails的其他appserver。
防爆。 CMD bundle exec unicorn -c config/unicorn.rb
我遇到了完全相同的问题。事实证明,真正的原因是我的config.public_file_server.enabled
没有设置config/environments/production.rb
。
为了解决这个问题,我只是在我的docker-compose.yml
中添加了一个环境变量:
version: '2'
services:
app:
...
environment:
- RAILS_SERVE_STATIC_FILES=true
我正在使用Rails 5.0.1。
希望这可以帮助。
因为我遇到了同样的问题,我遇到了这篇文章。我已经通过剥离我的容器解决了这个问题。我咨询了Rails的Docker文档:https://docs.docker.com/compose/rails/并相应地调整了我的Dockerfile和docker-compose.yml文件。
我现在继续使用nginx,因为我是Docker n00b,只是想让我的东西先行。
这是我的Dockerfile
# Base image:
FROM ruby:2.3.1
# Install dependencies
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev postgresql-client nodejs
# Set an environment variable where the Rails app is installed to inside of Docker image:
ENV RAILS_ROOT /Users/jessecalton/WatchNext
RUN mkdir -p $RAILS_ROOT/tmp/pids
# Set working directory, where the commands will be ran:
WORKDIR $RAILS_ROOT
# Gems:
ADD Gemfile* $RAILS_ROOT
RUN gem install bundler
RUN bundle install
# Copy the main application.
COPY . $RAILS_ROOT
还有我的docker-compose.yml
version: '3'
services:
# service configuration for our dockerized Rails app
app:
# use the Dockerfile next to this file
build: .
# sources environment variable configuration for our app
env_file: .env
# rely on the RAILS_ENV value of the host machine
environment:
RAILS_ENV: $RAILS_ENV
# makes the app container aware of the DB container
links:
- db
# expose the port we configured Unicorn to bind to
expose:
- "3000"
# service configuration for our database
db:
# use the preferred version of the official Postgres image
# see https://hub.docker.com/_/postgres/
image: postgres:latest
# service configuration for our web server
web:
# set the build context to the root of the Rails app
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- /Users/jessecalton/WatchNext/
# makes the web container aware of the app container
links:
- app
# expose the port we configured Nginx to bind to
ports:
- "3000:3000"
depends_on:
- db
我按照https://docs.docker.com/compose/rails/站点的其他配置说明进行操作,在创建和迁移数据库之后,让一切工作正常。
完全披露:我仍然需要找到一种方法来关闭并重新打开Puma服务器,而不会丢弃容器,从而丢弃数据库。换句话说,如果您按Ctrl + C关闭服务器然后再次执行docker-compose up
,Puma仍将在后台运行,您的应用程序将退出,并引用“服务器已在运行”。
只是想传递这些信息,因为同样的问题让我整天都有麻烦。