0

I have docker-compose project for Rails depends on mongo. I need to run mongorestore during build before CMD in dockerfile runs the rails server. How I can do that?

I tried https://stackoverflow.com/a/66069362/4011757 but it doesn't start the mongorestore.

The server doesn't start says collections not found needed to start it. So it is necessary to have mongorestore before start of the server

Dockerfile

FROM ruby:2.3.0

RUN apt-get update -qq && apt-get install -y nodejs npm
RUN npm install --global coffeescript
WORKDIR /
COPY Gemfile /Gemfile
COPY Gemfile.lock /Gemfile.lock

RUN mkdir -p /log && touch /log/development.log
RUN gem install bundler -v "1.17.3"
RUN bundle install
COPY . /
CMD ["sh", "-c", "bundle exec padrino start"]

docker-compose.yml

version: '3.4'
services: 
  mongo:
    image: mongo:3.2.4
    container_name: 'tickets-mongo'
    volumes: 
      - ./mongorestore.sh:/docker-entrypoint-initdb.d/mongorestore.sh
      - ./db:/db
    ports:
      - 27017:27017

  redis:
    image: redis:alpine
    container_name: redis
    depends_on: 
      - mongo
    ports: 
      - 6379:6379
    volumes:
      - redis_data:/var/lib/redis
  elasticsearch:
    image: elasticsearch:7.9.2
    container_name: 'tickets-elasticsearch'
    environment: 
      - discovery.type=single-node
    depends_on: 
      - redis
    ports: 
      - 9200:9200
    volumes: 
      - elasticsearch_data:/usr/share/elasticsearch/data
  app-worker:
    env_file: 
      - .env_development.env
    build: .
    container_name: tickets-worker
    depends_on: 
      - elasticsearch
    links: 
      - mongo:mongo
      - redis:redis
      - elasticsearch:elasticsearch
  app:
    env_file: 
      - .env_development.env
    build: .
    container_name: tickets-web
    depends_on: 
      - app-worker
    links: 
      - mongo:mongo
      - redis:redis
      - elasticsearch:elasticsearch
    ports: 
      - 3000:3000
volumes: 
  mongo: {}
  redis_data: {}
  elasticsearch_data: {}
SulmanWeb
  • 601
  • 6
  • 24

1 Answers1

0

Update:

I was afraid that depends_on or links might not work in this case. The reason is, Compose does start mongo before it starts either app-worker or app. But it doesn't wait until mongo service is in READY state to start the dependant services.

Basically, what you need is to find a way to tell Compose to wait until mongo service is in READY state.

The solution is described in Control startup and shutdown order in Compose documentation

I hope this helps you. Cheers !!!


Could you try adding depends_on in app-worker for mongo service. The docker-compose.yml file should look like:

...
  app-worker:
    env_file: 
      - .env_development.env
    build: .
    container_name: tickets-worker
    depends_on: 
      - elasticsearch
      - mongo
    links:
...

Although, I'm a little confused as to why you have the same context in both app and app-worker services.

Eranga Heshan
  • 5,133
  • 4
  • 25
  • 48