我第一次尝试将我的rails项目放在谷歌云引擎上,但遇到了很多麻烦。
我想用自定义运行时上传我的项目app.yaml
(因为我也希望 yarn 安装依赖项),但是部署命令失败并出现以下错误:Error Response: [4] Your deployment has failed to become healthy in the allotted time and therefore was rolled back. If you believe this was an error, try adjusting the 'app_start_timeout_sec' setting in the 'readiness_check' section.
PS:应用程序在本地运行(开发和生产环境)。
我的 app.yaml 看起来像这样:
entrypoint: bundle exec rails s -b '0.0.0.0' --port $PORT
env: flex
runtime: custom
env_variables:
My Environment variables
beta_settings:
cloud_sql_instances: ekoma-app:us-central1:ekoma-db
readiness_check:
path: "/_ah/health"
check_interval_sec: 5
timeout_sec: 4
failure_threshold: 2
success_threshold: 1
app_start_timeout_sec: 120
我的 Dockerfile 看起来像这样:
FROM l.gcr.io/google/ruby:latest
RUN apt-get update -qq && apt-get install apt-transport-https
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 libpq-dev imagemagick yarn
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
RUN gem install pkg-config -v "~> 1.1"
RUN bundle install && npm install
COPY . /app
当使用 ruby 运行时进行部署时,我意识到生成的 dockerfile 要复杂得多,而且可能是完整的,谷歌提供了一个 repo 来生成它。
因此,我尝试查看 google 共享的 ruby-docker 公共(public)存储库,但我不知道如何使用他们生成的 docker 镜像,因此修复了我的 Dockerfile 问题
https://github.com/GoogleCloudPlatform/ruby-docker
有人可以帮我弄清楚我的设置出了什么问题以及如何运行这些 ruby-docker 镜像(似乎非常有用!)?
谢谢!
最佳答案
app.yaml 中的“入口点”字段在自定义运行时运行时不使用。相反,在 Dockerfile 中设置 CMD。例如。:
CMD ["bundle", "exec", "rails", "s", "-b", "0.0.0.0", "--port", "8080"]
这可能会让您的应用程序运行。 (请记住,环境变量不是以 exec 形式插入的,因此我将
$PORT
替换为硬编码端口 8080,这是 App Engine 期望的端口。)作为备选:
可以使用 ruby-docker 存储库中的 Ruby 运行时镜像,而不必使用自定义运行时(即您可能不需要编写自己的 Dockerfile),即使您有自定义构建步骤(例如执行 yarn 安装)。
runtime: ruby
中的大部分构建过程是可定制的,但它没有很好的记录。如果您想尝试这条路径,TL;DR 是:runtime: ruby
在您的 app.yaml
并且不要提供您自己的 Dockerfile。 (当然要恢复 entrypoint
。)runtime: ruby
中通常不存在的 ubuntu 软件包, 在 app.yaml
中列出它们在 runtime_config
下: packages
.例如:runtime_config:
packages:
- libgeos-dev
- libproj-dev
app.yaml
中列出它们在 runtime_config
下: build
.它们在捆绑安装步骤(本身不能修改)之后在 Dockerfile 中执行。例如:runtime_config:
build:
- npm install
- bundle exec rake assets:precompile
- bundle exec rake setup_my_stuff
请注意,默认情况下,如果您不提供自定义构建步骤,ruby 运行时的行为就像有一个构建步骤:
bundle exec rake assets:precompile || true
.也就是说,默认情况下,runtime: ruby
将尝试在应用引擎部署期间编译您的 Assets 。如果您确实修改了构建步骤并且希望保留此行为,请确保将该 rake 任务作为自定义构建步骤的一部分。 关于ruby-on-rails - 谷歌云 ruby 部署和 ruby-docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233614/