We've moved discussions to Discord

Did an "official" docker-compose.yml get posted for Docker deployments?

Jeff Helman
I see a couple of threads talking about this, but I'm not seeing a docker-compose.yml in the repo (GitLab).

Chris Oliver ,  is this out there somewhere?
Chris Oliver
I don't think so. I had fiddled with one, but I don't remember ever including one. I wouldn't mind having one. There's a discussion on here with some examples, but it seems like people setup Docker for Rails a million different ways.
Jeff Helman
Yeah. In fact, I burned many hours over the weekend trying to get it working (i.e., with an existing Rails app), but couldn't get past bundler failures.  Looking at SO, this seems to be a common problem, but nobody can agree on a definitive solution (nor could I make any of them work). 

I will try some more as I have the bandwidth, and if I crack that nut, I'll definitely share with this community.
Nick Willever
This is the docker-compose I use for development, and I use the Dockerfile/entrypoint/etc.. in production. Please note my entrypoint does a migrate, you may not want to do that. Maybe that helps?

docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    volumes:
      - ./docker/volumes/postgres:/var/lib/postgresql/data
  web:
    build:
      context: .
      args:
        RAILS_ENV: development
    environment:
      - RAILS_ENV=development
      - RAILS_DATABASE_HOST=db
    volumes:
      - .:/opt/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

entrypoint.sh

#!/bin/sh
set -e

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

# Do migrations
bundle exec rake db:migrate

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

gem_installer.sh

#!/usr/bin/env sh
  
echo
echo "RAILS ENVIRONMENT: ${RAILS_ENV}"                                                                                                                                                         
echo

if [[ "${RAILS_ENV}" == "production" ]]; then
  with_or_without='without'; \
else
  with_or_without='with'; \
fi

bundle install --jobs `expr $(cat /proc/cpuinfo | grep -c "cpu cores") - 1` \
  --retry 3 --${with_or_without}=development,test

apk del --no-cache --update build-base \
                            linux-headers \
                            git \
                            nodejs

Dockerfile

FROM ruby:2.6-alpine

RUN apk add --no-cache --update build-base \
                                ca-certificates \
                                linux-headers \
                                git \
                                postgresql-dev \
                                nodejs \
                                tzdata
RUN gem install bundler

ARG RAILS_ENV=""
ENV RAILS_ENV=$RAILS_ENV
ENV RAILS_LOG_TO_STDOUT true
ENV APP_PATH /opt/myapp
WORKDIR $APP_PATH
ADD Gemfile $APP_PATH
ADD Gemfile.lock $APP_PATH
ADD gem_installer.sh /usr/bin/
RUN /usr/bin/gem_installer.sh

COPY . $APP_PATH

RUN mv $APP_PATH/entrypoint.sh /usr/bin; chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]

EXPOSE 3000

CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]

database.yml

default: &default
  adapter: postgresql
  host: <%= Settings.database.host %>
  port: 5432
  username: <%= Settings.database.username %>
  password: <%= Settings.database.password %>
  pool: <%= Settings.database.pool %>
  timeout: 5000
  reconnect: true

development:
  <<: *default
  database: myapp-development

settings.yml

database:
  host: <%= ENV.fetch('RAILS_DATABASE_HOST', 'localhost') %>
  username: <%= ENV.fetch('RAILS_DATABASE_USERNAME', 'postgres') %>                                                            
  password: <%= ENV.fetch('RAILS_DATABASE_PASSWORD', 'postgres') %>
  pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
Jeff Helman
Nick Willever  thanks very much! I'll give this a try.
Steve Church
Nick Willever I've tried your set-up but I get the following error not sure why permission is denied: 
/bin/sh: /usr/bin/gem_installer.sh: Permission denied
Steve Church
Ignore me, I needed to make the `.sh` files Executables. 

David Lowry
Chris Oliver I can see there's a few skeleton bits in the Pro repo now, but it doesn't seem to quite work out of the box (as you said - everyone does it differently) would be it be an idea to have a Dockerfile / -compose setup that "just works" with a Heroku dyno / Dokku server included? (My next step is to look at the above samples!) [or....is the sample setup meant to "just work" in some particular scenario?] Thanks!!

In my I missed the heroku section of this page https://jumpstartrails.com/docs/deploying (and got it working with Dokku after some failures); nonetheless, a fully featured "local" docker build / compose to rig together all the dependencies (out of the box) would be wonderful.
Nick Willever
Chris Oliver  Let me know if you want a merge request of those example files above or feel free to take them as you see fit.
Chris Oliver
Julian Rubisch submitted some Docker changes for development (but not production I believe) that I merged in.

It would be great to have some production-ready stuff as well, so feel free to make a PR. 👍
Shaunak Vairagare
Nick Willever isn't it missing yarn install  to pull down js and webpacker dependencies? Should it go after bundle install in the gem_installer.sh ?
Nick Willever
Shaunak Vairagare - Your correct, it can go after the gem installer file. I was using it as an API server.

For production-level I would need to add:

rake assets:precompile

And add node env variable to Dockerfile

NODE_ENV=production bin/webpack
Notifications
You’re not receiving notifications from this thread.