我正在为应用程序和数据库创建两个docker容器。在第一次启动docker-compose up
时,应用程序正在运行。然而,在下面的尝试中,它出现了:
Database 'time_distribution_production' already exists.
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1
Dockerfile
FROM ruby:3.1.2
ENV BUNDLER_VERSION=2.3.24
RUN apt update -qq && apt install -y libmsgpack-dev postgresql-client
RUN gem install bundler -v 2.3.24
RUN mkdir /time-distribution
WORKDIR /time-distribution
ENV RAILS_ENV production
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install
COPY . .
EXPOSE 3000
RUN chmod +x docker.sh
CMD ./docker.sh
docker.sh
#!/bin/bash
set -e
rm -f tmp/pids/server.pid
bundle exec rails db:setup
bundle exec rails db:migrate
bundle exec rails assets:precompile
bundle exec rails server -p 3000 -b '0.0.0.0'
docker-compose.production.yml
version: '3'
services:
app:
image: $IMAGE_NAME
restart: always
database:
restart: always
数据库。Yml用于生产数据库
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
production:
<<: *default
database: <%= Rails.application.credentials.dig(:production, :postgres_name) %>
username: <%= Rails.application.credentials.dig(:production, :postgres_username) %>
password: <%= Rails.application.credentials.dig(:production, :postgres_password) %>
host: database
我注意到当我重置卷:docker compose down -volumes
时错误消失了。我试图重新启动服务器上的容器。但无济于事。请告诉我如何解决这个问题。提前非常感谢!
docker.sh
bundle exec rails db:setup
将删除数据库中的所有数据。这肯定不是您想在生产环境中做的事情。
相反,我建议尝试用
创建一个数据库bundle exec rails db:create
当数据库已经存在时失败,但可以忽略。在下一步中,无论数据库是全新的还是之前存在的,迁移都将正常进行:
bundle exec rails db:create
bundle exec rails db:migrate
bundle exec rails assets:precompile