From 83dd18eaa769cdd1446a01a14d5c701bd7a00873 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 26 Apr 2015 15:20:05 +0200 Subject: [PATCH 1/6] Added Dockersupport. See README_DOCKER.md for further instructions. --- .dockerignore | 10 ++++ Dockerfile | 40 +++++++++++++ README_DOCKER.md | 68 +++++++++++++++++++++++ bin/test | 12 ++++ config/environments/development.rb.SAMPLE | 2 +- config/initializers/resque.rb | 2 + 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 README_DOCKER.md create mode 100755 bin/test create mode 100644 config/initializers/resque.rb diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..47a85ca1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.idea +.sass-cache +log +public/assets +public/system +tmp/* +vendor/bundle +coverage +*.sql* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..f337aafd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +FROM ruby:2.1-slim + +RUN apt-get update && \ + apt-get install --no-install-recommends -y \ + mysql-client \ + git \ + make \ + gcc \ + g++ \ + patch \ + libsqlite3-dev \ + libv8-dev \ + libmysqlclient-dev \ + libxml2-dev \ + libxslt1-dev \ + libffi-dev \ + libreadline-dev \ + xvfb \ + iceweasel && \ + rm -rf /var/lib/apt/lists/* && \ + apt-get clean + +ENV WORKDIR /usr/src/app + +RUN mkdir -p $WORKDIR +WORKDIR $WORKDIR + +# Copy plugins before the rest to allow bundler loading gemspecs +# TODO: Move plugins to gems and add them to Gemfile instead +COPY plugins $WORKDIR/plugins + +COPY Gemfile $WORKDIR/ +COPY Gemfile.lock $WORKDIR/ +RUN bundle install --jobs 4 + +COPY . $WORKDIR + +EXPOSE 3000 + +CMD ["rails", "server"] diff --git a/README_DOCKER.md b/README_DOCKER.md new file mode 100644 index 00000000..25ae242d --- /dev/null +++ b/README_DOCKER.md @@ -0,0 +1,68 @@ +# Foodsoft on Docker + +This document explains setup and using docker the foodsoft with docker. + +## Requirements + +* Docker (=> 1.5) +* Docker Compose (=> 1.1) +* Nothing more, no ruby, mysql, redis etc! + +For installing instructions see https://docs.docker.com/installation/. +Docker runs on every modern linux kernel, but also with a little help on MacOS +and Windows! + +## Setup + +Create docker volume for mysql data: + + mkdir -p ~/.docker-volumes/foodsoft/mysql + +Setup foodsoft development data: (This will take some time, containers needs +to be pulled from docker registry and a lot dependencies needs to be installed.) + + docker-compose run app rake foodsoft:setup_development + +TODO: Right know this is not gonna work because of the new database +configuration via ENV variable. See Notes below. + +## Usage + +Start containers (in foreground, stop them with `CTRL-C`) + + docker-compose up + +Run a rails/rake command + + docker-compose run app rake db:migrate + +Open a rails console + + docker-compose run app rails c + +Run the tests + + docker-compose run app ./bin/test + +Jump in a running container for debugging. + + docker exec -ti foodsoft_app_1 bash + +## Notes + +### Gemfile updates + +This is bit tricky, as the gemfiles are stored in the container and so we have +to rebuild the container each time we change the Gemfile. To avoid installing +all the gems from scratch we can use this workaround: + +```bash +docker-compose run app bundle update rails # Update Gemfile.lock file +docker ps -a # Look for the last exited container and grab the Container ID +docker commit -m "Updated rails" foodsoft_app +``` + +### Database configuration + +TO make thins easier we use the ENV Variable DATABASE_URL. But to make this +work, the shouldn't be any config/database.yml file! diff --git a/bin/test b/bin/test new file mode 100755 index 00000000..15f5797b --- /dev/null +++ b/bin/test @@ -0,0 +1,12 @@ +#!/bin/sh + +# Set test variables +export DATABASE_URL=mysql2://root:secret@mysql/test +export RAILS_ENV=test + +# Start virtuals X environment to allow integration testing via firefox/iceweasel +export DISPLAY=:99 +Xvfb :99 & + +# Start tests +rspec diff --git a/config/environments/development.rb.SAMPLE b/config/environments/development.rb.SAMPLE index 0b84aec8..7d04751a 100644 --- a/config/environments/development.rb.SAMPLE +++ b/config/environments/development.rb.SAMPLE @@ -39,7 +39,7 @@ Foodsoft::Application.configure do # Mailcatcher config, start mailcatcher from console with 'mailcatcher' # Mailcatcher can be installed by gem install mailcatcher config.action_mailer.delivery_method = :smtp - config.action_mailer.smtp_settings = { address: "localhost", port: 1025 } + config.action_mailer.smtp_settings = { address: "mailcatcher", port: 1025 } # Run resque tasks as part of the main app (not recommended for production) Resque.inline = true diff --git a/config/initializers/resque.rb b/config/initializers/resque.rb new file mode 100644 index 00000000..6301afa4 --- /dev/null +++ b/config/initializers/resque.rb @@ -0,0 +1,2 @@ +# Initializer to configure resque daemon +Resque.redis = ENV.fetch("REDIS_URL", "redis://localhost:6379") From f9cdd7f8bafe77012dfd25b32f13f6a1f6d99f5c Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 26 Apr 2015 15:23:45 +0200 Subject: [PATCH 2/6] Added docker-compose.yml --- docker-compose.yml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..97a58207 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +app: &app + build: . + command: bundle exec rails s + volumes: + - .:/usr/src/app + ports: + - "3000:3000" + links: + - mysql + - redis + - mailcatcher + environment: + - DATABASE_URL=mysql2://root:secret@mysql/development + - REDIS_URL=redis://redis:6379 + - QUEUE=foodsoft_notifier + +resque: + <<: *app + command: rake resque:work + ports: [] + +mailcatcher: + image: aboutsource/mailcatcher + ports: + - "1080:1080" + +mysql: + image: mysql:5.5 + volumes: + - ~/.docker-volumes/foodsoft/mysql:/var/lib/mysql + environment: + - MYSQL_ROOT_PASSWORD=secret + +redis: + image: redis:2.8 From 27050f8b02a4a75583a94fef37b62ac41f81cdf3 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 2 May 2015 13:47:52 +0200 Subject: [PATCH 3/6] Make rake setup task work with Docker --- .dockerignore | 1 + lib/tasks/foodsoft_setup.rake | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/.dockerignore b/.dockerignore index 47a85ca1..dae6acc5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,6 +2,7 @@ .idea .sass-cache log +config/database.yml public/assets public/system tmp/* diff --git a/lib/tasks/foodsoft_setup.rake b/lib/tasks/foodsoft_setup.rake index f6c0bd65..1b07ab1d 100644 --- a/lib/tasks/foodsoft_setup.rake +++ b/lib/tasks/foodsoft_setup.rake @@ -58,6 +58,10 @@ end def setup_database file = 'config/database.yml' + if ENV['DATABASE_URL'] + puts blue "DATABASE_URL found, please remember to also set it when running Foodsoft" + return nil + end return nil if skip?(file) database = ask("What kind of database do you use?\nOptions:\n(1) MySQL\n(2) SQLite", ["1","2"]) @@ -110,6 +114,7 @@ def setup_secret_token end def start_mailcatcher + return nil if ENV['MAILCATCHER_PORT'] # skip when it has an existing Docker container mailcatcher = ask("Do you want to start mailcatcher?\nOptions:\n(y) Yes\n(n) No", ["y","n"]) if mailcatcher === "y" puts yellow "Starting mailcatcher at http://localhost:1080..." From c0ae816c77f02415b85ab0cca73e3c4cdfe31283 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 2 May 2015 14:22:39 +0200 Subject: [PATCH 4/6] Improve testing script --- bin/test | 11 ++++++++--- docker-compose.yml | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/bin/test b/bin/test index 15f5797b..590b2a13 100755 --- a/bin/test +++ b/bin/test @@ -1,12 +1,17 @@ #!/bin/sh -# Set test variables -export DATABASE_URL=mysql2://root:secret@mysql/test +# We use TEST_DATABASE_URL to make sure we don't accidentaly overwrite dev/prod db +unset DATABASE_URL; export DATABASE_URL +[ "$TEST_DATABASE_URL" ] && export DATABASE_URL="$TEST_DATABASE_URL" export RAILS_ENV=test # Start virtuals X environment to allow integration testing via firefox/iceweasel export DISPLAY=:99 -Xvfb :99 & +Xvfb $DISPLAY -nolisten tcp & +XVFB_PID=$! # Start tests rspec + +# Cleanup +kill $XVFB_PID diff --git a/docker-compose.yml b/docker-compose.yml index 97a58207..6078cd0b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,7 @@ app: &app - DATABASE_URL=mysql2://root:secret@mysql/development - REDIS_URL=redis://redis:6379 - QUEUE=foodsoft_notifier + - TEST_DATABASE_URL=mysql2://root:secret@mysql/test resque: <<: *app From ca1abefeb68b8c70cfb7b4405b761e078644d082 Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 2 May 2015 14:26:15 +0200 Subject: [PATCH 5/6] Update Docker docs --- doc/SETUP_DEVELOPMENT.md | 4 ++++ README_DOCKER.md => doc/SETUP_DOCKER.md | 13 +++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) rename README_DOCKER.md => doc/SETUP_DOCKER.md (82%) diff --git a/doc/SETUP_DEVELOPMENT.md b/doc/SETUP_DEVELOPMENT.md index c011f5db..1e507c32 100644 --- a/doc/SETUP_DEVELOPMENT.md +++ b/doc/SETUP_DEVELOPMENT.md @@ -5,6 +5,10 @@ Gratulations, if you read this file locally, you have successfully cloned the foodsoft project from the git repository. Now you are only a few steps away from trying it out and then jumping into development. +This document describes how to setup Foodsoft for development on your local system. +Alternatively, you [run Foodsoft using Docker](SETUP_DOCKER.md). + + **System requirements**: [RVM](https://rvm.io/rvm/install), [Ruby 1.9.3+](https://www.ruby-lang.org/en/downloads/), diff --git a/README_DOCKER.md b/doc/SETUP_DOCKER.md similarity index 82% rename from README_DOCKER.md rename to doc/SETUP_DOCKER.md index 25ae242d..f886e316 100644 --- a/README_DOCKER.md +++ b/doc/SETUP_DOCKER.md @@ -1,6 +1,6 @@ # Foodsoft on Docker -This document explains setup and using docker the foodsoft with docker. +This document explains setting up and using Foodsoft with Docker. ## Requirements @@ -23,9 +23,6 @@ to be pulled from docker registry and a lot dependencies needs to be installed.) docker-compose run app rake foodsoft:setup_development -TODO: Right know this is not gonna work because of the new database -configuration via ENV variable. See Notes below. - ## Usage Start containers (in foreground, stop them with `CTRL-C`) @@ -40,6 +37,10 @@ Open a rails console docker-compose run app rails c +Setup the test database + + docker-compose run app rake db:setup RAILS_ENV=test DATABASE_URL=mysql2://root:secret@mysql/test + Run the tests docker-compose run app ./bin/test @@ -64,5 +65,5 @@ docker commit -m "Updated rails" foodsoft_app ### Database configuration -TO make thins easier we use the ENV Variable DATABASE_URL. But to make this -work, the shouldn't be any config/database.yml file! +To make this easier we use the environment variable `DATABASE_URL` +(and `TEST_DATABASE_URL` when using the testing script). From 5e2ff4158e65787e9b95bd4cd2769202ec75bcec Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 8 May 2015 13:05:48 +0200 Subject: [PATCH 6/6] Make mailcatcher & redis work with both Docker and local development --- config/environments/development.rb.SAMPLE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/environments/development.rb.SAMPLE b/config/environments/development.rb.SAMPLE index 7d04751a..6aad5be9 100644 --- a/config/environments/development.rb.SAMPLE +++ b/config/environments/development.rb.SAMPLE @@ -39,8 +39,8 @@ Foodsoft::Application.configure do # Mailcatcher config, start mailcatcher from console with 'mailcatcher' # Mailcatcher can be installed by gem install mailcatcher config.action_mailer.delivery_method = :smtp - config.action_mailer.smtp_settings = { address: "mailcatcher", port: 1025 } + config.action_mailer.smtp_settings = { address: ENV.fetch('MAILCATCHER_PORT_1025_TCP_ADDR', 'localhost'), port: ENV.fetch('MAILCATCHER_PORT_1025_TCP_PORT', 1025) } # Run resque tasks as part of the main app (not recommended for production) - Resque.inline = true + Resque.inline = true unless ENV['REDIS_URL'] end