« Back to Index

Docker Compose: Example Ruby Application

View original Gist on GitHub

docker-compose.yml

api:
  build: ./
  restart: "always"
  command: "/bin/bash -c 'source /bootstrap.sh'"
  ports:
    - "8080:9292"
  volumes:
    - "./docker-bootstrap-app.sh:/bootstrap.sh"
    - "./src:/app"
  links:
    - "db"
  labels:
    uk.co.bbci.api.mozart: "true"

db:
  image: "postgres"
  container_name: "local_postgres"
  environment:
    - "POSTGRES_DB=mozart"
    - "POSTGRES_PASSWORD=mysecretpassword"
  ports:
    - "5000:5432"
  labels:
    uk.co.bbci.db.mozart: "true"

Dockerfile

FROM ruby:2.1.2
MAINTAINER Mark McDonnell <mark.mcdonnell@bbc.co.uk>

LABEL uk.co.bbci.api.mozart="true"

RUN mkdir /app
WORKDIR /app

ADD src/Gemfile /app/Gemfile
RUN bundle install --retry 10 --jobs 4

EXPOSE 9292

docker-bootstrap-app.sh

#!/bin/bash

a=$(bundle check)
b="The Gemfile's dependencies are satisfied"

if [ "$a" != "$b" ]; then
  # building the image locally on a poor network
  # can result in gems not being installed
  bundle install --retry 10
fi

bundle exec sequel -E -m db/migrations/ postgres://$(echo "postgres:$DB_ENV_POSTGRES_PASSWORD@$DB_PORT_5432_TCP_ADDR:$DB_PORT_5432_TCP_PORT/$DB_ENV_POSTGRES_DB")
bundle exec rackup config.ru -p 9292 -o 0.0.0.0