Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

docker-rails.yml #4

Closed
rosskevin opened this issue Sep 11, 2015 · 1 comment
Closed

docker-rails.yml #4

rosskevin opened this issue Sep 11, 2015 · 1 comment

Comments

@rosskevin
Copy link
Member

Since multiple environments can be setup the same, why not try and DRY this up? We already use Dry::Config, so it's easy enough.

The goal would be to executed docker-rails <name>, where in the file below, the name matches an environment (but it's just a name, no magic there).

i.e. docker-rails test vs docker-rails parallel_tests vs docker-rails development

Here's a first draft of what would be inherited and ultimately fed to docker-compose:

verbose: true

#--------------------------------
# Allow skipping any command - good for debugging Dockerfile/compose configurations
#    skip:
#      - up
#      - before_command
#      - stop
#      - extract
#      - remove_volumes
#      - remove_compose
#      - remove_dangling
#      - remaining_containers
#
skip:
  - remove_compose

# local environments need elasticsearch, staging/production connects to existing running instance.
elasticsearch: &elasticsearch
  elasticsearch:
    image: library/elasticsearch:1.7
    ports:
      - "9200"

development:
  docker-compose:
    <<: *elasticsearch
    web:
      links:
        - elasticsearch # standard yaml doesn't merge arrays so we have to add this explicitly
      environment:
        - RAILS_ENV=development
      command: >
        bash -c "

        echo 'Bundling gems'
        && bundle install --jobs 4 --retry 3

        && echo 'Generating Spring binstubs'
        && bundle exec spring binstub --all

        && echo 'Clearing logs and tmp dirs'
        && bundle exec rake log:clear tmp:clear

        && echo 'Check and wait for database connection'
        && bundle exec docker-rails-db-check

        && echo 'DB rebuild'
        && bundle exec rake db:rebuild_dev

        && echo "Starting app server"
        && bundle exec rails s -p 3000

        && echo 'Setup and start foreman'
        && gem install foreman
        && foreman start
        "

test:
  before_command: rm -Rf target
  docker-compose:
    <<: *elasticsearch
    web:
      links:
        - elasticsearch # standard yaml doesn't merge arrays so we have to add this explicitly
      environment:
        - RAILS_ENV=test
      command: >
        bash -c "
        echo 'Bundling gems'
        && bundle install --jobs 4 --retry 3

        && echo 'Clearing logs and tmp dirs'
        && bundle exec rake log:clear tmp:clear

        && echo 'Check and wait for database connection'
        && bundle exec docker-rails-db-check

        && echo 'DB rebuild'
        && bundle exec rake db:rebuild_test

        && echo 'Tests'
        && cd ../..
        && xvfb-run -a bundle exec rake spec SPEC=spec/app/models/plan_spec.rb cucumber FEATURE=features/public_pages.feature
        "

#        &&
#         && xvfb-run -a bundle exec rake spec
#        && xvfb-run -a bundle exec rake spec SPEC=spec/app/models/plan_spec.rb

# cucumber
# rake FEATURE=features/adding_products.feature cucumber

parallel_tests:
  before_command: rm -Rf target
  docker-compose:
    <<: *elasticsearch
    web:
      links:
        - elasticsearch # standard yaml doesn't merge arrays so we have to add this explicitly
      environment:
        - RAILS_ENV=test
      command: >
        bash -c "

        echo 'Bundling gems'
        && bundle install --jobs 4 --retry 3

        && echo 'Clearing logs and tmp dirs'
        && bundle exec rake log:clear tmp:clear

        && echo 'Check and wait for database connection'
        && bundle exec docker-rails-db-check

        && echo 'DB rebuild'
        && bundle exec rake db:rebuild_test[true]

        && echo 'Tests'
        && cd ../..
        && xvfb-run -a bundle exec rake parallel:spec parallel:features
        "

staging:
  docker-compose:
    web:
      environment:
        - RAILS_ENV=staging
      command: >
        bash -c "

        echo 'Bundling gems'
        && bundle install --jobs 4 --retry 3

        && echo 'Clearing logs and tmp dirs'
        && bundle exec rake log:clear tmp:clear

        && echo 'Check and wait for database connection'
        && bundle exec docker-rails-db-check

        && echo 'DB rebuild'
        && bundle exec rake db:migrate

        && echo "Starting app server"
        && bundle exec rails s -p 3000

        && echo 'Setup and start foreman'
        && gem install foreman
        && foreman start
        "

docker-compose:
  web:
    build: .
    working_dir: /project/spec/dummy
    ports:
      - "3000"

    links:
      - db

    volumes:
      - .:/project

    volumes_from:
      # Mount the gems data volume container for cached bundler gem files
      - #{GEMS_VOLUME_NAME}

    # https://docs.docker.com/v1.6/docker-compose/cli/#environment-variables
    environment:
      # Tell bundler where to get the files
      - GEM_HOME=#{GEMS_VOLUME_PATH}

  db:
    # https://github.com/docker-library/docs/tree/master/mysql
    image: library/mysql:5.7.6
    ports:
      - "3306"

    # https://github.com/docker-library/docs/tree/master/mysql#environment-variables
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
@rosskevin
Copy link
Member Author

The one thing I don't like is the redundancy of commands, i.e. they all use the same few setup commands. Sample shows where the elasticsearch is used in dev and test environments, not staging or production. This sample could probably be cleanup up further with some yaml inheritance, but note that lists are not merged in yaml inheritance, only dictionaries, so it's a bit of a trade-off of dry vs readable and extensible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant