diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..144c8ce4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +Dockerfile +.git +.github +public \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..a802e597 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM ruby:2 + +RUN apt-get update +RUN apt-get install -y rbenv libxml2 libxslt-dev openssl nodejs postgresql-client +RUN gem install bundler + +#RUN mkdir /civic-server +#RUN cd /civic-server && bundle install + +COPY Gemfile /civic-server/Gemfile +COPY Gemfile.lock /civic-server/Gemfile.lock + +RUN cd /civic-server && bundle update --bundler && rbenv rehash && bundle install + +COPY $PWD /civic-server +WORKDIR /civic-server + +ENTRYPOINT ["/civic-server/docker-entrypoint.sh"] diff --git a/Gemfile b/Gemfile index 1d62d385..acbccbbf 100644 --- a/Gemfile +++ b/Gemfile @@ -62,3 +62,7 @@ group :development do gem 'capistrano-rbenv', '~> 2.0' gem 'capistrano-passenger', '~> 0.2.0' end + +group :production do + gem 'puma', '~> 5.6.4' +end diff --git a/config/database.yml b/config/database.yml index 33ea5846..30515af6 100644 --- a/config/database.yml +++ b/config/database.yml @@ -2,7 +2,9 @@ default: &default adapter: postgresql pool: 5 encoding: unicode - host: localhost + host: <%= ENV['DATABASE_HOST'] %> + username: <%= ENV['DATABASE_USER'] %> + password: <%= ENV['DATABASE_PASSWORD'] %> development: <<: *default diff --git a/config/environments/production.rb b/config/environments/production.rb index 589211f4..1251b886 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -36,12 +36,15 @@ # `config.assets.precompile` has moved to config/initializers/assets.rb + #run background tasks syncronously + config.active_job.queue_adapter = :inline + # Specifies the header that your server uses for sending files. # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. - config.force_ssl = true + #config.force_ssl = true # Set to :debug to see everything in the log. config.log_level = :info diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..5c8b1774 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3' + +services: + civic: + build: . + ports: + - "3000:3000" + environment: + RAILS_ENV: production + SECRET_KEY_BASE: production_key_eretred547568 + DATABASE_HOST: db + DATABASE_USER: postgres + DATABASE_PASSWORD: psw + depends_on: + - db + db: + image: postgres:13.5 + ports: + - "5432:5432" + environment: + POSTGRES_PASSWORD: psw + healthcheck: + test: "PASSWORD=psw pg_isready -U postgres" + interval: 10s + timeout: 5s + retries: 5 diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 00000000..ea9ee438 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +set -e +set -u # unset variables throw error +set -o pipefail # pipes fail when partial command fails + +bundle update --bundler + +# Note: make sure env $DATABASE_* items are set! + +# Attempt connect to db and if +# it fails, then assume db does not exist and +# db creation should be executed: +if PGPASSWORD=$DATABASE_PASSWORD psql -lqt -U$DATABASE_USER -h$DATABASE_HOST | cut -d \| -f 1 | grep -q civic; then + echo "civic database already exists. Skipping database initialisation." +else + echo "Creating civic database" + rake db:create + + echo "Initializing civic database" + rake db:structure:load + + echo "Loading a recent database backup" + # unpack ./db/data.sql.gz (required before loading data dump): + gunzip ./db/data.sql.gz + # load the sanitized version of a recent database backup found in ./db: + rake civic:load[force] + echo "Done" +fi + +echo "Starting server now. You can test it with e.g. http://localhost:3000/api/genes/3845?identifier_type=entrez_id" +rails server -b 0.0.0.0