谁能指出,-在 Docker 容器内的 Rails API 应用程序上运行 RSpec 测试的最佳方法是什么(在容器构建/运行期间)?
目的是能够将开发环境与其他环境分开,并仅在模式下运行测试,仅在环境development中启动 Puma 服务器。staging, production
什么是放置bundle exec rspec命令的正确位置, - 在单独的entrypoint.sh脚本中,直接在Dockerfile,docker-compose.yml文件或其他解决方案中?
所有 Google 的结果以及 PragProg 的Docker for Rails 开发人员一书都没有示例,运行他们提供的测试的唯一方法是针对已经运行的容器运行它们。
其实我的Dockerfile样子是这样的:
FROM ruby:2.6.1
RUN apt-get update -yqq
RUN apt-get install -yqq --no-install-recommends build-essential zip unzip libpq-dev libaio1 libaio-dev nodejs
ENV APP_HOME=/usr/src/app
ENV BUNDLE_PATH /gems
COPY . $APP_HOME
RUN echo "gem: --no-rdoc --no-ri" >> ~/.gemrc
WORKDIR $APP_HOME
RUN gem update --system
RUN gem install bundler
RUN bundle install
RUN ["chmod", "+x", "entrypoint.sh"]
CMD ["./entrypoint.sh"]
entrypoint.sh看起来像这样:
#!/bin/bash
set -e
if [ -f tmp/pids/server.pid ]; then
rm tmp/pids/server.pid
fi
./wait-for-it.sh ${DATABASE_HOST}:${DATABASE_PORT}
if [ -z "$RAILS_ENV" ]; then
echo "RAILS_ENV variable is not set, will use development by default"
bundle exec rails db:reset
bundle exec rails db:migrate
bundle exec rspec
else
bundle exec rails s -e $RAILS_ENV -p 3000 -b 0.0.0.0
fi
最后,docker-compose.yml:
version: '3.3'
services:
api:
build: ../..
ports:
- '3000:3000'
volumes:
- .:/usr/src/app
- gem_cache:/gems
env_file:
- ./env/database.env
- ./env/web.env
depends_on:
- database
# Keeps the stdin open, so we can attach to our app container's process and
# do stuff such as `byebug` or `binding.pry`:
stdin_open: true
# Allows us to send signals (CTRL+C, CTRL+P + CTRL+Q) into the container
tty: true
database:
image: postgres:9.6
env_file:
- ./env/database.env
volumes:
- db-data:/var/lib/postgresql/data
ports:
- 5432:5432
volumes:
db-data:
gem_cache:
谢谢你。