Add .env file for docker-compose

This commit is contained in:
riggraz
2019-09-23 12:14:35 +02:00
parent 3ce611374b
commit 10f73d876e
13 changed files with 84 additions and 12 deletions

6
.env-example Normal file
View File

@@ -0,0 +1,6 @@
ENVIRONMENT=development
POSTGRES_USER=yourusernamehere
POSTGRES_PASSWORD=yourpasswordhere
APP_NAME="You App Name Here"

3
.gitignore vendored
View File

@@ -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

View File

@@ -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

View File

@@ -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>

View File

@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Astuto</title>
<title><%= Rails.application.name %></title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

19
check-env.sh Normal file
View File

@@ -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

View File

@@ -15,5 +15,9 @@ module App
# 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

View File

@@ -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'] %>

View File

@@ -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:

View File

@@ -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

View File

@@ -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 "$@"

View File

@@ -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

View File

@@ -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"