Permalink
Browse files

Dockerize application

* Created Dockerfile
* Created our very own ruby 1.9.3 base container
  as it is no longer supported
* Added a docker-compose.yaml to run against a proper mysql
  database
* Added puma as default server

Signed-off-by: Rafael Porres Molina <rafa@sourced.tech>
  • Loading branch information...
rporres committed Jul 9, 2018
1 parent d862707 commit f1ef16722215743d23af003b3c3c892d5bc6e37d
@@ -0,0 +1,16 @@
FROM pageonex/ruby-base:v1.9.3.2
ENV PATH $PATH:/usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16/
WORKDIR /tmp
ADD ./Gemfile Gemfile
ADD ./Gemfile.lock Gemfile.lock
RUN bundle install
ENV APP_ROOT /workspace
RUN mkdir -p $APP_ROOT
WORKDIR $APP_ROOT
COPY . $APP_ROOT
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
@@ -65,3 +65,6 @@ gem 'piwik_analytics'
# for downloading zipped archives
gem 'rubyzip'
gem 'puma', '~> 3.11.4'
gem 'puma_worker_killer'
@@ -77,6 +77,7 @@ GEM
i18n (~> 0.5)
ffi (1.6.0)
formatador (0.2.4)
get_process_mem (0.2.2)
guard (1.7.0)
formatador (>= 0.2.4)
listen (>= 0.6.0)
@@ -117,7 +118,7 @@ GEM
mysql2 (0.3.21)
nokogiri (1.5.9)
orm_adapter (0.4.0)
pg (0.15.0)
pg (0.18.4)
piwik_analytics (1.0.2)
actionpack
activesupport
@@ -127,6 +128,10 @@ GEM
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
puma (3.11.4)
puma_worker_killer (0.1.0)
get_process_mem (~> 0.2)
puma (>= 2.7, < 4)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
@@ -235,6 +240,8 @@ DEPENDENCIES
mysql2 (~> 0.3.21)
pg
piwik_analytics
puma (~> 3.11.4)
puma_worker_killer
rails (= 3.2.13)
rmagick
rodf
@@ -252,4 +259,4 @@ RUBY VERSION
ruby 1.9.3p551
BUNDLED WITH
1.16.2
1.16.4
@@ -0,0 +1,41 @@
# Pageonex on Docker
You can run locally using [docker compose](https://docs.docker.com/compose/). In order to start the pageonex and mysql containers, run from the root of this repository:
```
docker-compose up -d --build
```
To create the initial database:
```
docker-compose run app rake db:migrate --trace
```
To get the rails console (See [INSTALL.md](INSTALL.md) for instructions on creating an admin user)
```
docker-compose run app rails console
```
If you make changes in the app and you want to rebuild your container
```
docker-compose up -d --build
```
If you want to log in the mysql database
```
docker-compose exec mysql mysql -u root --password=root
```
If you want to inspect the logs
```
docker-compose logs
```
If you want to shut down everything
```
docker-compose stop
```
If you want to remove everything (including volumes)
```
docker-compose rm -s -v
```
@@ -11,49 +11,63 @@ PageOnex runs in Ruby on Rails, so you'll ned to install:
Process
-------
You might want to clone the repository with git:
> git clone git@github.com:numeroteca/pageonex.git
```
git clone git@github.com:numeroteca/pageonex.git
```
Or download the .zip file from https://github.com/numeroteca/pageonex/archive/master.zip
Go to the directory
> cd pageonex
```
cd pageonex
```
Setup a database and then edit the `config/database.yml` file to give the app access to that database (we use MySQL).
Install the gems
> bundle install
```
bundle install
```
Run the migration (this will create the database with its tables):
> rake db:migrate
```
rake db:migrate
```
Create a PageOneX admin user like this:
> rails console
> User.create(:username=>'admin',:email=>'admin@admin.admin',:password=>'1234',:password_confirmation=>'1234')
> me = User.first
> me.admin = true
> me.save
```
rails console
User.create(:username=>'admin-user',:email=>'admin-user@admin.admin',:password=>'hola1234',:password_confirmation=>'hola1234')
me = User.first
me.admin = true
me.save
```
Load the list of newspapers into the data base:
> rake scraping:update_media
```
rake scraping:update_media
```
Run the server:
> rails server
```
rails server
```
You can now navigate the app at:
> http://localhost:3000/
or
> http://0.0.0.0:3000/
You can now navigate the app at `http://localhost:3000/` or `http://0.0.0.0:3000/`
### Options
If you have problems with the dependencies of the gems check this tricks.
**Problems with dependencies**
Run the same commands with 'bundle exec' like
>bundle exec rake db:migrate
```
bundle exec rake db:migrate
```
If you have problems with the pg gem, needed for the "heroku" version, you can run:
>bundle install --without heroku
```
bundle install --without heroku
```
@@ -0,0 +1,13 @@
# TODO: This is temporary, will need to adjust it when we connect to
# a proper CI
PROJECT = pageonex
DOCKER_REGISTRY = docker.io
DOCKER_ORG = pageonex
VERSION := $(TAG)
DOCKER_IMAGE = $(DOCKER_REGISTRY)/$(DOCKER_ORG)/$(PROJECT):$(VERSION)
docker-build:
docker build -t $(DOCKER_IMAGE) .
docker-push: docker-build
docker push $(DOCKER_IMAGE)
@@ -1,25 +1,24 @@
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
default: &default
adapter: <%= ENV['DATABASE_ADAPTER'].blank? ? 'sqlite3' : ENV['DATABASE_ADAPTER'] %>
encoding: <%= ENV['DATABASE_ENCODING'].blank? ? 'utf8' : ENV['DATABASE_ENCODING'] %>
pool: <%= ENV['DATABASE_POOL'].blank? ? 5 : ENV['DATABASE_POOL'].to_i %>
timeout: <%= ENV['DATABASE_TIMEOUT'].blank? ? 5000 : ENV['DATABASE_TIMEOUT'].to_i %>
username: <%= ENV['DATABASE_USERNAME'].blank? ? 'root' : ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'].blank? ? 'root' : ENV['DATABASE_PASSWORD'] %>
host: <%= ENV['DATABASE_HOST'].blank? ? 'localhost' : ENV['DATABASE_HOST'] %>
port: <%= ENV['DATABASE_PORT'].blank? ? 3306 : ENV['DATABASE_PORT'].to_i %>
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
<<: *default
database: <%= ENV['DATABASE_NAME'].blank? ? 'db/development.sqlite3' : ENV['DATABASE_NAME'] %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
<<: *default
database: <%= ENV['DATABASE_NAME'].blank? ? 'db/test.sqlite3' : ENV['DATABASE_NAME'] %>
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
<<: *default
database: <%= ENV['DATABASE_NAME'].blank? ? 'db/production.sqlite3' : ENV['DATABASE_NAME'] %>
@@ -0,0 +1,69 @@
rackup DefaultRackup
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
preload_app!
# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end
before_fork do
require 'puma_worker_killer'
PumaWorkerKiller.enable_rolling_restart # Default is every 6 hours
end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
on_worker_boot do
# Valid on Rails up to 4.1 the initializer method of setting `pool` size
ActiveSupport.on_load(:active_record) do
config = ActiveRecord::Base.configurations[Rails.env] ||
Rails.application.config.database_configuration[Rails.env]
config['pool'] = ENV['RAILS_MAX_THREADS'] || 5
ActiveRecord::Base.establish_connection(config)
end
end
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
@@ -0,0 +1,51 @@
version: '2'
services:
mysql:
image: mysql:5.6.40
ports:
- "13306:13306"
volumes_from:
- dbdata
volumes:
- ./docker-files/pageonex.cnf:/etc/mysql/conf.d/pageonex.cnf
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=dev
dbdata:
image: tianon/true
volumes:
- /var/lib/mysql
app:
build: .
environment:
- RAILS_ENV=development
- DATABASE_ADAPTER=mysql2
- DATABASE_USERNAME=root
- DATABASE_PASSWORD=root
- DATABASE_HOST=mysql
- DATABASE_PORT=13306
- DATABASE_NAME=dev
depends_on:
- "mysql"
ports:
- '3000:3000'
volumes_from:
- app_data
links:
- "mysql"
command:
- ./docker-files/cleanup-pid-file-and-wait-for-mysql.sh
- mysql
- root
- bundle
- exec
- puma
- -C
- "config/puma.rb"
app_data:
image: tianon/true
volumes:
- ".:/workspace"
@@ -0,0 +1,21 @@
#!/bin/bash
# cleanup-pid-file-and-wait-for-mysql.sh
set -e
host="$1"
password="$2"
shift; shift
cmd="$@"
rm -f /workspace/tmp/pids/server.pid
port=${DATABASE_PORT:-3306}
until mysql -u root --port $port --password=$password -h $host -e "SELECT 1"; do
>&2 echo "Mysql is unavailable - sleeping"
sleep 1
done
>&2 echo "Mysql is up - executing command"
exec $cmd
@@ -0,0 +1,2 @@
[mysqld]
port=13306
Oops, something went wrong.

0 comments on commit f1ef167

Please sign in to comment.