From cf811c89f29c2d50cc27d81a9c999c4580a78b30 Mon Sep 17 00:00:00 2001 From: Jesse Horne Date: Tue, 17 Jan 2023 14:10:36 -0500 Subject: [PATCH 1/3] started working on dockerization --- Dockerfile | 11 ++++++ README.md | 15 +++++++ config/database.yml | 6 +++ config/environments/docker.rb | 73 +++++++++++++++++++++++++++++++++++ config/secrets.yml | 3 ++ docker-compose.yml | 20 ++++++++++ script/bootstrap | 4 ++ script/setup | 7 +++- 8 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 config/environments/docker.rb create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..56583524f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +# syntax=docker/dockerfile:1 +FROM ruby:3.1.3 +WORKDIR /crimethinc +COPY . . +RUN apt-get update -qq && apt-get install -y nodejs postgresql-client ruby-full git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev + +RUN gem install bundler --conservative && gem install os && bundle install + +CMD bundle exec rails db:create && bundle exec rails db:migrate && ./script/server + +EXPOSE 3000 \ No newline at end of file diff --git a/README.md b/README.md index 8f557a17c..03f83c778 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,21 @@ Drop the database, rebuild it, and fill it with seed data. *** +## Docker + +### Build it +```sh +docker build -t crimethinc . +``` + +### Run it + +This runs the website rails server and postgres server in two separate containers. This is the recommended way to do things instead of running two seperate services in a single container. + +```sh +docker compose up +``` + ## How to guides… - [Add a new language to `/languages`](/blob/main/docs/languages.md) diff --git a/config/database.yml b/config/database.yml index 11a394fd7..764c86de0 100644 --- a/config/database.yml +++ b/config/database.yml @@ -3,6 +3,12 @@ default: &default encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> +docker: + <<: *default + host: host.docker.internal + username: postgres + database: crimethinc + development: <<: *default host: localhost diff --git a/config/environments/docker.rb b/config/environments/docker.rb new file mode 100644 index 000000000..e653598a0 --- /dev/null +++ b/config/environments/docker.rb @@ -0,0 +1,73 @@ +require 'active_support/core_ext/integer/time' + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded any time + # it changes. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable server timing + config.server_timing = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join('tmp/caching-dev.txt').exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # Suppress logger output for asset requests. + config.assets.quiet = true + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true + + # Uncomment if you wish to allow Action Cable access from any origin. + # config.action_cable.disable_request_forgery_protection = true + + # For using #url_for et al in non-views/helpers + Rails.application.routes.default_url_options[:host] = 'localhost:3000' +end diff --git a/config/secrets.yml b/config/secrets.yml index 7754895ff..a2c5f33e5 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -10,6 +10,9 @@ # Make sure the secrets in this file are kept private # if you're sharing your code publicly. +docker: + secret_key_base: 806f80029116e5db4d4c906d25b9050fd7b844aaf23756b39e50694556ce3f0143783eac22cdca409f6983223ddd631a5047b1dacc06b10544b663ebca818c5d + development: secret_key_base: 3274374298988534e80a29e9804457a23549d92d44e8ac094e2b12af80482f0c518fb8190ec395b7d5d9041076aa8198bb9692eb0145b44c15ce73eaeb0df46e diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..3e6d0a595 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +version: '3' +services: + db: + image: postgres + volumes: + - ./tmp/db:/var/lib/postgresql/data + environment: + - POSTGRES_HOST_AUTH_METHOD=trust + ports: + - 5432:5432 + web: + image: crimethinc:latest + ports: + - "3000:3000" + links: + - db + environment: + - RAILS_ENV=docker + extra_hosts: + - "host.docker.internal:host-gateway" diff --git a/script/bootstrap b/script/bootstrap index 9c383bbf0..6d469f614 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -47,5 +47,9 @@ echo '==> Installing bundler…' gem install bundler echo +echo '==> Installing os...' +gem install os +echo + echo '==> All done! Your system is now bootstrapped.' echo '==> Next up: ./script/setup' diff --git a/script/setup b/script/setup index 09ce1c5b4..f4fcc956e 100755 --- a/script/setup +++ b/script/setup @@ -5,6 +5,7 @@ require 'pathname' require 'fileutils' +require 'os' include FileUtils # path to your application root. @@ -19,8 +20,10 @@ chdir APP_ROOT do system! 'gem install bundler --conservative' system('bundle check') || system!('bundle install') - puts '==> Starting postgresql' - system! 'brew services start postgresql' + if OS.mac? then + puts '==> Starting postgresql' + system! 'brew services start postgresql' + end puts '==> Preparing database' system! 'bundle exec rails db:create' From e2be08c92712c148a4e3e78227817e6d16e64bf6 Mon Sep 17 00:00:00 2001 From: Jesse Horne Date: Sun, 22 Jan 2023 00:17:23 -0500 Subject: [PATCH 2/3] removed unnecessary dependencies and cleaned up CMD with entrypoint as suggested by just1602 --- Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 56583524f..f9f3d7ace 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,10 +2,10 @@ FROM ruby:3.1.3 WORKDIR /crimethinc COPY . . -RUN apt-get update -qq && apt-get install -y nodejs postgresql-client ruby-full git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev +RUN apt-get update -qq && apt-get install -y nodejs postgresql-client RUN gem install bundler --conservative && gem install os && bundle install -CMD bundle exec rails db:create && bundle exec rails db:migrate && ./script/server +ENTRYPOINT ["rails", "server"] -EXPOSE 3000 \ No newline at end of file +EXPOSE 3000 From 0b3112d447fa7678d4bf88c5c485e7320ae14fbe Mon Sep 17 00:00:00 2001 From: Jesse Horne Date: Sun, 22 Jan 2023 00:32:46 -0500 Subject: [PATCH 3/3] added way to specify environment --- config/database.yml | 6 --- config/environments/docker.rb | 73 ----------------------------------- docker-compose.yml | 2 +- 3 files changed, 1 insertion(+), 80 deletions(-) delete mode 100644 config/environments/docker.rb diff --git a/config/database.yml b/config/database.yml index 764c86de0..11a394fd7 100644 --- a/config/database.yml +++ b/config/database.yml @@ -3,12 +3,6 @@ default: &default encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> -docker: - <<: *default - host: host.docker.internal - username: postgres - database: crimethinc - development: <<: *default host: localhost diff --git a/config/environments/docker.rb b/config/environments/docker.rb deleted file mode 100644 index e653598a0..000000000 --- a/config/environments/docker.rb +++ /dev/null @@ -1,73 +0,0 @@ -require 'active_support/core_ext/integer/time' - -Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. - - # In the development environment your application's code is reloaded any time - # it changes. This slows down response time but is perfect for development - # since you don't have to restart the web server when you make code changes. - config.cache_classes = false - - # Do not eager load code on boot. - config.eager_load = false - - # Show full error reports. - config.consider_all_requests_local = true - - # Enable server timing - config.server_timing = true - - # Enable/disable caching. By default caching is disabled. - # Run rails dev:cache to toggle caching. - if Rails.root.join('tmp/caching-dev.txt').exist? - config.action_controller.perform_caching = true - config.action_controller.enable_fragment_cache_logging = true - - config.cache_store = :memory_store - config.public_file_server.headers = { - 'Cache-Control' => "public, max-age=#{2.days.to_i}" - } - else - config.action_controller.perform_caching = false - - config.cache_store = :null_store - end - - # Store uploaded files on the local file system (see config/storage.yml for options). - config.active_storage.service = :local - - # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false - - config.action_mailer.perform_caching = false - - # Print deprecation notices to the Rails logger. - config.active_support.deprecation = :log - - # Raise exceptions for disallowed deprecations. - config.active_support.disallowed_deprecation = :raise - - # Tell Active Support which deprecation messages to disallow. - config.active_support.disallowed_deprecation_warnings = [] - - # Raise an error on page load if there are pending migrations. - config.active_record.migration_error = :page_load - - # Highlight code that triggered database queries in logs. - config.active_record.verbose_query_logs = true - - # Suppress logger output for asset requests. - config.assets.quiet = true - - # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true - - # Annotate rendered view with file names. - # config.action_view.annotate_rendered_view_with_filenames = true - - # Uncomment if you wish to allow Action Cable access from any origin. - # config.action_cable.disable_request_forgery_protection = true - - # For using #url_for et al in non-views/helpers - Rails.application.routes.default_url_options[:host] = 'localhost:3000' -end diff --git a/docker-compose.yml b/docker-compose.yml index 3e6d0a595..86ead4289 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,6 @@ services: links: - db environment: - - RAILS_ENV=docker + - RAILS_ENV=${RAILS_ENV} extra_hosts: - "host.docker.internal:host-gateway"