Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker/Mongodb data not persistent

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

like image 388
Michael H. Avatar asked Dec 07 '22 14:12

Michael H.


1 Answers

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
like image 181
ET Come Back Avatar answered Dec 21 '22 03:12

ET Come Back