Skip to content
Permalink
Browse files

Add .env file for docker-compose

  • Loading branch information
riggraz committed Sep 23, 2019
1 parent 3ce6113 commit 10f73d876e5b99b6c0a6174b98d15df0c4bb3793
Showing with 84 additions and 12 deletions.
  1. +6 βˆ’0 .env-example
  2. +3 βˆ’0 .gitignore
  3. +1 βˆ’2 Dockerfile
  4. +1 βˆ’1 app/views/layouts/_header.html.erb
  5. +1 βˆ’1 app/views/layouts/application.html.erb
  6. +19 βˆ’0 check-env.sh
  7. +4 βˆ’0 config/application.rb
  8. +2 βˆ’4 config/database.yml
  9. +7 βˆ’0 docker-compose.yml
  10. +27 βˆ’4 docker-entrypoint.sh
  11. +5 βˆ’0 script/docker-run.sh
  12. +3 βˆ’0 script/docker-update-and-run.sh
  13. +5 βˆ’0 script/docker-update.sh
@@ -0,0 +1,6 @@
ENVIRONMENT=development

POSTGRES_USER=yourusernamehere
POSTGRES_PASSWORD=yourpasswordhere

APP_NAME="You App Name Here"
@@ -4,6 +4,9 @@
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore the actual contents of .env
.env

# Ignore bundler config.
/.bundle

@@ -29,5 +29,4 @@ ENTRYPOINT ["/app/docker-entrypoint.sh"]

EXPOSE 3000

# Start the main process.
CMD ["foreman", "start", "-p", "3000"]
# No default CMD is provided in Dockerfile
@@ -1,6 +1,6 @@
<nav class="navbar navbar-expand-md navbar-light">
<div class="container">
<%= link_to 'Astuto', root_path, class: 'navbar-brand' %>
<%= link_to Rails.application.name, root_path, class: 'navbar-brand' %>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Astuto</title>
<title><%= Rails.application.name %></title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

@@ -0,0 +1,19 @@
#!/bin/bash

set -e # Abort on error

function check_env_vars () {
for name; do
: ${!name:?$name must not be empty}
done
}

if [ ! -f .env ]; then
echo 'A .env file must be present.'
exit 2
fi

if ! check_env_vars "ENVIRONMENT" "APP_NAME" "POSTGRES_USER" "POSTGRES_PASSWORD"; then
echo 'Some variables are not set in .env, please refer to script/check-env.sh for the list'
exit 4
fi
@@ -15,5 +15,9 @@ class Application < Rails::Application
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.

def name
ENV["APP_NAME"]
end
end
end
@@ -19,8 +19,8 @@ default: &default
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: db
username: postgres
password:
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>

development:
<<: *default
@@ -82,5 +82,3 @@ test:
production:
<<: *default
database: app_production
username: app
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
@@ -2,12 +2,19 @@ version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
environment:
- UPDATE=0
- ENVIRONMENT
- APP_NAME
- POSTGRES_USER
- POSTGRES_PASSWORD
volumes:
- .:/app
ports:
@@ -1,8 +1,20 @@
#!/bin/bash

# This file serves 3 use cases:
# 1: if the env variable UPDATE is 1, the database is prepared
# 2: if a command was supplied, it is executed
# 3: otherwise, check env variable ENVIRONMENT and launch server

# Exit immediately if a command exits with a non-zero status.
set -e

# Check environment variables
/bin/bash ./check-env.sh

# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid

# Use case 1
if [ "$UPDATE" = 1 ]; then
# Create database, load schema, run migrations and seed data in an idempotent way.
echo "Preparing database..."
@@ -17,8 +29,19 @@ if [ "$UPDATE" = 1 ]; then
exit 0
fi

# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid
# Use case 2
if [ ! $# -eq 0 ]; then
exec "$@"
exit 0
fi

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
# Use case 3
echo "Environment is: $ENVIRONMENT"
if [ $ENVIRONMENT == "development" ]; then
# Launch Rails server and webpack-dev-server using Foreman
foreman start -p 3000
else # production
# Compile assets with Webpack and then launch Rails server
./bin/webpack
rails server -e production
fi
@@ -1,3 +1,8 @@
#!/bin/bash

# Exit immediately if a command exits with a non-zero status.
set -e

echo "Starting Astuto..."

docker-compose up "$@"
@@ -1,4 +1,7 @@
#!/bin/bash

# Exit immediately if a command exits with a non-zero status.
set -e

/bin/bash script/docker-update.sh
/bin/bash script/docker-run.sh
@@ -1,3 +1,8 @@
#!/bin/bash

# Exit immediately if a command exits with a non-zero status.
set -e

echo "Starting update..."
echo "-> Docker image will be rebuilt if necessary"
echo "-> Database schema will be updated if necessary"

0 comments on commit 10f73d8

Please sign in to comment.
You can’t perform that action at this time.