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

New docker compose file format #10

Merged
merged 3 commits into from
Jul 14, 2017
Merged
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
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
data
.bundle
.git
tmp/*
log/*
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SECRET_KEY_BASE=abcdefg
MYSQL_USER=beer
MYSQL_PASSWORD=beer
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

22 changes: 8 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
FROM alpine:3.4
FROM ruby:2.4-alpine

ENV APP_DIR=/usr/app
ENV APP_DIR=/app

RUN apk update && apk upgrade && \
apk add ruby ruby-io-console ruby-bundler ruby-irb ruby-bigdecimal tzdata mysql-dev && \
apk add nodejs
RUN apk update && \
apk add --no-cache tzdata mysql-dev mysql-client nodejs curl-dev ruby-dev build-base bash

RUN mkdir -p $APP_DIR
WORKDIR $APP_DIR

# Cache bundle install
COPY Gemfile $APP_DIR/
COPY Gemfile.lock $APP_DIR/

RUN apk add --virtual build-dependencies curl-dev ruby-dev build-base && \
cd $APP_DIR; bundle install --without development test -j4 && \
apk del build-dependencies && \
rm -rf /var/cache/apk/*
COPY Gemfile Gemfile.lock ./
RUN bundle install --without development test -j2

COPY . $APP_DIR
RUN chown -R nobody:nogroup $APP_DIR
USER nobody

ENV RAILS_ENV=production
RUN bundle exec rake assets:precompile

# Publish port 8080
EXPOSE 8080

CMD [ "/app/docker/start.sh" ]
17 changes: 3 additions & 14 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 4.2.0'
gem 'mysql2'

Expand All @@ -22,19 +20,10 @@ gem 'jquery-rails'
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
gem 'puma'

gem 'pry-byebug'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]
group :development do
gem 'pry-byebug'
end
92 changes: 43 additions & 49 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.2.7.1)
actionpack (= 4.2.7.1)
actionview (= 4.2.7.1)
activejob (= 4.2.7.1)
actionmailer (4.2.9)
actionpack (= 4.2.9)
actionview (= 4.2.9)
activejob (= 4.2.9)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.7.1)
actionview (= 4.2.7.1)
activesupport (= 4.2.7.1)
actionpack (4.2.9)
actionview (= 4.2.9)
activesupport (= 4.2.9)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.7.1)
activesupport (= 4.2.7.1)
actionview (4.2.9)
activesupport (= 4.2.9)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (4.2.7.1)
activesupport (= 4.2.7.1)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (4.2.9)
activesupport (= 4.2.9)
globalid (>= 0.3.0)
activemodel (4.2.7.1)
activesupport (= 4.2.7.1)
activemodel (4.2.9)
activesupport (= 4.2.9)
builder (~> 3.1)
activerecord (4.2.7.1)
activemodel (= 4.2.7.1)
activesupport (= 4.2.7.1)
activerecord (4.2.9)
activemodel (= 4.2.9)
activesupport (= 4.2.9)
arel (~> 6.0)
activesupport (4.2.7.1)
activesupport (4.2.9)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
Expand All @@ -47,16 +47,16 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
concurrent-ruby (1.0.5)
connection_pool (2.2.0)
dalli (2.7.6)
erubis (2.7.0)
execjs (2.7.0)
foundation-rails (5.3.3.0)
railties (>= 3.1.0)
sass (>= 3.2.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
globalid (0.4.0)
activesupport (>= 4.2.0)
haml (4.0.7)
tilt
haml-rails (0.9.0)
Expand All @@ -71,25 +71,25 @@ GEM
haml (~> 4.0.0)
nokogiri (~> 1.6.0)
ruby_parser (~> 3.5)
i18n (0.7.0)
i18n (0.8.6)
jbuilder (2.4.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.1.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
json (1.8.6)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mail (2.6.6)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.1)
minitest (5.10.2)
multi_json (1.12.1)
mysql2 (0.4.4)
nokogiri (1.6.8.1)
Expand All @@ -102,21 +102,21 @@ GEM
byebug (~> 9.0)
pry (~> 0.10)
puma (3.6.0)
rack (1.6.4)
rack (1.6.8)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.7.1)
actionmailer (= 4.2.7.1)
actionpack (= 4.2.7.1)
actionview (= 4.2.7.1)
activejob (= 4.2.7.1)
activemodel (= 4.2.7.1)
activerecord (= 4.2.7.1)
activesupport (= 4.2.7.1)
rails (4.2.9)
actionmailer (= 4.2.9)
actionpack (= 4.2.9)
actionview (= 4.2.9)
activejob (= 4.2.9)
activemodel (= 4.2.9)
activerecord (= 4.2.9)
activesupport (= 4.2.9)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.7.1)
railties (= 4.2.9)
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
Expand All @@ -126,15 +126,13 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (4.2.7.1)
actionpack (= 4.2.7.1)
activesupport (= 4.2.7.1)
railties (4.2.9)
actionpack (= 4.2.9)
activesupport (= 4.2.9)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.3.0)
rdoc (4.2.2)
json (~> 1.4)
redis (3.3.0)
rake (12.0.0)
redis (3.3.3)
ruby_parser (3.8.2)
sexp_processor (~> 4.1)
sass (3.2.19)
Expand All @@ -143,15 +141,12 @@ GEM
sass (~> 3.2.2)
sprockets (~> 2.8, < 3.0)
sprockets-rails (~> 2.0)
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
sexp_processor (4.7.0)
sidekiq (4.1.2)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
redis (~> 3.2, >= 3.2.1)
sinatra (1.4.7)
sinatra (1.4.8)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
Expand All @@ -166,7 +161,7 @@ GEM
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
thor (0.19.1)
thread_safe (0.3.5)
thread_safe (0.3.6)
tilt (1.4.1)
turbolinks (2.5.3)
coffee-rails
Expand All @@ -190,11 +185,10 @@ DEPENDENCIES
puma
rails (~> 4.2.0)
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
sidekiq (~> 4.0)
sinatra
turbolinks
uglifier (>= 1.3.0)

BUNDLED WITH
1.12.5
1.15.1
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ To later access the app in the browser open your `/etc/hosts` file with ~~vim~~

127.0.0.1 beer.docker

Then prepare and start the app:
Then start the app:

docker-compose up -d db # needs some time to initialize, so we start it first
docker-compose logs db # use this to check if db:mysql service has finished starting
docker-compose run web bundle exec rake db:setup # initialize the app's database
docker-compose up # start all services
docker-compose up

All containers should now be running and you should be able to navigate your browser to the page [http://beer.docker](http://beer.docker).
All containers should now magically be running and you should be able to navigate your browser to the page [http://beer.docker](http://beer.docker).

Sometimes it does not work on the first try. Maybe MySQL was not up yet? Some other thing happened? Delete the containers and start over.
To stop and remove all containers simply take everything down:

docker-compose down

## How it works

Expand All @@ -53,17 +52,20 @@ The nginx proxy container contains the docker-gen service. This listens for new

### db

The MySQL container which is just a default MySQL container. The data is stored locally in the `./data` directory. This means we can start and stop the container without losing data.
The MariaDB container which is just a default MariaDB container. The data is stored locally in a mapped volume. This means the data is persisted on your local machine, not in the Docker container. When you restart the Docker container `db` all your data should still be there. If you want to start from scratch just delete all the files in `./data`:

The root password is passed via an environment variable. Don't do that in production!
rm -rf ./data/*

### web

This container serves our Rails app in a Unicorn application server. Scaling up the app is as easy as running `docker-compose scale web=4`. Now you have 4 running web containers. The nginx proxy service acts as a load balancer in front of them and automatically picks new instances up.
This container serves our Rails app from a Puma application server. Scaling up the app is as easy as running `docker-compose scale web=4`. Now you have 4 running web containers. The nginx proxy service acts as a load balancer in front of them and automatically picks new instances up.

The command of the container is set to the `docker/start.sh` script. This script waits until it gets an answer from the MariaDB container. As soon as the database is up it checks if the database is already initialized. If not it runs `rake db:setup` to initialize the app's database.

### worker

This is another container based on the Rails app. But it does not run Unicorn but Sidekiq for processing background tasks (Brewing beer!).
This is another container based on the Rails app. But it does not run Puma but Sidekiq for processing background tasks (Brewing beer!).

### redis

We use Redis as the backend for [Sidekiq](http://sidekiq.org/).
Expand All @@ -74,4 +76,4 @@ This container runs a Memached instance for Rails fragment caching.

## About me

I'm a Ruby engineer at [nine.ch](https://nine.ch). I don't consider myself a Docker expert. But I had and still have fun experimenting with it. If you're interested or have question regarding this repo you'll find me on [Twitter](https://twitter.com/neckhair82).
I'm a Ruby engineer at [nine.ch](https://nine.ch). I had and still have fun experimenting with Docker. If you're interested or have a question regarding this repo you'll find me on [Twitter](https://twitter.com/neckhair82).
4 changes: 2 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ production:
adapter: mysql2
pool: 5
timeout: 5000
username: root
password: <%= ENV['DB_ENV_MYSQL_ROOT_PASSWORD'] %>
username: <%= ENV['MYSQL_USER'] %>
password: <%= ENV['MYSQL_PASSWORD'] %>
host: 'db'
database: beer_production
Loading