I just started learning Docker and am now trying to build my Rails application that uses postgres in a docker setup. Currently my docker file looks as follows:
FROM ruby:2.2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
# Set an environment variable to store where the app is installed to inside
# of the Docker image.
ENV INSTALL_PATH /app-name
RUN mkdir -p $INSTALL_PATH
# This sets the context of where commands will be ran in and is documented
# on Docker's website extensively.
WORKDIR $INSTALL_PATH
COPY Gemfile Gemfile.lock ./
COPY vendor/gems/coupons vendor/gems/coupons
RUN bundle install
COPY . .
I used the official docker quickstart page to get this file up and running. This is my docker compose file:
volumes:
postgres_data: {}
services:
db:
image: postgres:9.5.4
env_file: .env
volumes:
- postgres_data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/app-name
env_file: .env
ports:
- "3000:3000"
depends_on:
- db
Now this all works perfectly fine, the problem is that I don't know what would be the best strategy for my database data. I would wish to implement something as follows:
- When my database container is started for the first time, it should automatically import data from a file that is in the root of my application.
- Each time my Rails container is started, my application should check if database migrations need to be executed. If new migrations are pending, this should be executed automatically.
Are these tasks able to be executed with docker/docker-compose? I couldn't really find anything like this on the web.