Merge pull request #2103 from TracksApp/update-docker-configuration

Update the docker configuration
This commit is contained in:
Matt Rogers 2018-05-31 08:09:56 -05:00 committed by GitHub
commit a4476ae74a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 194 additions and 54 deletions

1
.gitignore vendored
View file

@ -19,7 +19,6 @@
/log/*.log
/public/assets/
/tmp
config/database.yml
config/deploy.rb
config/site.yml
db/data.yml

View file

@ -1,22 +1,19 @@
language: ruby
sudo: false
cache: bundler
rvm:
- 1.9.3
- 2.0.0
- 2.1
- 2.2
addons:
firefox: 45.8.0esr
sudo: true
services:
- docker
env:
- DATABASE_URL=mysql2://localhost/tracks_test
- DATABASE_URL=sqlite3:db/test.sqlite3
bundler_args: --without development --jobs=3 --retry=3
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- "cp config/site.yml.tmpl config/site.yml"
script: "if [[ $DATABASE_URL == mysql* ]]; then bundle exec rake ci:full; else bundle exec rake ci:lite; fi"
- DOCKER_COMPOSE_VERSION=1.21.0
before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod 0755 docker-compose
- sudo mv docker-compose /usr/local/bin
install: true
script: "script/cibuild"
addons:
apt:
packages:
- docker-ce
notifications:
email: false
irc:

View file

@ -1,41 +1,14 @@
FROM phusion/passenger-ruby22:0.9.18
FROM ruby:2.3
WORKDIR /home/app/tracks
WORKDIR /app
# Install gems
COPY Gemfile Gemfile.lock ./
RUN bundle install --without development test mysql therubyracer --jobs=3
RUN touch /etc/app-env
# Install and configure the app
ENV RAILS_ENV=production
ENV DATABASE_URL=sqlite3:/var/tracks/database.sqlite3
RUN mkdir /var/tracks && chmod 777 /var/tracks
RUN touch /var/tracks/database.sqlite3 && chown app.app /var/tracks/database.sqlite3
COPY . ./
COPY config/site.yml.tmpl /etc/tracks/config/site.yml
RUN ln -sf /etc/tracks/config/site.yml config/site.yml
RUN bundle exec rake assets:precompile
RUN chown -R app.app .
COPY Gemfile* /app/
RUN bundle install
# Configure nginx
RUN echo "env DATABASE_URL;" > /etc/nginx/main.d/env.conf
RUN echo "\
server {\n\
listen 80 default_server;\n\
server_name _;\n\
root /home/app/tracks/public;\n\
passenger_enabled on;\n\
passenger_user app;\n\
passenger_ruby /usr/bin/ruby2.2;\n\
}" > /etc/nginx/sites-enabled/tracks.conf
RUN rm /etc/nginx/sites-enabled/default
RUN rm -f /etc/service/nginx/down
RUN mkdir /app/log
# Migrate the database on startup
RUN echo "#!/bin/sh\nbundle exec rake db:migrate" > /etc/my_init.d/90_tracks.sh
RUN chmod +x /etc/my_init.d/90_tracks.sh
COPY . /app/
VOLUME ["/etc/tracks", "/var/tracks"]
# Default command for baseimage-docker
CMD ["/sbin/my_init"]
EXPOSE 3000

41
Dockerfile.passenger Normal file
View file

@ -0,0 +1,41 @@
FROM phusion/passenger-ruby22:0.9.18
WORKDIR /home/app/tracks
# Install gems
COPY Gemfile Gemfile.lock ./
RUN bundle install --without development test mysql therubyracer --jobs=3
# Install and configure the app
ENV RAILS_ENV=production
ENV DATABASE_URL=sqlite3:/var/tracks/database.sqlite3
RUN mkdir /var/tracks && chmod 777 /var/tracks
RUN touch /var/tracks/database.sqlite3 && chown app.app /var/tracks/database.sqlite3
COPY . ./
COPY config/site.yml.tmpl /etc/tracks/config/site.yml
RUN ln -sf /etc/tracks/config/site.yml config/site.yml
RUN bundle exec rake assets:precompile
RUN chown -R app.app .
# Configure nginx
RUN echo "env DATABASE_URL;" > /etc/nginx/main.d/env.conf
RUN echo "\
server {\n\
listen 80 default_server;\n\
server_name _;\n\
root /home/app/tracks/public;\n\
passenger_enabled on;\n\
passenger_user app;\n\
passenger_ruby /usr/bin/ruby2.2;\n\
}" > /etc/nginx/sites-enabled/tracks.conf
RUN rm /etc/nginx/sites-enabled/default
RUN rm -f /etc/service/nginx/down
# Migrate the database on startup
RUN echo "#!/bin/sh\nbundle exec rake db:migrate" > /etc/my_init.d/90_tracks.sh
RUN chmod +x /etc/my_init.d/90_tracks.sh
VOLUME ["/etc/tracks", "/var/tracks"]
# Default command for baseimage-docker
CMD ["/sbin/my_init"]

View file

@ -1,3 +1,6 @@
#!/usr/bin/env ruby
if ENV["RAILS_ENV"] != "production"
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')

View file

@ -1,4 +1,7 @@
#!/usr/bin/env ruby
if ENV["RAILS_ENV"] != "production"
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError

View file

@ -1,4 +1,8 @@
#!/usr/bin/env ruby
if ENV["RAILS_ENV"] != "production"
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'

View file

@ -1,4 +1,7 @@
#!/usr/bin/env ruby
if ENV["RAILS_ENV"] != "production"
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
require_relative '../config/boot'
require 'rake'
Rake.application.run

View file

@ -1,4 +1,7 @@
#!/usr/bin/env ruby
if ENV["RAILS_ENV"] != "production"
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
require 'pathname'
# path to your application root.

View file

@ -1,4 +1,7 @@
#!/usr/bin/env ruby
if ENV["RAILS_ENV"] != "production"
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
# This file loads spring without using Bundler, in order to be fast
# It gets overwritten when you run the `spring binstub` command

32
config/database.yml Normal file
View file

@ -0,0 +1,32 @@
development:
adapter: mysql2
database: tracks
# set this if you are storing utf8 in your mysql database to handle strings
# like "Réné". Not needed for sqlite. For PostgreSQL use encoding: unicode
# encoding: utf8
host: db
username: root
password:
test:
adapter: mysql2
database: tracks_test
# set this if you are storing utf8 in your mysql database to handle strings
# like "Réné". Not needed for sqlite. For PostgreSQL use encoding: unicode
# encoding: utf8
host: db
username: root
password:
# Production config is disabled by default
#
# production:
# adapter: mysql2
# database: tracks
# # set this if you are storing utf8 in your mysql database to handle strings
# # like "Réné".Not needed for sqlite. For PostgreSQL use encoding: unicode
# # encoding: utf8
# host: localhost
# username: root
# password:

19
docker-compose.yml Normal file
View file

@ -0,0 +1,19 @@
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: '1'
volumes:
- "db-data:/var/lib/mysql"
web:
build: .
volumes:
- "${VOLUME:-.:/app/workdir}"
ports:
- "3000:3000"
depends_on:
- db
volumes:
db-data:

32
script/cibuild Executable file
View file

@ -0,0 +1,32 @@
#!/bin/bash
set -e
docker_compose="docker-compose --file docker-compose.yml"
function cleanup() {
$docker_compose down
}
function die() {
echo $@
exit 1
}
trap cleanup EXIT
export RAILS_ENV=test
# Put a config/site.yml file in place since it's needed for operation
cp config/site.yml.tmpl config/site.yml
$docker_compose build
$docker_compose up -d
script/poll-for-db
# Leaving this in since it will be needed for Rails 5
# bin/rails db:environment:set RAILS_ENV=test || true
bin/rake db:reset
bin/rake ci:lite

16
script/docker-environment Executable file
View file

@ -0,0 +1,16 @@
#!/bin/sh
# Run a command in the app's environment
set -e
# Find our app dir and just run the command in we're in the container since the
# container is built with an /etc/app-env file inside of it.
appdir=$(cd $(dirname "$0")/.. && pwd)
[ -f /etc/app-env ] && exec "$@"
# Otherwise, run docker compose to run our command in the container
cmd="$@"; [ "$#" -eq 0 ] && cmd=bash
export VOLUME="$appdir:/app"
image=${DOCKER_IMAGE:=web}
exec docker-compose run --rm $image $cmd

12
script/poll-for-db Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
appdir=$(cd $(dirname "$0")/.. && pwd)
[ -f /etc/app-env ] || exec "$appdir/script/docker-environment" $0 $@
for i in {1..60}; do
curl -sf db:3306 >/dev/null && exit
sleep 1
done
echo "Unable to reach database!"
exit 1