Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Docker for development #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerdev/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_PROJECT_NAME=minka-dev
16 changes: 16 additions & 0 deletions .dockerdev/.pryrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true
# rubocop:disable all


if defined?(PryByebug)
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
end

if defined?(Rails)
require 'rails/console/app'
require 'rails/console/helpers'
include Rails::ConsoleMethods
end
26 changes: 26 additions & 0 deletions .dockerdev/.psqlrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Don't display the "helpful" message on startup.
\set QUIET 1

-- Allow specifying the path to history file via `PSQL_HISTFILE` env variable
-- (and fallback to the defaukt $HOME/.psql_history otherwise)
\set HISTFILE `[[ -z $PSQL_HISTFILE ]] && echo $HOME/.psql_history || echo $PSQL_HISTFILE`

-- Show how long each query takes to execute
\timing

-- Use best available output format
\x auto

-- Verbose error reports
\set VERBOSITY verbose

-- If a command is run more than once in a row,
-- only store it once in the history
\set HISTCONTROL ignoredups
\set COMP_KEYWORD_CASE upper

-- By default, NULL displays as an empty space. Is it actually an empty
-- string, or is it null? This makes that distinction visible
\pset null '[NULL]'

\unset QUIET
42 changes: 42 additions & 0 deletions .dockerdev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM ruby:2.6.5-alpine

ARG BUNDLER_VERSION
ARG NODE_VERSION

# Install system dependencies
RUN apk --update add less bash git curl wget build-base && \
apk add postgresql-dev && \
apk add curl-dev && \
apk add tzdata && \
apk add geos geos-dev && \
apk add postgresql-client && \
apk add nodejs && \
apk add vim imagemagick && \
apk add vim exiftool && \
rm -rf /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log

ENV LANG=C.UTF-8

ENV NVM_DIR /root/.nvm
RUN touch /root/.bashrc
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.38.0/install.sh | bash

RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"

ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH

# Configure bundler
# https://bundler.io/v2.1/guides/bundler_docker_guide.html
ENV GEM_HOME=/bundle
ENV PATH /app/bin:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH

# Upgrade RubyGems and install required Bundler version
RUN gem update --system && \
gem install bundler:$BUNDLER_VERSION

# Create a directory for the app code
RUN mkdir -p /app

WORKDIR /app
12 changes: 12 additions & 0 deletions .dockerdev/Dockerfile.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:12.13.0

# Install base system dependencies
RUN apt-get update -qq && apt-get install -yq --no-install-recommends build-essential tzdata curl wget
RUN apt-get install -yq imagemagick libimage-exiftool-perl

ENV LANG=C.UTF-8

# Create a directory for the app code
RUN mkdir -p /app

WORKDIR /app
26 changes: 26 additions & 0 deletions .dockerdev/com.user.docker-host-alias.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.docker-host-alias</string>
<key>ProgramArguments</key>
<array>
<string>/sbin/ifconfig</string>
<string>lo0</string>
<string>alias</string>
<string>172.17.0.1</string>
</array>
<key>RunAtLoad</key> <true/>
<key>Nice</key>
<integer>10</integer>
<key>KeepAlive</key>
<false/>
<key>AbandonProcessGroup</key>
<true/>
<key>StandardErrorPath</key>
<string>/var/log/docker-host-alias.log</string>
<key>StandardOutPath</key>
<string>/var/log/docker-host-alias.log</string>
</dict>
</plist>
162 changes: 162 additions & 0 deletions .dockerdev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
version: '3.4'

x-app: &app
build:
context: .
dockerfile: ./Dockerfile
args:
BUNDLER_VERSION: '1.17.3'
NODE_VERSION: '12.13.0'
image: minka-dev
tmpfs:
- /tmp

x-backend: &backend
<<: *app
stdin_open: true
tty: true
environment: &backend-env
NODE_ENV: ${NODE_ENV:-development}
RAILS_ENV: ${RAILS_ENV:-development}
REDIS_URL: redis://redis:6379/
REDIS_HOST: redis
REDIS_POST: 6379
DATABASE_URL: postgres://postgres:postgres@postgis:5432
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
DATABASE_HOST: postgis
SELENIUM_URL: http://selenium:4444/wd/hub
TEST_APP_HOST: 'http://test'
TEST_APP_PORT: 8200
AWS_REGION: eu-west-1
AWS_ACCESS_KEY_ID: minio
AWS_SECRET_ACCESS_KEY: 2NVQWHvTT73XMgqapGchy6yAtwHezMZn
S3_BUCKET: minka-development
S3_FORCE_PATH: 'true'
S3_ENDPOINT: http://172.17.0.1:9000
NODE_API_URL: http://172.17.0.1:4000/v1
ES_URL: http://elasticsearch:9200
JWT_SECRET: jwtSecret
JWT_APP_SECRET: jwtApplicationSecret

