Docker/Mongodb data not persistent

岁酱吖の 提交于 2019-12-24 07:28:31

问题


Iam running a rails api server with mongodb all worked perfectly find and I started to move my server into docker.

Unfortunately whenever I stop my server (docker-compose down) and restart it all data are lost and the db is completely empty.

This is my docker-compose file:

version: '2'
services:
  mongodb:
    image: mongo:3.4
    command: mongod
    ports:
      - "27017:27017"
    environment:
      - MONGOID_ENV=test
    volumes:
      - /data/db

  api:
    build: .
    depends_on:
      - 'mongodb'
    ports:
      - "3001:3001"
    command: bundle exec rails server -p 3001 -b '0.0.0.0'
    environment:
      - RAILS_ENV=test
    links:
      - mongodb

And this is my dockerfile:

FROM ruby:2.5.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

ENV APP_HOME /app

RUN mkdir $APP_HOME

WORKDIR $APP_HOME

COPY Gemfile* $APP_HOME/
RUN bundle install

COPY . $APP_HOME

RUN chown -R nobody:nogroup $APP_HOME
USER nobody

ENV RACK_ENV test
ENV MONGOID_ENV test

EXPOSE 3001

Any idea whats missing here?

Thanks, Michael


回答1:


In docker-compose, I think your "volumes" field in the mongodb service isn't quite right. I think

volumes:
  - /data/db

Should be:

volumes:
  - ./localFolder:/data/db


来源:https://stackoverflow.com/questions/50930008/docker-mongodb-data-not-persistent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!