From cc3399c363c252c68e693e1eb596a4baaba1c19d Mon Sep 17 00:00:00 2001 From: Chris Veleris Date: Tue, 14 Nov 2023 14:02:46 +0200 Subject: [PATCH] Add docker instructions --- .dockerignore | 5 +++++ Dockerfile | 22 ++++++++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++++++ app.rb | 15 +++++++++++---- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a833ad9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +*.sqlite3 +*.sqlite3-shm +*.sqlite3-wal +certs/ +.DS_Store \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..831ed87 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM ruby:3.2.2-slim + +RUN apt-get update -qq && apt-get install -y build-essential libsqlite3-dev openssl + +WORKDIR /usr/src/app + +COPY Gemfile* ./ + +RUN bundle config set without 'development test' && bundle install + +COPY . . + +EXPOSE 9292 + +ENV RACK_ENV=production + +RUN mkdir -p certs && \ + openssl req -x509 -newkey rsa:4096 -keyout certs/server.key -out certs/server.crt -days 365 -nodes -subj '/CN=localhost' + +RUN rake db:migrate + +CMD ["puma", "-C", "app/config/puma.rb"] diff --git a/README.md b/README.md index 75d377f..bc44501 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,36 @@ To start the application, run the following command in your terminal: puma -C app/config/puma.rb ``` +### Docker + +In order to start the docker container you need 3 enviromental variables: + +```bash +TUDUDI_USER_EMAIL +TUDUDI_USER_PASSWORD +TUDUDI_SESSION_SECRET +``` + +**PLEASE NOTE:** I am generating a new SSL certificate inside the Dockerfile. There will be an option to create and link an externally generated one in the future - at this stage I am doing this for simplicity. + +1. Create a random session secret and copy the hash to use it as a `TUDUDI_SESSION_SECRET`: + ```bash + openssl rand -hex 64 + ``` + +2. Run the docker command with your produced hash at the previous step: + ```bash + docker run -e TUDUDI_USER_EMAIL=myemail@example.com + -e TUDUDI_USER_PASSWORD=mysecurepassword + -e TUDUDI_SESSION_SECRET=3337c138d17ac7acefa412e5db0d7ef6540905b198cc28c5bf0d11e48807a71bdfe48d82ed0a0a6eb667c937cbdd1db3e1e6073b3148bff37f73cc6398a39671 + -v ~/tududi_db:/db + -p 9292:9292 + -d tududi + ``` + +3. Navigate to https://localhost:9292 and fill in your email and password. +4. Enjoy + ### Testing To run tests: diff --git a/app.rb b/app.rb index 38a3ba4..e51515d 100644 --- a/app.rb +++ b/app.rb @@ -25,9 +25,16 @@ set :public_folder, 'public' configure do enable :sessions set :sessions, httponly: true, secure: production?, expire_after: 2_592_000 - set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) } - set :session_secret, - '740cca863278d6cbacb64dbdd41cfdb1598e8208ce9b9d29b0a1e7c1e1367ca1241d8048849ee88784731d43879c94f5b9f0a639135828d590a447acb2d98e1c' + set :session_secret, ENV.fetch('TUDUDI_SESSION_SECRET') { SecureRandom.hex(64) } + + # Auto-create user if not exists + if ENV['TUDUDI_USER_EMAIL'] && ENV['TUDUDI_USER_PASSWORD'] + user = User.find_or_initialize_by(email: ENV['TUDUDI_USER_EMAIL']) + if user.new_record? + user.password = ENV['TUDUDI_USER_PASSWORD'] + user.save + end + end end use Rack::Protection @@ -85,4 +92,4 @@ end get '/inbox' do erb :inbox -end \ No newline at end of file +end