From 5e4f3fb730ebb44aa3c674ac27449329c8903b74 Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 10 Nov 2017 17:02:07 +0100 Subject: [PATCH] Fix setup for development with docker (PR #523) --- Dockerfile-dev | 39 ++++++++++++++++ bin/test | 2 +- doc/DEPLOYMENT.md | 60 ------------------------ doc/SETUP_DEVELOPMENT.md | 30 +++++++----- doc/SETUP_DEVELOPMENT_DOCKER.md | 82 +++++++++++++++++++++++++++++++++ doc/SETUP_DOCKER.md | 66 -------------------------- doc/SETUP_PRODUCTION.md | 67 ++++++++++++++++++++++++--- docker-compose-dev.yml | 41 +++++++++++++++++ docker-compose.yml | 44 ------------------ 9 files changed, 241 insertions(+), 190 deletions(-) create mode 100644 Dockerfile-dev delete mode 100644 doc/DEPLOYMENT.md create mode 100644 doc/SETUP_DEVELOPMENT_DOCKER.md delete mode 100644 doc/SETUP_DOCKER.md create mode 100644 docker-compose-dev.yml delete mode 100644 docker-compose.yml diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 00000000..7e784e2a --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,39 @@ +FROM ruby:2.3 + +# Install dependencies +RUN deps='libmagic-dev xvfb qt5-default libqt5webkit5-dev gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x' && \ + apt-get update && \ + apt-get install --no-install-recommends -y $deps && \ + rm -Rf /var/lib/apt/lists/* /var/cache/apt/* + +ENV PORT=3000 \ + SMTP_SERVER_PORT=2525 \ + RAILS_ENV=development \ + RAILS_LOG_TO_STDOUT=true \ + RAILS_SERVE_STATIC_FILES=true \ + \ + BUNDLE_PATH=/home/app/bundle \ + BUNDLE_APP_CONFIG=/home/app/bundle/config + +# Run app and all commands as user 'app'. This avoids changing permissions +# for files in mounted volume. Symlink for similarity with production image. +RUN adduser --gecos GECOS --disabled-password --shell /bin/bash app && \ + ln -s /home/app/src /usr/src/app +USER app + +WORKDIR /home/app/src + +# Copy files needed for installing gem dependencies, and install them. +COPY Gemfile Gemfile.lock ./ +COPY plugins ./plugins +RUN bundle config build.nokogiri "--use-system-libraries" && \ + bundle install -j 4 + +# Copy the application code +COPY . ./ + +EXPOSE 3000 + +# cleanup, and by default start web process from Procfile +ENTRYPOINT ["./docker-entrypoint.sh"] +CMD ["./proc-start", "web"] diff --git a/bin/test b/bin/test index 590b2a13..441ac32f 100755 --- a/bin/test +++ b/bin/test @@ -11,7 +11,7 @@ Xvfb $DISPLAY -nolisten tcp & XVFB_PID=$! # Start tests -rspec +rspec $@ # Cleanup kill $XVFB_PID diff --git a/doc/DEPLOYMENT.md b/doc/DEPLOYMENT.md deleted file mode 100644 index 30f5f4e0..00000000 --- a/doc/DEPLOYMENT.md +++ /dev/null @@ -1,60 +0,0 @@ -# Deployment - -## Docker - -_This section is a work in progress._ - -### Build - -To build the docker image, run: - - docker build --tag foodsoft:dev --rm . - -There is also an [official production docker image](https://hub.docker.com/r/foodcoops/foodsoft/), -which will let you avoid this step. - -### Run (basic) - -You'll need to set at least the following environment variables: - -* `SECRET_KEY_BASE` - random string of 30+ characters, try `rake secret` -* `DATABASE_URL` - pointing to your MySQL installation (`mysql2://user:pass@mysql.host/foodsoftdb?encoding=utf8`) -* `REDIS_URL` - pointing to your Redis instance (`redis://redis.host:6379`) - -You'll also need to supply the Foodsoft configuration file, for example by -mounting it as a volume. Copy `config/app_config.yml.SAMPLE` to `config/app_config.yml` -and customize the settings. - -Then run the webserver, exposing port 3000 on the current host: - - docker run --name foodsoft_web -p 3000 \ - -e SECRET_KEY_BASE -e DATABASE_URL -e REDIS_URL -e RAILS_FORCE_SSL=false \ - -v `pwd`/config/app_config.yml:/usr/src/app/config/app_config.yml:ro \ - foodsoft:dev - -This should get you started. But first you'll need to populate the database: - - docker run --name foodsoft_setup --rm \ - -e SECRET_KEY_BASE -e DATABASE_URL -e REDIS_URL \ - -v `pwd`/config/app_config.yml:/usr/src/app/config/app_config.yml:ro \ - foodsoft:dev bundle exec rake db:setup - -To run the worker (recommended!), supply a different command -(see [Procfile](../Procfile) for other types): - - docker run --name foodsoft_worker \ - -e SECRET_KEY_BASE -e DATABASE_URL -e REDIS_URL \ - -v `pwd`/config/app_config.yml:/usr/src/app/config/app_config.yml:ro \ - foodsoft:dev ./proc-start worker - -To also run the cronjobs, start the previous command but substituting -`mail` with `cron`. That should give you the ingredients for a production-setup. -With the help of a front-end webserver doing ssl, of course. - - -### Run (docker-compose) - -In practice, you'd probably want to use docker-compose. If you know Docker well enough, -you'll have no problem to set this up. For inspiration, look at the -[foodcoops.net production setup](https://github.com/foodcoops/foodcoops.net). - diff --git a/doc/SETUP_DEVELOPMENT.md b/doc/SETUP_DEVELOPMENT.md index 475cb6cc..05b87ca4 100644 --- a/doc/SETUP_DEVELOPMENT.md +++ b/doc/SETUP_DEVELOPMENT.md @@ -1,15 +1,14 @@ -Getting foodsoft running for development -======================================== +# Getting Foodsoft running for development 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). Or if you just want to -run Foodsoft without changing its code, the -[Turnkey Linux Foodsoft appliance](http://www.turnkeylinux.org/foodsoft) may be useful. - +This document describes how to setup Foodsoft for development within your local +system. Another option is to use [docker for development](SETUP_DEVELOPMENT_DOCKER.md). +If instead you just want to run Foodsoft without changing its code, please refer to +[hosting](https://foodcoops.github.io/foodsoft-hosting/) or +[deployment](https://github.com/foodcoops/foodsoft/wiki/Deployment-notes). **System requirements**: [RVM](https://rvm.io/rvm/install), @@ -20,8 +19,7 @@ run Foodsoft without changing its code, the **Optional**: [Redis](http://redis.io/). -Getting started ---------------- +### Getting started 0. Clone the repository from GitHub: @@ -100,9 +98,7 @@ Getting started 9. Have phun! - -Manual configuration --------------------- +### Manual configuration The rake task `foodsoft:setup_development` helps you to setup foodsoft. If you want to have more control, you can do these steps manually as @@ -173,3 +169,13 @@ explained here. From now on you have a smtp server listening on 1025. To see the emails go to http://localhost:1080 + + +## Docker + +To avoid having to go through setting up all dependencies, you can also run Foodsoft +within a docker image. While the default [`Dockerfile`](../Dockerfile) is setup for production, +[`Dockerfile.dev`](../Dockerfile.dev) is meant for development. Even better, you can +use docker-compose (using [`docker-compose.dev.yml`](../docker-compose.dev.yml)) to +setup the whole stack at once. + diff --git a/doc/SETUP_DEVELOPMENT_DOCKER.md b/doc/SETUP_DEVELOPMENT_DOCKER.md new file mode 100644 index 00000000..a88c2aa7 --- /dev/null +++ b/doc/SETUP_DEVELOPMENT_DOCKER.md @@ -0,0 +1,82 @@ +# Foodsoft on Docker + +This document explains setting up Foodsoft with Docker for development. +system. Another option is to run it [within an existing system](SETUP_DEVELOPMENT.md). +If instead you just want to run Foodsoft without changing its code, please refer to +[hosting](https://foodcoops.github.io/foodsoft-hosting/) or +[deployment](https://github.com/foodcoops/foodsoft/wiki/Deployment-notes). + + +## Requirements + +* Docker (=> 1.9.1) +* Docker Compose (=> 1.4) +* 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 + +Then setup foodsoft development (this will take some time, containers needs +to be pulled from docker registry and a lot dependencies needs to be installed) + + docker-compose -f docker-compose-dev.yml run foodsoft rake foodsoft:setup_development + +Do not enable mailcatcher, because this is already included as a docker image. + +To avoid having to pass the `-f` argument all the time, it is recommended to setup +an alias, e.g. by adding the following line to your ~/.bashrc + + alias docker-compose-dev='docker-compose -f docker-compose-dev.yml' + +then reload it by executing `. ~/.bashrc`. Following commands assume this is setup. + + +## Usage + +Start containers (in foreground, stop them with `Ctrl-C`) + + docker-compose-dev up + +Run a rails/rake command + + docker-compose-dev run --rm foodsoft rake db:migrate + +Open a rails console + + docker-compose-dev run --rm foodsoft rails c + +Setup the test database + + docker-compose-dev run --rm foodsoft rake db:setup RAILS_ENV=test DATABASE_URL=mysql2://root:secret@mariadb/test + +Run the tests + + docker-compose-dev run --rm foodsoft ./bin/test + +Jump in a running container for debugging. + + docker exec -ti foodsoft_foodsoft_1 bash + + +## Notes + +### Receiving mails + +Go to [http://localhost:1080](http://localhost:1080) + +### Gemfile updates + +As the gem bundle is stored in a volume, you can run + + docker-compose-dev run foodsoft bundle install + docker-compose-dev restart foodsoft foodsoft_worker + +Do this each time you update your `Gemfile`. + +### Database configuration + +To make this easier we use the environment variable `DATABASE_URL` +(and `TEST_DATABASE_URL` when using the testing script). diff --git a/doc/SETUP_DOCKER.md b/doc/SETUP_DOCKER.md deleted file mode 100644 index 7ecca9d5..00000000 --- a/doc/SETUP_DOCKER.md +++ /dev/null @@ -1,66 +0,0 @@ -# Foodsoft on Docker - -This document explains setting up and using Foodsoft with Docker. - -## Requirements - -* Docker (=> 1.9.1) -* Docker Compose (=> 1.4) -* 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 - -## 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 - -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 - -Jump in a running container for debugging. - - docker exec -ti foodsoft_app_1 bash - -Receiving mails - -Go to http://localhost:1080. - -## Notes - -### Gemfile updates - -As we use a special container (`bundle`, see `docker-compose.yml`) you only -have to run the bundle command as normally: `docker-compose run app bundle` - -### Database configuration - -To make this easier we use the environment variable `DATABASE_URL` -(and `TEST_DATABASE_URL` when using the testing script). diff --git a/doc/SETUP_PRODUCTION.md b/doc/SETUP_PRODUCTION.md index e5fde0c1..25b44a51 100644 --- a/doc/SETUP_PRODUCTION.md +++ b/doc/SETUP_PRODUCTION.md @@ -1,9 +1,62 @@ -Running Foodsoft in production -============================== +# Deployment -As you might have noticed, documentation is scarce and insufficient. If you -intend to deploy foodsoft in production, we would love to guide you through the -process. You can contact the mailing list -[foodsoft-discuss](http://foodsoft.51229.x6.nabble.com/foodsoft-discuss-f5.html). +The recommended way to run Foodsoft in production is using docker. Alternative options are +discussed [in the wiki](https://github.com/foodcoops/foodsoft/wiki/Deployment-notes). If you +have any questions, please contact the mailing list [foodsoft-discuss](http://foodsoft.51229.x6.nabble.com/foodsoft-discuss-f5.html). + +## Docker + +_This section is a work in progress._ + +### Build + +You can use the [official production docker image](https://hub.docker.com/r/foodcoops/foodsoft/). +If you want to build the image yourself instead, run: + + docker build --tag foodsoft:latest --rm . + +### Run (basic) + +You'll need to set at least the following environment variables: + +* `SECRET_KEY_BASE` - random string of 30+ characters, try `rake secret` +* `DATABASE_URL` - pointing to your MySQL installation (`mysql2://user:pass@mysql.host/foodsoftdb?encoding=utf8`) +* `REDIS_URL` - pointing to your Redis instance (`redis://redis.host:6379`) + +You'll also need to supply the Foodsoft configuration file, for example by +mounting it as a volume. Copy `config/app_config.yml.SAMPLE` to `config/app_config.yml` +and customize the settings. + +Then run the webserver, exposing port 3000 on the current host: + + docker run --name foodsoft_web -p 3000 \ + -e SECRET_KEY_BASE -e DATABASE_URL -e REDIS_URL -e RAILS_FORCE_SSL=false \ + -v `pwd`/config/app_config.yml:/usr/src/app/config/app_config.yml:ro \ + foodsoft:latest + +This should get you started. But first you'll need to populate the database: + + docker run --name foodsoft_setup --rm \ + -e SECRET_KEY_BASE -e DATABASE_URL -e REDIS_URL \ + -v `pwd`/config/app_config.yml:/usr/src/app/config/app_config.yml:ro \ + foodsoft:latest bundle exec rake db:setup + +To run the worker (recommended!), supply a different command +(see [Procfile](../Procfile) for other types): + + docker run --name foodsoft_worker \ + -e SECRET_KEY_BASE -e DATABASE_URL -e REDIS_URL \ + -v `pwd`/config/app_config.yml:/usr/src/app/config/app_config.yml:ro \ + foodsoft:latest ./proc-start worker + +To also run the cronjobs, start the previous command but substituting +`mail` with `cron`. That should give you the ingredients for a production-setup. +With the help of a front-end webserver doing ssl, of course. + + +### Run (docker-compose) + +In practice, you'd probably want to use docker-compose. If you know Docker well enough, +you'll have no problem to set this up. For inspiration, look at the +[foodcoops.net production setup](https://github.com/foodcoops/foodcoops.net). -Please see our wiki page: [Deployment notes](https://github.com/foodcoops/foodsoft/wiki/Deployment-notes). diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 00000000..c2ec4cdd --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,41 @@ +version: '2' +services: + + foodsoft: + extends: foodsoft_worker + command: ./proc-start web + ports: + - "3000:3000" + + foodsoft_worker: + build: + context: . + dockerfile: Dockerfile-dev + command: ./proc-start worker + volumes: + - bundle:/home/app/bundle + - .:/home/app/src + environment: + - DATABASE_URL=mysql2://root:secret@mariadb/development?encoding=utf8 + - REDIS_URL=redis://redis:6379 + - QUEUE=foodsoft_notifier + - TEST_DATABASE_URL=mysql2://root:secret@mariadb/test?encoding=utf8 + + mailcatcher: + image: aboutsource/mailcatcher + ports: + - "1080:1080" + + mariadb: + image: mariadb:10.1 + environment: + - MYSQL_ROOT_PASSWORD=secret + volumes: + - mariadb:/var/lib/mysql + + redis: + image: redis:3.2-alpine + +volumes: + bundle: + mariadb: diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 4b1b0968..00000000 --- a/docker-compose.yml +++ /dev/null @@ -1,44 +0,0 @@ -bundle: - image: foodsoft_app - command: /bin/true - volumes: - - /home/app/bundle - -app: &app - build: . - command: bundle exec rails server --binding 0.0.0.0 - volumes_from: - - bundle - volumes: - - .:/home/app/src - ports: - - "3000:3000" - links: - - mysql - - redis - - mailcatcher - environment: - - DATABASE_URL=mysql2://root:secret@mysql/development?encoding=utf8 - - REDIS_URL=redis://redis:6379 - - QUEUE=foodsoft_notifier - - TEST_DATABASE_URL=mysql2://root:secret@mysql/test?encoding=utf8 - -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