Skip to content

Commit

Permalink
Merge pull request #2424 from TracksApp/docker-fix
Browse files Browse the repository at this point in the history
Better Docker config: Use entrypoint to setup the environment, run in…
  • Loading branch information
ZeiP authored Jul 19, 2020
2 parents 6701282 + 5bb7233 commit b57a62b
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 115 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ COPY . /app/
COPY config/database.docker.yml /app/config/database.yml
COPY config/site.docker.yml /app/config/site.yml

COPY docker-startserver.sh /
RUN RAILS_ENV=production bundle exec rake assets:precompile

ENTRYPOINT ["/app/docker-entrypoint.sh"]

EXPOSE 3000

#CMD ["rails", "server", "-e", "production", "-b", "0.0.0.0"]
CMD ["./docker-startserver.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]
2 changes: 1 addition & 1 deletion bin/bundle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
unless ENV["RAILS_ENV"] == "production" || File.exist?("#{__dir__}/../.skip-docker")
if File.exist?("#{__dir__}/../.use-docker")
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
Expand Down
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
unless ENV["RAILS_ENV"] == "production" || File.exist?("#{__dir__}/../.skip-docker")
if File.exist?("#{__dir__}/../.use-docker")
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end

Expand Down
2 changes: 1 addition & 1 deletion bin/rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
unless ENV["RAILS_ENV"] == "production" || File.exist?("#{__dir__}/../.skip-docker")
if File.exist?("#{__dir__}/../.use-docker")
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end
require_relative '../config/boot'
Expand Down
2 changes: 1 addition & 1 deletion bin/spring
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
unless ENV["RAILS_ENV"] == "production" || File.exist?("#{__dir__}/../.skip-docker")
if File.exist?("#{__dir__}/../.use-docker")
exec("#{__dir__}/../script/docker-environment", $PROGRAM_NAME, *ARGV) unless File.exist?("/etc/app-env")
end

Expand Down
56 changes: 18 additions & 38 deletions config/database.docker.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,24 @@
#development:
# adapter: mysql2
# database: tracks_dev
# # 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: docker
# port: 3307
# username: tracks_dev
# password: FqUKMWPz5mh8UPhypZvq

#development:
# adapter: postgresql
# database: tracks_dev
# # 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: docker
# port: 5432
# username: tracks_dev
# password: password

#development:
# adapter: sqlite3
# database: db.sqlite

#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: docker
# port: 3307
# username: tracks_tst
# password: 9rMNV4y6RVcqmJTo2QoR
test:
adapter: <%= ENV.fetch('DATABASE_TYPE') %>
encoding: <%= ENV.fetch('DATABASE_ENCODING') %>
database: <%= ENV.fetch('DATABASE_NAME') %>
host: <%= ENV.fetch('DATABASE_HOST') %>
port: <%= ENV.fetch('DATABASE_PORT') %>
username: <%= ENV.fetch('DATABASE_USERNAME') %>
password: <%= ENV.fetch('DATABASE_PASSWORD') %>

# Production config is disabled by default
#
development:
adapter: <%= ENV.fetch('DATABASE_TYPE') %>
encoding: <%= ENV.fetch('DATABASE_ENCODING') %>
database: <%= ENV.fetch('DATABASE_NAME') %>
host: <%= ENV.fetch('DATABASE_HOST') %>
port: <%= ENV.fetch('DATABASE_PORT') %>
username: <%= ENV.fetch('DATABASE_USERNAME') %>
password: <%= ENV.fetch('DATABASE_PASSWORD') %>

production:
adapter: <%= ENV.fetch('DATABASE_TYPE') %>
encoding: <%= ENV.fetch('DATABASE_ENCODING') %>
database: <%= ENV.fetch('DATABASE_NAME') %>
host: <%= ENV.fetch('DATABASE_HOST') %>
port: <%= ENV.fetch('DATABASE_PORT') %>
Expand Down
32 changes: 0 additions & 32 deletions config/database.yml

This file was deleted.

6 changes: 0 additions & 6 deletions config/site.docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ secret_token: "secret"
# Set to true when your application is running with https
force_ssl: false

# Configure how static assets (images, stylesheets, etc.) will be served.
# The best practice is to have a proxying web server such as Apache or Nginx
# serve static assets (images, stylesheets, javascript) for you. Change
# this to 'true' if you want Rails to be responsible for serving the static assets
# serve_static_assets: false

# Uncomment if you want to dispatch todos that come from email based on the To:
# address rather than the From: address.
# email_dispatch: 'to'
Expand Down
6 changes: 0 additions & 6 deletions config/site.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ secret_token: "change-me"
# Set to true when your application is running with https
force_ssl: false

# Configure how static assets (images, stylesheets, etc.) will be served.
# The best practice is to have a proxying web server such as Apache or Nginx
# serve static assets (images, stylesheets, javascript) for you. Change
# this to 'true' if you want Rails to be responsible for serving the static assets
# serve_static_assets: false

# Uncomment if you want to dispatch todos that come from email based on the To:
# address rather than the From: address.
# email_dispatch: 'to'
Expand Down
29 changes: 19 additions & 10 deletions doc/installation.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Installing Tracks

The following instructions will guide you through the installation of Tracks from source.
Tracks can be installed several ways: You can run it through Docker, which is recommended because all requirements have already been taken care of for you, or you can install it on a custom server from source.

This description is intended for people installing Tracks from scratch. If you would like to upgrade an existing installation, please see the [upgrade documentation](upgrading.md).
Instructions for the Docker-based installation as well as other options are available in the Tracks wiki: https://github.com/TracksApp/tracks/wiki/Installation. The wiki also has tips and instructions for specific environments. These instructions are only for installation from source in a custom environment.

For alternative installation options and tips for specific environments, please see [Installation](https://github.com/TracksApp/tracks/wiki/Installation) on the wiki.
This description is intended for people installing Tracks from scratch. If you would like to upgrade an existing installation, please see the [upgrade documentation](upgrading.md).

## Prerequisites

Tracks has a few software requirements that must be satisfied before installation:

1. **Ruby**. Tracks requires Ruby 2.4 or greater, but is not tested with 2.7.
2. **Bundler**. Tracks requires a recent version of [Bundler](http://bundler.io) to handle the installation of dependencies. Bundler is typically installed by running `gem install bundler`.
3. **Database**. Tracks is tested on [MySQL](http://www.mysql.com/) and [SQLite](http://www.sqlite.org/), but [PostgreSQL](http://www.postgresql.org/) can also be used. Of the three, SQLite requires the least configuration. Whatever your choice, the appropriate database software must be installed.
3. **Database**. Tracks is tested on [MySQL](http://www.mysql.com/) and [SQLite](http://www.sqlite.org/), but [PostgreSQL](http://www.postgresql.org/) can also be used. Of the three, SQLite requires the least configuration but is also the least performant and may make it difficult to operate in the future. We recommend either MySQL or PostgreSQL. Whatever your choice, the appropriate database software must be installed.

## Get Tracks

Expand All @@ -29,15 +29,23 @@ There are two methods of downloading Tracks:

## Set up the database

*This section only applies if you will be using Tracks with a MySQL database.*
*This section doesn't apply if using SQLite.*

You need to create a database and database-user to use with Tracks. For this, you can use an GUI tool or go into a terminal and issue the following commands:

You need to create a database and database-user to use with Tracks. For this, you can use MySQL Administrator or go into a terminal and issue the following commands:
### MySQL

mysql -u root -p
$ mysql -u root -p
mysql> CREATE DATABASE tracks;
mysql> GRANT ALL PRIVILEGES ON tracks.* TO yourmysqluser@localhost \
IDENTIFIED BY 'password-goes-here' WITH GRANT OPTION;

### PostgreSQL

$ sudo -u postgres psql
postgres=# CREATE USER tracks WITH ENCRYPTED PASSWORD 'password-goes-here';
postgres=# CREATE DATABASE tracks OWNER=tracks;

## Install dependencies

Tracks is built upon a number of Ruby libraries (known as ‘gems’). The Bundler tool makes it easy to install all the gems that Tracks needs, and ensures that they are all the correct versions.
Expand All @@ -64,8 +72,7 @@ Tracks is built upon a number of Ruby libraries (known as ‘gems’). The Bundl
2. Open the file `config/database.yml` and edit the `production:` section with the details of your database. If you are using MySQL the `adapter:` line should read `adapter: mysql2`, `host: localhost` (in the majority of cases), and your username and password should match those you assigned when you created the database. If you are using SQLite3, you should have only two lines under the production section: `adapter: sqlite3` and `database: db/tracks.db`.
3. Open the file `config/site.yml`, and read through the settings to make sure that they suit your setup. In most cases, all you need to change are the `secret_token`, the administrator email address (`admin_email`), and the time zone setting. For the time zone setting you can use the command `bundle exec rake time:zones:local` to see all available timezones on your machine
4. If you are using Windows, you may need to check the ‘shebang’ lines (`#!/usr/bin/env ruby`) of the `/public/dispatch.*` files and all the files in the `/script` directory. They are set to `#!/usr/bin/env ruby` by default. This should work for all Unix based setups (Linux or Mac OS X), but Windows users will probably have to change it to something like `#c:/ruby/bin/ruby` to point to the Ruby binary on your system.
5. If you intend to deploy Tracks using its included web server, you’ll need to uncomment and change the `serve_static_assets` configuration option to `true` in `config/site.yml` in order for the images, stylesheets, and javascript files to be served correctly.
6. If you intend to use Tracks behind a web server or reverse proxy with https enabled, ensure to set `force_ssl` option to `true`.
5. If you intend to use Tracks behind a web server or reverse proxy with https enabled, ensure to set `force_ssl` option to `true`.

## Populate your database with the Tracks schema

Expand All @@ -85,10 +92,12 @@ Static assets (images, stylesheets, and javascript) need to be compiled in order

While still in the Terminal inside the Tracks root directory, issue the following command:

bundle exec rails server -e production
RAILS_SERVE_STATIC_FILES=TRUE bundle exec rails server -e production

If all goes well, you should see some text informing you that the server is running: `=> Rails application starting in production on http://localhost:3000`. If you are already running other services on port 3000, you need to select a different port when running the server, using the `-p` option.

Optimally you should serve static files using Nginx or Apache, especially in larger production instances. If you do this, you can omit the RAILS_SERVE_STATIC_FILES=TRUE from the start of the command.

## Visit Tracks in a browser

Visit `http://localhost:3000/signup` in a browser (or whatever URL and port was reported when you started the server in the step above) and chose a user name and password for admin user. Once logged in as admin, you can add other (ordinary level) users. If you need to access Tracks from a mobile/cellular phone browser, visit `http://yourdomain.com/mobile/`. This mobile version is a special, lightweight version of Tracks, designed to use on a mobile browser.
Expand Down
9 changes: 9 additions & 0 deletions doc/upgrading.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Upgrading Tracks
## Upgrading from Tracks 2.4.2 to 2.5

* If you're using the Docker Compose environment and want to run the commands in
the bin/ directory inside the container from the host system, add a .use-docker
file to the root directory. This replaces the old .skip-docker file requirement
to favor the more common setup and avoid placing unexpected requirements.

* The Docker environment has been changed quite a bit. However, it should work
at least as before for the usual needs.

## Upgrading from Tracks 2.3 to 2.4.2

Expand Down
10 changes: 9 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ services:
- db-data:/var/lib/mysql
web:
build: .
environment:
# These are set in script/ci-build, so we need to pass-thru them.
RAILS_ENV: $RAILS_ENV
DATABASE_NAME: $DATABASE_NAME
DATABASE_USERNAME: root
DATABASE_PASSWORD_EMPTY: 1
volumes:
- ${VOLUME:-.:/app}
- ${VOLUME:-.}:/app:Z
- ${VOLUME:-.}/config/database.docker.yml:/app/config/database.yml:Z
- ${VOLUME:-.}/config/site.docker.yml:/app/config/site.yml:Z
ports:
- 3000:3000
depends_on:
Expand Down
22 changes: 22 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

export RAILS_ENV=${RAILS_ENV:-production}
export DATABASE_NAME=${DATABASE_NAME:-tracks}
export DATABASE_HOST=${DATABASE_HOST:-db}
export DATABASE_PORT=${DATABASE_PORT:-3306}
export DATABASE_USERNAME=${DATABASE_USERNAME:-tracks}
if [ "$DATABASE_PASSWORD_EMPTY" != 1 ];
then
export DATABASE_PASSWORD=${DATABASE_PASSWORD:-password}
else
export DATABASE_PASSWORD=""
fi
export DATABASE_TYPE=${DATABASE_TYPE:-mysql2}
export DATABASE_ENCODING=${DATABASE_ENCODING:-utf8}

export RAILS_SERVE_STATIC_FILES=TRUE
export RAILS_LOG_TO_STDOUT=TRUE

rails db:migrate

exec "$@"
4 changes: 0 additions & 4 deletions docker-startserver.sh

This file was deleted.

6 changes: 1 addition & 5 deletions script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ function die() {
exit 1
}


trap cleanup EXIT

export RAILS_ENV=test
export TRACKS_DB=tracks_test

# Put a config/site.yml file in place since it's needed for operation
cp config/site.yml.tmpl config/site.yml
export DATABASE_NAME=tracks_test

$docker_compose build
$docker_compose up -d
Expand Down
9 changes: 3 additions & 6 deletions script/docker-environment
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/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.
# Find our app dir
appdir=$(cd $(dirname "$0")/.. && pwd)
[ -f /etc/app-env ] && exec "$@"

# Otherwise, run docker compose to run our command in the container
# Check if we've been told to run the command in Docker Composer.
cmd="$@"; [ "$#" -eq 0 ] && cmd=bash
export VOLUME="$appdir:/app"
export VOLUME="$appdir"
image=${DOCKER_IMAGE:=web}

port_publish=""; [ "${BIND_DOCKER_SERVICE_PORTS:-}" = 1 ] && port_publish="--service-ports"
Expand Down

0 comments on commit b57a62b

Please sign in to comment.