From a7be37ef679fd782db46c9493f218a84f18bc7c5 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 17 Mar 2017 10:48:20 +0100 Subject: [PATCH] Add docker files for running in production mode [SCI-1096] --- Dockerfile | 4 +- Dockerfile.production | 28 +++ Gemfile | 2 +- Makefile | 12 +- config/database.yml | 2 +- docker-compose.production.yml | 40 +++++ production.env | 21 +++ wmake-main.sh | 327 ---------------------------------- wmake-template.sh | 15 -- 9 files changed, 104 insertions(+), 347 deletions(-) create mode 100644 Dockerfile.production create mode 100644 docker-compose.production.yml create mode 100644 production.env delete mode 100644 wmake-main.sh delete mode 100644 wmake-template.sh diff --git a/Dockerfile b/Dockerfile index 8f0cd3ea2..b76ba61dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,11 @@ -FROM rails:4.2.5 +FROM ruby:2.3.3 MAINTAINER BioSistemika # additional dependecies RUN apt-get update -qq && \ apt-get install -y \ + nodejs \ + postgresql-client \ default-jre-headless \ unison \ sudo graphviz --no-install-recommends \ diff --git a/Dockerfile.production b/Dockerfile.production new file mode 100644 index 000000000..f47aa0598 --- /dev/null +++ b/Dockerfile.production @@ -0,0 +1,28 @@ +FROM ruby:2.3.3 +MAINTAINER BioSistemika + +# additional dependecies +RUN apt-get update -qq && \ + apt-get install -y \ + nodejs \ + postgresql-client \ + default-jre-headless \ + sudo graphviz --no-install-recommends \ + sudo libfile-mimeinfo-perl && \ + rm -rf /var/lib/apt/lists/* + +# create app directory +ENV APP_HOME /usr/src/app +RUN mkdir $APP_HOME +WORKDIR $APP_HOME +COPY . . +RUN rm -f $APP_HOME/config/application.yml +RUN rm -f $APP_HOME/production.env + +RUN bundle install + +ENV RAILS_ENV production +ENV DATABASE_URL=postgresql://postgres@db/scinote_production +RUN bash -c "PAPERCLIP_HASH_SECRET=$(openssl rand -base64 128 | tr -d '\n') SECRET_KEY_BASE=$(openssl rand -hex 64) rake assets:precompile" + +CMD rails s -b 0.0.0.0 diff --git a/Gemfile b/Gemfile index fa1a3f747..40ad63e33 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'http://rubygems.org' -ruby '2.2.4' +ruby '2.3.3' gem 'rails', '4.2.5' gem 'figaro' diff --git a/Makefile b/Makefile index a468c1dbc..dcca7209d 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,9 @@ heroku: docker: @docker-compose build +docker-production: + @docker-compose -f docker-compose.production.yml build + db-cli: @$(MAKE) rails cmd="rails db" @@ -21,7 +24,10 @@ database: @$(MAKE) rails cmd="rake db:create db:setup db:migrate" rails: - @docker-compose run web $(cmd) + @docker-compose run --rm web $(cmd) + +rails-production: + @docker-compose -f docker-compose.production.yml run --rm web $(cmd) run: rm tmp/pids/server.pid || true @@ -40,6 +46,9 @@ worker: cli: @$(MAKE) rails cmd="/bin/bash" +cli-production: + @$(MAKE) rails-production cmd="/bin/bash" + tests: @$(MAKE) rails cmd="rake test" @@ -56,4 +65,3 @@ export: @git checkout-index -a -f --prefix=scinote/ @tar -zcvf scinote-$(shell git rev-parse --short HEAD).tar.gz scinote @rm -rf scinote - diff --git a/config/database.yml b/config/database.yml index f028fdda5..663aec731 100644 --- a/config/database.yml +++ b/config/database.yml @@ -82,4 +82,4 @@ test: # url: <%= ENV['DATABASE_URL'] %> # production: - <<: *default + url: <%= ENV['DATABASE_URL'] %> diff --git a/docker-compose.production.yml b/docker-compose.production.yml new file mode 100644 index 000000000..502c5d098 --- /dev/null +++ b/docker-compose.production.yml @@ -0,0 +1,40 @@ +version: '2.1' +services: + db: + image: postgres:9.4 + volumes: + - scinote_production_postgres:/var/lib/postgresql/data + + web: + build: + context: . + dockerfile: Dockerfile.production + image: scinote_web:production + stdin_open: true + env_file: ./production.env + tty: true + depends_on: + - db + ports: + - "3000:3000" + command: bash -c "rm -f /usr/src/app/tmp/pids/server.pid && rails s -b 0.0.0.0" + environment: + - RAILS_ENV=production + volumes: + - scinote_production_files:/usr/src/app/public/system + + jobs: + image: scinote_web:production + tty: true + depends_on: + - db + command: rake jobs:work + env_file: ./production.env + environment: + - RAILS_ENV=production + volumes: + - scinote_production_files:/usr/src/app/public/system + +volumes: + scinote_production_postgres: + scinote_production_files: diff --git a/production.env b/production.env new file mode 100644 index 000000000..8a0f8531e --- /dev/null +++ b/production.env @@ -0,0 +1,21 @@ +SECRET_KEY_BASE="" +PAPERCLIP_HASH_SECRET="" + +DATABASE_URL=postgresql://postgres@db/scinote_production + +MAIL_FROM="" +MAIL_REPLYTO="" + +SMTP_ADDRESS="" +SMTP_PORT="" +SMTP_DOMAIN="" +SMTP_USERNAME="" +SMTP_PASSWORD="" +MAIL_SERVER_URL="" + +PAPERCLIP_STORAGE="filesystem" + +ENABLE_TUTORIAL=true +ENABLE_RECAPTCHA=false +ENABLE_USER_CONFIRMATION="false" +ENABLE_USER_REGISTRATION="true" diff --git a/wmake-main.sh b/wmake-main.sh deleted file mode 100644 index 394d86ebd..000000000 --- a/wmake-main.sh +++ /dev/null @@ -1,327 +0,0 @@ -#!/bin/bash -set -e - -################################### -# VARIABLES -################################### -IMAGE_NAME="railsdocker_web" -PG_IMAGE_NAME="postgres:9.4" -WEB_CONTAINER="web" -DB_CONTAINER="db" -DBDATA_CONTAINER="dbdata" -WIN_HOST_RAILS_WS=$PWD -WIN_HOST_DB_TEST="$PWD/db/test.dump" -WIN_HOST_DB_DEV="$PWD/db/development.dump" -CONTAINER_MOUNT_WS="//tmp//app" -CONTAINER_RAILS_WS_PARENT="//usr//src//" -CONTAINER_RAILS_WS="//usr//src//app" -PORT=3000 -################################### - - -# Show help etc. -PROCEED=0 -if [ $# -eq 0 ]; then - echo "Invalid arguments. Call '$FILENAME help' for more info." -elif [ $1 == "help" ]; then - echo "Usage: $FILENAME COMMAND/S" - echo "" - echo "Windows Boot2Docker helper script. If starting from scratch, the following order of commands should be used to get you started: docker, setup, build." - echo "" - echo "Commands:" - echo " $FILENAME help Prints out the help contents" - echo "" - echo " $FILENAME bash Starts the bash console on host computer with Docker environment configured" - echo "" - echo " $FILENAME docker Creates the Docker image from the Dockerfile" - echo "" - echo " $FILENAME data Initializes the data persistent container" - echo "" - echo " $FILENAME run Starts the Rails server in the configured Docker container" - echo "" - echo " $FILENAME cli Starts the bash console in the configured Docker container" - echo " (with unison syncing which means a bit slower startup, but faster command execution)" - echo "" - echo " $FILENAME cli active Connects to the bash console in the already running Docker container" - echo "" - echo " $FILENAME cli nosync Starts the bash console in the configured Docker container" - echo " (without database container running)" - echo " (without Unison syncing, fast startup but horrendously slow command execution)" - echo "" - echo " $FILENAME cli nodb Starts the bash console in the configured Docker container" - echo " (without database container running)" - echo " (with unison syncing which means a bit slower startup, but faster command execution)" - echo "" - echo " $FILENAME db-cli Starts the bash console in the configured database Docker container" - echo "" - echo " $FILENAME db-cli active Connects to the bash console in the already running database Docker container" - echo "" - echo " $FILENAME psql Connects to the database of the running database Docker container" - echo "" - echo " $FILENAME pgdump Dumps the database contents of the running database Docker container into dump files on the host" - echo "" - echo " $FILENAME pgrestore Restores the database contents of the dump files on host into the Docker container" - echo "" - echo " $FILENAME clean Removes the Docker image (this will also remove the downloaded packages)" - echo "" - echo " $FILENAME cclean Removes all the stopped Docker containers (cleans up some disk space)" -else - PROCEED=1 -fi - -# Only continue if not help... -if [ $PROCEED -eq 1 ]; then - cd "$B2D_HOME" - - # Copy of Boot2Docker start.sh from here on - - # clear the MSYS MOTD - - cd "$(dirname "$BASH_SOURCE")" - - ISO="$HOME/.boot2docker/boot2docker.iso" - - if [ ! -e "$ISO" ]; then - echo 'copying initial boot2docker.iso (run "boot2docker.exe download" to update)' - mkdir -p "$(dirname "$ISO")" - cp ./boot2docker.iso "$ISO" - fi - - echo 'initializing...' - ./boot2docker.exe init - echo - - echo 'starting...' - ./boot2docker.exe start - echo - - echo 'IP address of docker VM:' - ./boot2docker.exe ip - B2D_IP="$(./boot2docker.exe ip)" - echo - - echo 'setting environment variables ...' - ./boot2docker.exe shellinit | sed 's,\\,\\\\,g' # eval swallows single backslashes in windows style path - eval "$(./boot2docker.exe shellinit 2>/dev/null | sed 's,\\,\\\\,g')" - echo - - # Modification of original code - cd "$VBOX_HOME" - echo "$VBOX_HOME" - set +e - "$VBOX_HOME/"VBoxManage controlvm boot2docker-vm natpf1 delete "rails-server" - set -e - "$VBOX_HOME/"VBoxManage controlvm boot2docker-vm natpf1 "rails-server,tcp,127.0.0.1,$PORT,,$PORT" - cd "$B2D_HOME" - - echo 'You can now use `docker` directly, or `boot2docker ssh` to log into the VM.' - - - # End of copy Boot2Docker start.sh - - if [ $1 == "bash" ]; then - exec "$BASH" --login -i - elif [ $1 == "docker" ]; then - exec "$BASH" --login -i -c "\ - cd $WIN_HOST_RAILS_WS\ - ;docker pull $PG_IMAGE_NAME\ - ;docker build -t $IMAGE_NAME .\ - ;docker rm $WEB_CONTAINER\ - ;docker rm $DB_CONTAINER" - elif [ $1 == "data" ]; then - exec "$BASH" --login -i -c "\ - docker create \ - -v //var//lib//postgres \ - --name $DBDATA_CONTAINER \ - $PG_IMAGE_NAME \ - //bin//true" - elif [ $1 == "run" ]; then - exec "$BASH" --login -i -c "\ - docker kill $WEB_CONTAINER\ - ;docker rm $WEB_CONTAINER\ - ;docker kill $DB_CONTAINER\ - ;docker rm $DB_CONTAINER\ - ;docker run \ - --rm \ - --name $DB_CONTAINER \ - --volumes-from $DBDATA_CONTAINER \ - -p 5432:5432 \ - $PG_IMAGE_NAME \ - & (sleep 10\ - ;docker run \ - --rm \ - --name $WEB_CONTAINER \ - --link $DB_CONTAINER:db \ - -ti \ - -v $WIN_HOST_RAILS_WS:$CONTAINER_MOUNT_WS \ - -w $CONTAINER_MOUNT_WS \ - -p $PORT:$PORT \ - -u 'root' \ - $IMAGE_NAME \ - //bin//bash -l -c '\ - rm -r tmp/pids/server.pid\ - ;export PATH=/usr/local/bundle/bin:\$PATH\ - ;cp -rf $CONTAINER_MOUNT_WS// $CONTAINER_RAILS_WS_PARENT\ - ;chmod -R 777 $CONTAINER_MOUNT_WS\ - ;chmod -R 777 $CONTAINER_RAILS_WS\ - ;unison \ - $CONTAINER_MOUNT_WS \ - $CONTAINER_RAILS_WS \ - -perms 0 \ - -ignorecase false \ - -links false \ - -ignoreinodenumbers \ - -auto \ - -silent \ - -ignore \"Path vendor/bundle\" \ - -ignore \"Path .git\" \ - -ignore \"Path .bundle\" \ - -repeat 1 \ - > /dev/null \ - & chmod -R 777 $CONTAINER_RAILS_WS\ - ;cd $CONTAINER_RAILS_WS\ - ;rails server -b 0.0.0.0 -p $PORT')" - elif [[ ( $1 == "cli" ) && ( $2 == "active" ) ]]; then - exec "$BASH" --login -i -c "\ - docker exec \ - -ti \ - $WEB_CONTAINER \ - //bin//bash -l" - elif [[ ( $1 == "cli" ) && ( $2 == "nosync" ) ]]; then - exec "$BASH" --login -i -c "\ - docker kill $WEB_CONTAINER\ - ;docker rm $WEB_CONTAINER\ - ;docker run \ - --rm \ - --name $WEB_CONTAINER \ - -ti \ - -v $WIN_HOST_RAILS_WS:$CONTAINER_RAILS_WS \ - -w $CONTAINER_RAILS_WS \ - -p $PORT:$PORT \ - $IMAGE_NAME \ - //bin//bash -l" - elif [[ ( $1 == "cli" ) && ( $2 == "nodb" ) ]]; then - exec "$BASH" --login -i -c " - docker kill $WEB_CONTAINER\ - ;docker rm $WEB_CONTAINER\ - ;docker run \ - --rm \ - --name $WEB_CONTAINER \ - -ti \ - -v $WIN_HOST_RAILS_WS:$CONTAINER_MOUNT_WS \ - -w $CONTAINER_MOUNT_WS \ - -u 'root' \ - $IMAGE_NAME //bin//bash -l -c '\ - rm -r tmp/pids/server.pid\ - ;export PATH=/usr/local/bundle/bin:\$PATH\ - ;cp -rf $CONTAINER_MOUNT_WS// $CONTAINER_RAILS_WS_PARENT\ - ;chmod -R 777 $CONTAINER_MOUNT_WS\ - ;chmod -R 777 $CONTAINER_RAILS_WS\ - ;unison \ - $CONTAINER_MOUNT_WS \ - $CONTAINER_RAILS_WS \ - -perms 0 \ - -ignorecase false \ - -links false \ - -ignoreinodenumbers \ - -auto \ - -silent \ - -ignore \"Path vendor/bundle\" \ - -ignore \"Path .git\" \ - -ignore \"Path .bundle\" \ - -repeat 1 \ - > /dev/null \ - & chmod -R 777 $CONTAINER_RAILS_WS\ - ;cd $CONTAINER_RAILS_WS\ - ;//bin//bash'" - elif [ $1 == "cli" ]; then - exec "$BASH" --login -i -c " - docker kill $WEB_CONTAINER\ - ;docker rm $WEB_CONTAINER\ - ;docker kill $DB_CONTAINER\ - ;docker rm $DB_CONTAINER\ - ;docker run \ - --rm \ - --name $DB_CONTAINER \ - --volumes-from $DBDATA_CONTAINER \ - -p 5432:5432 \ - $PG_IMAGE_NAME \ - > /dev/null \ - & (sleep 10\ - ;docker run \ - --rm \ - --name $WEB_CONTAINER \ - --link $DB_CONTAINER:db \ - -ti \ - -v $WIN_HOST_RAILS_WS:$CONTAINER_MOUNT_WS \ - -w $CONTAINER_MOUNT_WS \ - -u 'root' \ - $IMAGE_NAME //bin//bash -l -c '\ - rm -r tmp/pids/server.pid\ - ;export PATH=/usr/local/bundle/bin:\$PATH\ - ;cp -rf $CONTAINER_MOUNT_WS// $CONTAINER_RAILS_WS_PARENT\ - ;chmod -R 777 $CONTAINER_MOUNT_WS\ - ;chmod -R 777 $CONTAINER_RAILS_WS\ - ;unison \ - $CONTAINER_MOUNT_WS \ - $CONTAINER_RAILS_WS \ - -perms 0 \ - -ignorecase false \ - -links false \ - -ignoreinodenumbers \ - -auto \ - -silent \ - -ignore \"Path vendor/bundle\" \ - -ignore \"Path .git\" \ - -ignore \"Path .bundle\" \ - -repeat 1 \ - > /dev/null \ - & chmod -R 777 $CONTAINER_RAILS_WS\ - ;cd $CONTAINER_RAILS_WS\ - ;//bin//bash')" - elif [[ ( $1 == "db-cli" ) && ( $2 == "active" ) ]]; then - exec "$BASH" --login -i -c "\ - docker exec \ - -ti \ - $DB_CONTAINER \ - //bin//bash -l" - elif [ $1 == "db-cli" ]; then - exec "$BASH" --login -i -c "\ - docker kill $DB_CONTAINER\ - ;docker rm $DB_CONTAINER\ - ;docker run \ - --rm \ - --name $DB_CONTAINER \ - -ti \ - --volumes-from $DBDATA_CONTAINER \ - -p 5432:5432 \ - $PG_IMAGE_NAME \ - //bin//bash -l" - elif [ $1 == "psql" ]; then - eval "'$PSQL_BIN/psql' -h $B2D_IP -p 5432 -U postgres" - elif [ $1 == "pgdump" ]; then - eval "'$PSQL_BIN/pg_dump' -h $B2D_IP -p 5432 -U postgres -f '$WIN_HOST_DB_TEST' -O -c -C scinote_test" - eval "'$PSQL_BIN/pg_dump' -h $B2D_IP -p 5432 -U postgres -f '$WIN_HOST_DB_DEV' -O -c -C scinote_development" - elif [ $1 == "pgrestore" ]; then - eval "'$PSQL_BIN/psql' -h $B2D_IP -p 5432 -U postgres -f '$WIN_HOST_DB_TEST' -d scinote_test" - eval "'$PSQL_BIN/psql' -h $B2D_IP -p 5432 -U postgres -f '$WIN_HOST_DB_DEV' -d scinote_development" - elif [ $1 == "clean" ]; then - exec "$BASH" --login -i -c "\ - docker rmi -f $IMAGE_NAME\ - ;docker rmi -f $PG_IMAGE_NAME" - elif [ $1 == "cclean" ]; then - exec "$BASH" --login -i -c "\ - docker rm $(docker ps -a -q)" - fi -fi -unset IMAGE_NAME -unset PG_IMAGE_NAME -unset WEB_CONTAINER -unset DB_CONTAINER -unset WIN_HOST_RAILS_WS -unset WIN_HOST_DB_TEST -unset WIN_HOST_DB_DEV -unset CONTAINER_RAILS_WS -unset PORT -unset B2D_IP -unset PROCEED \ No newline at end of file diff --git a/wmake-template.sh b/wmake-template.sh deleted file mode 100644 index 23468753d..000000000 --- a/wmake-template.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -set -e - -B2D_HOME=C:/"Program Files"/"Boot2Docker for Windows" -VBOX_HOME=C:/"Program Files"/Oracle/VirtualBox -PSQL_BIN=C:/"Program Files"/PostgreSQL/9.4/bin -FILENAME="wmake.sh" - -# Call the "real" function -. wmake-main.sh $1 $2 - -unset B2D_HOME -unset VBOX_HOME -unset PSQL_BIN -unset FILENAME \ No newline at end of file