From 3f22c8a03610bf7a293664375ada4b7319f1c24b Mon Sep 17 00:00:00 2001 From: Jyri-Petteri Paloposki Date: Sun, 19 Jul 2020 16:12:57 +0300 Subject: [PATCH] Better Docker config: Use entrypoint to setup the environment, run in production mode by default, defaults for all config values --- Dockerfile | 8 +++-- config/database.docker.yml | 60 +++++++++++++------------------------- config/site.docker.yml | 6 ---- config/site.yml.tmpl | 6 ---- docker-entrypoint.sh | 17 +++++++++++ docker-startserver.sh | 4 --- 6 files changed, 42 insertions(+), 59 deletions(-) create mode 100755 docker-entrypoint.sh delete mode 100755 docker-startserver.sh diff --git a/Dockerfile b/Dockerfile index f1f36a06..08e473f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,9 +17,11 @@ COPY . /app/ COPY config/database.docker.yml /app/config/database.yml COPY config/site.docker.yml /app/config/site.yml -COPY docker-startserver.sh / +RUN RAILS_ENV=production bundle exec rake assets:precompile + +COPY docker-entrypoint.sh / +ENTRYPOINT ["./docker-entrypoint.sh"] EXPOSE 3000 -#CMD ["rails", "server", "-e", "production", "-b", "0.0.0.0"] -CMD ["./docker-startserver.sh"] +CMD ["rails", "server", "-b", "0.0.0.0"] diff --git a/config/database.docker.yml b/config/database.docker.yml index 790d8f77..cabb221f 100644 --- a/config/database.docker.yml +++ b/config/database.docker.yml @@ -1,44 +1,24 @@ -#development: -# adapter: mysql2 -# database: tracks_dev -# # set this if you are storing utf8 in your mysql database to handle strings -# # like "Réné". Not needed for sqlite. For PostgreSQL use encoding: unicode -# # encoding: utf8 -# host: docker -# port: 3307 -# username: tracks_dev -# password: FqUKMWPz5mh8UPhypZvq - -#development: -# adapter: postgresql -# database: tracks_dev -# # set this if you are storing utf8 in your mysql database to handle strings -# # like "Réné". Not needed for sqlite. For PostgreSQL use encoding: unicode -# # encoding: utf8 -# host: docker -# port: 5432 -# username: tracks_dev -# password: password - -#development: -# adapter: sqlite3 -# database: db.sqlite - -#test: -# adapter: mysql2 -# database: tracks_test -# # set this if you are storing utf8 in your mysql database to handle strings -# # like "Réné". Not needed for sqlite. For PostgreSQL use encoding: unicode -# # encoding: utf8 -# host: docker -# port: 3307 -# username: tracks_tst -# password: 9rMNV4y6RVcqmJTo2QoR - -# Production config is disabled by default -# -development: +test: adapter: <%= ENV.fetch('DATABASE_TYPE') %> + encoding: <%= ENV.fetch('DATABASE_ENCODING') %> + database: <%= ENV.fetch('DATABASE_NAME') %> + host: <%= ENV.fetch('DATABASE_HOST') %> + port: <%= ENV.fetch('DATABASE_PORT') %> + username: <%= ENV.fetch('DATABASE_USERNAME') %> + password: <%= ENV.fetch('DATABASE_PASSWORD') %> + +development: + adapter: <%= ENV.fetch('DATABASE_TYPE') %> + encoding: <%= ENV.fetch('DATABASE_ENCODING') %> + database: <%= ENV.fetch('DATABASE_NAME') %> + host: <%= ENV.fetch('DATABASE_HOST') %> + port: <%= ENV.fetch('DATABASE_PORT') %> + username: <%= ENV.fetch('DATABASE_USERNAME') %> + password: <%= ENV.fetch('DATABASE_PASSWORD') %> + +production: + adapter: <%= ENV.fetch('DATABASE_TYPE') %> + encoding: <%= ENV.fetch('DATABASE_ENCODING') %> database: <%= ENV.fetch('DATABASE_NAME') %> host: <%= ENV.fetch('DATABASE_HOST') %> port: <%= ENV.fetch('DATABASE_PORT') %> diff --git a/config/site.docker.yml b/config/site.docker.yml index 02c8b373..0b1a04d3 100644 --- a/config/site.docker.yml +++ b/config/site.docker.yml @@ -25,12 +25,6 @@ secret_token: "secret" # Set to true when your application is running with https force_ssl: false -# Configure how static assets (images, stylesheets, etc.) will be served. -# The best practice is to have a proxying web server such as Apache or Nginx -# serve static assets (images, stylesheets, javascript) for you. Change -# this to 'true' if you want Rails to be responsible for serving the static assets -# serve_static_assets: false - # Uncomment if you want to dispatch todos that come from email based on the To: # address rather than the From: address. # email_dispatch: 'to' diff --git a/config/site.yml.tmpl b/config/site.yml.tmpl index 362c8ef6..29c53704 100644 --- a/config/site.yml.tmpl +++ b/config/site.yml.tmpl @@ -28,12 +28,6 @@ secret_token: "change-me" # Set to true when your application is running with https force_ssl: false -# Configure how static assets (images, stylesheets, etc.) will be served. -# The best practice is to have a proxying web server such as Apache or Nginx -# serve static assets (images, stylesheets, javascript) for you. Change -# this to 'true' if you want Rails to be responsible for serving the static assets -# serve_static_assets: false - # Uncomment if you want to dispatch todos that come from email based on the To: # address rather than the From: address. # email_dispatch: 'to' diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 00000000..68931845 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +export RAILS_ENV=${RAILS_ENV:-production} +export DATABASE_NAME=${DATABASE_NAME:-tracks} +export DATABASE_HOST=${DATABASE_HOST:-db} +export DATABASE_PORT=${DATABASE_PORT:-3306} +export DATABASE_USERNAME=${DATABASE_USERNAME:-tracks} +export DATABASE_PASSWORD=${DATABASE_PASSWORD:-password} +export DATABASE_TYPE=${DATABASE_TYPE:-mysql2} +export DATABASE_ENCODING=${DATABASE_ENCODING:-utf8} + +export RAILS_SERVE_STATIC_FILES=TRUE +export RAILS_LOG_TO_STDOUT=TRUE + +rails db:migrate + +exec "$@" diff --git a/docker-startserver.sh b/docker-startserver.sh deleted file mode 100755 index dfe1e6e5..00000000 --- a/docker-startserver.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -rails db:migrate -rails server -b 0.0.0.0