Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-french committed Mar 31, 2018
0 parents commit e9bc4e3
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
FROM heroku/heroku:16-build
MAINTAINER Jeff French <[email protected]>

# Which versions?
ENV RUBY_VERSION=2.5.1 \
BUNDLER_VERSION=1.15.2 \
NODE_VERSION=8.10.0 \
YARN_VERSION=1.0.2

# Setup the environment
ENV LC_ALL=en_US.UTF-8 \
GEM_PATH=/app/heroku/ruby/bundle/ruby/$RUBY_VERSION \
GEM_HOME=/app/heroku/ruby/bundle/ruby/$RUBY_VERSION \
PATH=/app/heroku/ruby/ruby-$RUBY_VERSION/bin:/app/heroku/ruby/node-$NODE_VERSION/bin:/app/heroku/ruby/yarn-$YARN_VERSION/bin:/app/user/bin:/app/heroku/ruby/bundle/ruby/$RUBY_VERSION/bin:$PATH \
BUNDLE_APP_CONFIG=/app/heroku/ruby/.bundle/config \
WORKDIR_PATH=/app/user \
SCRIPT_PATH=/app/profile.d

# Add the init script
COPY ./init.sh /usr/bin/init.sh

# Install all the tooling
RUN set -e ;\
echo "Setting up directories and installing Ruby v${RUBY_VERSION}, Node v${NODE_VERSION}, Yarn v${YARN_VERSION}, and Bundler v${BUNDLER_VERSION}..." ;\
#####
# Setup directories
#
mkdir -p "${WORKDIR_PATH}" ;\
mkdir -p "${SCRIPT_PATH}" ;\
mkdir -p /app/heroku/ruby/bundle/ruby/$RUBY_VERSION ;\
#####
# Make the init script executable
#####
chmod +x /usr/bin/init.sh ;\
#####
# Install Ruby
#####
mkdir -p /app/heroku/ruby/ruby-$RUBY_VERSION ;\
curl -s --retry 3 -L https://heroku-buildpack-ruby.s3.amazonaws.com/heroku-16/ruby-$RUBY_VERSION.tgz | tar xz -C /app/heroku/ruby/ruby-$RUBY_VERSION ;\
#####
# Install Node
#####
curl -s --retry 3 -L http://s3pository.heroku.com/node/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz | tar xz -C /app/heroku/ruby/ ;\
mv /app/heroku/ruby/node-v$NODE_VERSION-linux-x64 /app/heroku/ruby/node-$NODE_VERSION ;\
#####
# Install Yarn
#####
curl -s --retry 3 -L https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz | tar xz -C /app/heroku/ruby/ ;\
mv /app/heroku/ruby/yarn-v$YARN_VERSION /app/heroku/ruby/yarn-$YARN_VERSION ;\
#####
# Install Bundler
#####
gem install bundler -v $BUNDLER_VERSION --no-ri --no-rdoc ;\
#####
# export env vars during run time
#####
echo "cd /app/user/" > "${SCRIPT_PATH}/home.sh" ;\
echo "export PATH=\"$PATH\" GEM_PATH=\"$GEM_PATH\" GEM_HOME=\"$GEM_HOME\" SECRET_KEY_BASE=\"$SECRET_KEY_BASE\" BUNDLE_APP_CONFIG=\"$BUNDLE_APP_CONFIG\"" > "${SCRIPT_PATH}/ruby.sh"

WORKDIR /app/user

# Run bundler to cache dependencies
ONBUILD COPY ["Gemfile", "Gemfile.lock", "${WORKDIR_PATH}/"]
ONBUILD RUN bundle install --path /app/heroku/ruby/bundle --jobs 4

# run npm or yarn install
# add yarn.lock to .slugignore in your project
ONBUILD COPY package*.json yarn.* "${WORKDIR_PATH}/"
ONBUILD RUN [ -f yarn.lock ] && yarn install --no-progress || npm install

# Add all files
ONBUILD ADD . "${WORKDIR_PATH}"

ENTRYPOINT ["/usr/bin/init.sh"]
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Heroku Rails Development with Docker
=====================================

This Docker image is intended to serve as a base for your Rails dev environment based on Heroku.


## Example Usage

### Dockerfile

All you need is to use this image as your base:

```dockerfile
FROM moonswitch/heroku-rails
```

### Docker Compose

Assuming you have a Rails app with the above Dockerfile in the root of the repo, you can setup a `docker-compose.yaml` file like this:

```yaml
services:
db:
image: postgres:10
volumes:
- postgresql-data:/var/lib/postgresql/data

app:
build: .
depends_on:
- db
- cache
volumes:
- ./:/app/user
environment:
DATABASE_URL: 'postgres://postgres:@db:5432/postgres'
ports:
- 3000:3000
command: bundle exec rails server
```
Then just run `docker-compose up` and visit `http://localhost:3000` in your browser.
33 changes: 33 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# just keep going if we don't have anything to install
set +e

if [ ! -f /etc/profile.d/secret.sh ]; then
# add secret key base to init
echo "export SECRET_KEY_BASE=\"$(openssl rand -base64 32)\"" > /etc/profile.d/secret.sh
chmod +x /etc/profile.d/secret.sh
fi
# export secret before anything
source /etc/profile.d/secret.sh

# if any changes to Gemfile occur between runs (e.g. if you mounted the
# host directory in the container), it will install changes before proceeding
if [ -f $WORKDIR_PATH/Gemfile ]; then
bundle check || bundle install --jobs 4
fi

if [ "$RAILS_ENV" == "production" ]; then
bundle exec rake assets:precompile
fi

for SCRIPT in $SCRIPT_PATH/*; do
[ -f "$SCRIPT" ] || continue
set -x;
source $SCRIPT;
{ set +x; } 2>/dev/null
done

set -e

exec "$@"

0 comments on commit e9bc4e3

Please sign in to comment.