17
I'm having the same issue. Here's what I'm currently working on:
我有同样的问题。这是我目前正在做的事情:
Option 1: use a single image for both nginx and the app
This way, I can build the image once (with the app, precompiled assets and nginx), then run two instances of it: one running the app server, and another for the nginx frontend:
这样,我可以构建一次图像(使用app,预编译资产和nginx),然后运行它的两个实例:一个运行app服务器,另一个运行nginx前端:
docker build -t hello .
docker run --name hello-app hello rackup
docker run --name hello-web -p 80:80 --link hello-app:app hello nginx
Not pretty, but very easy to set up and upgrade.
不漂亮,但很容易设置和升级。
Option 2: use a shared volume, and precompile assets as a job
Shared volumes cannot be updated in the build process, but can be updated by a container instance. So we can run our rake task to precompile the assets just before running our app:
共享卷无法在构建过程中更新,但可以由容器实例更新。因此,我们可以在运行应用程序之前运行我们的rake任务来预编译资产:
docker build -t hello .
docker run -v /apps/hello/assets:/app/public/assets hello rake assets:precompile
docker run --name hello-app hello rackup
docker run --name hello-web -p 80:80 --link hello-app:app -v /apps/hello/assets:/usr/share/nginx/html/assets nginx
This looks like a more robust option, but will require a more complex instrumentation. I'm leaning towards this option, however, since we'll need a separate job for database migrations anyway.
这看起来像一个更强大的选项,但需要更复杂的仪器。但是,我倾向于这个选项,因为无论如何我们都需要一个单独的数据库迁移工作。
Option 3: distribute the assets to a CDN on build-time
Your Dockerfile can upload the resulting assets directly to a CDN. Then you configure your Rails app to use it as the asset_host
. Something like:
您的Dockerfile可以将生成的资产直接上传到CDN。然后配置Rails应用程序以将其用作asset_host。就像是:
RUN rake assets:precompile && aws s3 sync public/assets s3://test-assets/
I'm currently experimenting with this option. Since I'm using Amazon CloudFront, looks like I can just sync the resulting assets to S3 using the AWS CLI. There's also a gem for that (asset_sync), but it looks stale.
我正在尝试这个选项。由于我使用的是Amazon CloudFront,看起来我可以使用AWS CLI将生成的资产同步到S3。还有一个宝石(asset_sync),但它看起来很陈旧。
The downside is that you'll have to send the needed AWS credentials to the build context or the Dockerfile itself – this might need committing them to your source repository, if you're using an automated build.
缺点是您必须将所需的AWS凭证发送到构建上下文或Dockerfile本身 - 如果您使用的是自动构建,则可能需要将它们提交到源存储库。