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: # or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global' # git config --global core.excludesfile '~/.gitignore_global'
# Ignore the actual contents of .env
.env
# Ignore bundler config. # Ignore bundler config.
/.bundle /.bundle

View File

@@ -29,5 +29,4 @@ ENTRYPOINT ["/app/docker-entrypoint.sh"]
EXPOSE 3000 EXPOSE 3000
# Start the main process. # No default CMD is provided in Dockerfile
CMD ["foreman", "start", "-p", "3000"]

View File

@@ -1,6 +1,6 @@
<nav class="navbar navbar-expand-md navbar-light"> <nav class="navbar navbar-expand-md navbar-light">
<div class="container"> <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"> <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> <span class="navbar-toggler-icon"></span>
</button> </button>

View File

@@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Astuto</title> <title><%= Rails.application.name %></title>
<%= csrf_meta_tags %> <%= csrf_meta_tags %>
<%= csp_meta_tag %> <%= 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 # Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading # -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application. # the framework and any gems in your application.
def name
ENV["APP_NAME"]
end
end end
end end

View File

@@ -19,8 +19,8 @@ default: &default
encoding: unicode encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: db host: db
username: postgres username: <%= ENV['POSTGRES_USER'] %>
password: password: <%= ENV['POSTGRES_PASSWORD'] %>
development: development:
<<: *default <<: *default
@@ -82,5 +82,3 @@ test:
production: production:
<<: *default <<: *default
database: app_production database: app_production
username: app
password: <%= ENV['APP_DATABASE_PASSWORD'] %>

View File

@@ -2,12 +2,19 @@ version: '3'
services: services:
db: db:
image: postgres image: postgres
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
volumes: volumes:
- ./tmp/db:/var/lib/postgresql/data - ./tmp/db:/var/lib/postgresql/data
web: web:
build: . build: .
environment: environment:
- UPDATE=0 - UPDATE=0
- ENVIRONMENT
- APP_NAME
- POSTGRES_USER
- POSTGRES_PASSWORD
volumes: volumes:
- .:/app - .:/app
ports: ports:

View File

@@ -1,8 +1,20 @@
#!/bin/bash #!/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. # Exit immediately if a command exits with a non-zero status.
set -e 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 if [ "$UPDATE" = 1 ]; then
# Create database, load schema, run migrations and seed data in an idempotent way. # Create database, load schema, run migrations and seed data in an idempotent way.
echo "Preparing database..." echo "Preparing database..."
@@ -17,8 +29,19 @@ if [ "$UPDATE" = 1 ]; then
exit 0 exit 0
fi fi
# Remove a potentially pre-existing server.pid for Rails. # Use case 2
rm -f /app/tmp/pids/server.pid if [ ! $# -eq 0 ]; then
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@" exec "$@"
exit 0
fi
# 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..." echo "Starting Astuto..."
docker-compose up "$@" docker-compose up "$@"

View File

@@ -1,4 +1,7 @@
#!/bin/bash #!/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-update.sh
/bin/bash script/docker-run.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 "Starting update..."
echo "-> Docker image will be rebuilt if necessary" echo "-> Docker image will be rebuilt if necessary"
echo "-> Database schema will be updated if necessary" echo "-> Database schema will be updated if necessary"