From c9dd4fc8f07ece2dd4a38c48e4823b11c787019c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Thu, 28 Jul 2022 09:37:28 -0300 Subject: [PATCH] add docker setup --- Dockerfile | 58 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 28 ++++------------------ docker-compose.yml | 44 +++++++++++++++++++++++++++++++++++ scripts/setup | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 23 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100755 scripts/setup diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5854f9e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,58 @@ +FROM ruby:3.1.2-alpine + +# App variables +ENV APP_PATH /progress-test/ +ENV APP_USER progress-test +ENV APP_USER_HOME /home/progress-test +# Bundler variables +ENV BUNDLE_PATH $APP_USER_HOME +ENV GEM_HOME $APP_USER_HOME/bundle +ENV BUNDLE_BIN $GEM_HOME/bin +# Rails variables +ENV RAILS_LOG_TO_STDOUT=true +# System variables +ENV PATH $PATH:$BUNDLE_BIN +# Timezone +ENV TZ=America/Sao_Paulo + +# Puma +EXPOSE 3000 + +# UID of the user that will be created +ARG UID + +# Validating if UID argument was provided +RUN : "${UID:?You must provide a UID argument when building this image.}" + +# Creating an user so we don't run everything as root +RUN adduser -h $APP_USER_HOME -D -u $UID $APP_USER + +# cd to $APP_PATH +WORKDIR $APP_PATH + +COPY Gemfile* package.json yarn.lock $APP_PATH + +# Installing libraries and gems +# The package shared-mime-info is required in a Rails dependency +# shared-mime-info ref: https://vtex.slack.com/archives/CE16Q9XRT/p1616716948056900 +RUN apk update && \ + apk add --no-cache \ + git \ + build-base \ + postgresql-dev \ + tzdata \ + nodejs \ + yarn \ + less \ + graphviz \ + shared-mime-info && \ + bundle config jobs $(nproc --all) && \ + bundle install && \ + yarn && \ + chown -R $APP_USER:$APP_USER $APP_USER_HOME && \ + chown -R $APP_USER:$APP_USER $APP_PATH && \ + rm -r /var/cache/apk/* + +COPY --chown=progress-test . $APP_PATH + +USER $APP_USER diff --git a/README.md b/README.md index 7db80e4..723d87c 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,6 @@ -# README +# Requisitos +- Docker +- docker-compose -This README would normally document whatever steps are necessary to get the -application up and running. - -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... +# Setup +- Executar no terminal: './scripts/setup' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5febea0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,44 @@ +version: "3.6" + +x-rails: &rails + image: progress-test + volumes: + - .:/progress-test:delegated + - gems:/home/progress-test:delegated + environment: + - DISABLE_SPRING=1 + - DATABASE_URL=postgres://postgres:postgres@db:5432 + depends_on: + - db + +services: + web: + <<: *rails + container_name: progress-test-web + command: > + sh -c ' + bundle check || bundle install && + rm -f tmp/pids/server.pid && + rails s -b 0.0.0.0' + ports: + - "3000:3000" + + db: + image: postgres:11.3-alpine + container_name: progress-test-db + volumes: + - pg-data:/var/lib/postgresql/data:delegated + - ./scripts:/scripts:delegated + - ./tmp:/tmp:delegated + ports: + - "5435:5432" + + gems: + image: busybox:latest + container_name: progress-test-gems + volumes: + - /gems + +volumes: + gems: + pg-data: diff --git a/scripts/setup b/scripts/setup new file mode 100755 index 0000000..7d07830 --- /dev/null +++ b/scripts/setup @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +set -e + +cd $(dirname $(dirname $0)) + +yellow_text(){ + echo "`tput setaf 3`$@`tput sgr0`" +} + +green_text(){ + echo "`tput setaf 2`$@`tput sgr0`" +} + +build_docker_image(){ + if [ -f Dockerfile ]; then + docker build --build-arg UID=$UID -t progress-test . + else + red_text 'Could not find Dockerfile. WTF?' + return + fi +} + +########################## +### Build docker image ### +########################## + +yellow_text 'Building progress-test image, this might take a while...' + +if [[ $(docker images | grep progress-test) ]]; then + green_text 'Found progress-test docker image. Do you want to rebuild it? (y/N)' + read build_image + + if [[ $build_image == 'y' ]]; then + build_docker_image + else + yellow_text 'Ok, skipping...' + fi +else + build_docker_image +fi + +###################### +### Database setup ### +###################### + +yellow_text 'Preparing the database...' +docker-compose run --rm web rails db:create + + +green_text 'Everything ready to run!'