# Misc
BOOTSNAP_CACHE_DIR: /bundle/bootsnap
WEBPACKER_DEV_SERVER_HOST: webpacker
WEB_CONCURRENCY: 1
HISTFILE: /app/log/.bash_history
EDITOR: vi
volumes:
- ../:/app:cached
- rails_cache:/app/tmp/cache
- bundle:/bundle
- node_modules:/app/node_modules
- ./scripts:/scripts:ro
- ./.psqlrc:/root/.psqlrc:ro
- ./.pryrc:/root/.pryrc:ro
depends_on:
- postgis
- redis
- elasticsearch
x-api: &api
build:
context: .
dockerfile: ./Dockerfile.api
environment: &api-env
ES_URL: http://elasticsearch:9200
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
DATABASE_HOST: postgis
DATABASE_PORT: 5432
REDIS_HOST: redis
REDIS_POST: 6379
API_URL: http://172.17.0.1:3000
CURRENT_VERSION_URL: http://localhost:4000/v1
STATIC_IMAGE_PREFIX: http://172.17.0.1:3000/attachments/
JWT_SECRET: jwtSecret
JWT_APP_SECRET: jwtApplicationSecret
image: minka-api-dev
tmpfs:
- /tmp
stdin_open: true
tty: true
volumes:
- ../iNaturalistAPI:/app:cached
- node_modules_api:/app/node_modules
- ./.psqlrc:/root/.psqlrc:ro
depends_on:
- postgis
- redis
- elasticsearch

services:
rails:
<<: *backend
command: bundle exec rails server -b 0.0.0.0
ports:
- 3000:3000
api:
<<: *api
command: node app.js
ports:
- 4000:4000

runner:
<<: *backend
command: /bin/bash
environment:
<<: *backend-env
HOST: http://rails:3000
DOMAIN: http://localhost:3000

api-runner:
<<: *api
command: /bin/bash

jobs:
<<: *backend
command: bundle exec sidekiq -C config/sidekiq.yml
environment:
<<: *backend-env
HOST: http://rails:3000

postgis:
image: postgis/postgis
volumes:
- ./.psqlrc:/root/.psqlrc:ro
- ./volumes/postgis:/var/lib/postgresql/data
- ./log:/root/log:cached
environment:
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432

elasticsearch:
image: kaibadash/elasticsearch-kuromoji:7.3.1
environment:
- node.name=elasticsearch
- cluster.name=es-docker-cluster
- cluster.initial_master_nodes=elasticsearch
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./volumes/elasticsearch:/usr/share/elasticsearch/data
ports:
- 9200

redis:
image: redis:6.2-alpine
volumes:
- redis:/data
ports:
- 6379

volumes:
redis:
bundle:
node_modules:
node_modules_api:
rails_cache:
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ config/environments/development.rb

app/assets/javascripts/*-webpack.js

.dockerdev/volumes/*
63 changes: 63 additions & 0 deletions bin/dockerdev
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

cd .dockerdev

case "$1" in
setup-localhost-alias)
cp ../.dockerdev/com.user.docker-host-alias.plist /Library/LaunchDaemons/
chmod 0644 /Library/LaunchDaemons/com.user.docker-host-alias.plist
chown root:wheel /Library/LaunchDaemons/com.user.docker-host-alias.plist
launchctl load /Library/LaunchDaemons/com.user.docker-host-alias.plist
;;
start)
docker-compose up -d $2
docker attach $(docker-compose ps -q $2)
;;
start-services)
docker-compose up -d postgis redis elasticsearch
;;
run)
docker-compose up -d runner
docker-compose exec runner "${@:2}"
;;
api-run)
docker-compose up -d api-runner
docker-compose exec api-runner "${@:2}"
;;
stop)
docker-compose down
;;
server)
docker-compose up -d rails && docker attach $(docker-compose ps -q rails)
;;
api-server)
docker-compose up -d api && docker attach $(docker-compose ps -q api)
;;
jobs)
docker-compose up -d jobs && docker attach $(docker-compose ps -q jobs)
;;
compile-assets)
npm install
docker-compose run --rm runner ./node_modules/.bin/gulp webpack
;;
setup)
docker-compose build --no-cache
docker-compose run --rm runner setup
docker-compose run --rm runner npm install
docker-compose run --rm runner ./node_modules/.bin/gulp webpack
;;
api-setup)
docker-compose run --rm api-runner npm install
docker-compose run --rm api-runner cp config_example.js config.js
;;
setup-localhost-alias)
cp com.user.docker-host-alias.plist /Library/LaunchDaemons/
chmod 0644 /Library/LaunchDaemons/com.user.docker-host-alias.plist
chown root:wheel /Library/LaunchDaemons/com.user.docker-host-alias.plist
launchctl load /Library/LaunchDaemons/com.user.docker-host-alias.plist
;;
*)
docker-compose up -d runner
docker-compose exec runner $*
;;
esac
14 changes: 13 additions & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Dir.chdir APP_ROOT do
end

puts "\n== Creating Template Database =="
if system "command -v createdb"
if system "which createdb &> /dev/null"
system "createdb template_postgis"
system "psql -d template_postgis -c 'create extension postgis'"
system "psql -d template_postgis -c 'create extension \"uuid-ossp\"'"
Expand All @@ -47,6 +47,18 @@ Dir.chdir APP_ROOT do
system "rm -f log/*"
system "rm -rf tmp/cache"

puts "\n== Rebuilding elasticsearch indexes =="
system "rake es:rebuild"

puts "\n== Installing node modules"
system "source $NVM_DIR/nvm.sh && npm install"

puts "\n== Loading sample data"
system "rails r \"Site.create( name: 'iNaturalist', url: 'http://localhost:3000' )\""
system "rails r tools/load_sources.rb"
system "rails r tools/load_iconic_taxa.rb"
system "rake inaturalist:generate_translations_js"

puts "\n== Restarting application server =="
system "touch tmp/restart.txt"
end
Loading