本文介绍了数据库未持久保存在Docker中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我在这里缺少一些非常基本的东西。我已经看到了一些有关使用Docker持久化数据的较旧问题,但是我想我正在关注找到的最新文档。
我有一个尝试在docker中运行的Rails应用程序。它运行良好,但是每次启动时,我都会得到 ActiveRecord :: NoDatabaseError 。创建数据库并迁移它之后,应用程序运行良好,直到关闭它并重新启动它。

I know I am missing something very basic here. I have see some of the older questions on persisting data using docker, but I think I am following the most recent documentation found here.I have a rails app that I am trying to run in docker. It runs fine but every time I start it up i get ActiveRecord::NoDatabaseError. After I create the database and migrate it, the app runs fine, until I shut it down and restart it.

这是我的docker文件:

here is my docker file:

FROM ruby:2.3.0
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
ENV RAILS_ROOT /ourlatitude
RUN mkdir -p $RAILS_ROOT/tmp/pids
WORKDIR $RAILS_ROOT
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler
RUN bundle install
COPY . .

这是我的docker-compose.yml文件

and here is my docker-compose.yml file

version: '2'
services:
  db:
    image: postgres:9.4.5
  app:
    build: .
    environment:
      RAILS_ENV: $RAILS_ENV
  ports:
    - "3000:3000"
  command: bundle exec rails s -b 0.0.0.0
  volumes:
    - .:/ourlatitude/database
  depends_on:
    - db

我遵循的基本流程是这样:

the basic flow I am following is this:

export RAILS_ENV=development
docker-compose build
docker-compose up
docker-compose run app rake db:create
docker-compose run app rake db:migrate

这时应用程序将运行正常

at this point the app will be running fine

但是我这样做

docker-compose down
docker-compose up

然后我回到 ActiveRecord :: NoDatabaseError

如我所说,我认为我缺少了一些非常基本的东西。

So as I said, I think I am missing something very basic.

推荐答案

它看起来不像是将postgres放在一个卷上,可能是缺少其他持久数据源n您的应用程序容器,看来您错过了应用程序容器定义的缩进。

It doesn't look like you put your postgres on a volume, you may be missing other persistent data sources in your app container, and it appears you missed some indentation on your app container definition.

version: '2'
services:
  db:
    image: postgres:9.4.5
    volumes:
      - postgres-data:/var/lib/postgresql/data
  app:
    build: .
    environment:
      RAILS_ENV: $RAILS_ENV
    ports:
      - "3000:3000"
    command: bundle exec rails s -b 0.0.0.0
    volumes:
      - .:/ourlatitude/database
    depends_on:
      - db
volumes:
  postgres-data:
    driver: local

在上面的示例中,postgres数据存储在一个命名卷中。请参阅上的建议,以获取有关持久保存该应用程序数据的更多详细信息。如果仍在丢失数据,请在容器上检查 docker diff $ container_id 的输出,以查看在卷外更改了哪些文件,这些文件在一次宕机/宕机时会丢失

In the example above, the postgres data is stored in a named volume. See the advice on docker hub for more details on persisting data for that application. If you are still losing data, check the output of docker diff $container_id on a container to see what files are changing outside of your volumes that would be lost on a down/up.

这篇关于数据库未持久保存在Docker中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:49