Skip to content

Instantly share code, notes, and snippets.

@nacengineer
Last active November 15, 2019 21:01
Show Gist options
  • Save nacengineer/041ad2cbe0ec4771f2472a710d297c19 to your computer and use it in GitHub Desktop.
Save nacengineer/041ad2cbe0ec4771f2472a710d297c19 to your computer and use it in GitHub Desktop.
Typical Rails + Docker Desktop Setup
version: '3'
services:
db:
image: postgres:12-alpine
volumes:
- ./tmp/db:/var/lib/postgresql/data
- ./tmp/backups:/backups
# runs on localhost:30001
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-user@example.com}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-pass1234}
volumes:
- ./tmp/pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-30001}:80"
restart: unless-stopped
depends_on:
- db
redis:
image: redis
restart: unless-stopped
# runs on localhost:3000
web:
# pass in your dotenv variables
env_file:
- .env.development
environment:
- REDIS_URL=redis://redis:6379/0
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
# same as Dockerfile install location
- .:/my-awesome-application
ports:
- "3000:3000"
depends_on:
- db
- sidekiq
- redis
tty: true
stdin_open: true
restart: unless-stopped
sidekiq:
environment:
- REDIS_URL=redis://redis:6379/0
build: .
volumes:
# same as Dockerfile install location
- .:/my-awesome-application
depends_on:
- redis
command: bundle exec sidekiq
restart: unless-stopped
# you can docker attach to this node and interact with your specs that are running via guard-rspec
guard:
build: .
volumes:
# same as Dockerfile install location
- .:/my-awesome-application
environment:
- RAILS_ENV=test
command: bundle exec guard --no-bundler-warning
# add this line for no prompt --no-interactions
tty: true
stdin_open: true
restart: unless-stopped
FROM ruby:2.5-alpine
RUN apk add --update \
bash \
build-base \
nodejs \
postgresql-dev \
postgresql-client \
libxml2-dev \
libxslt-dev \
tzdata \
&& rm -rf /var/cache/apk/*
# Build variables you can override at runtime
ARG app_name=my-awesome-application
ARG port=3000
# Map Build variables to Environment
ENV app_name $app_name
ENV port $port
RUN mkdir /$app_name
WORKDIR /$app_name
COPY Gemfile /$app_name/Gemfile
COPY Gemfile.lock /$app_name/Gemfile.lock
# optional if no nokogiri
# RUN bundle config build.nokogiri --use-system-libraries
RUN bundle install
COPY . /$app_name
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE $port
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
@nacengineer
Copy link
Author

nacengineer commented Nov 15, 2019

Guard section assumes specs running via rspec-guard. You can eliminate if not needed. If used you can attach with

docker attach network-name_guard_1 <- network-name is usually = to your folder name

And detach without stopping the container with CNTRL+p+q Google docker commands for more information

Also if you use binding.pry or binding.irb in your webapp code, you can attach similarly to your web application and interact with it.

Optional:

  • redis
  • sidekiq
  • pgadmin
  • guard

would be started with
docker-compose build omit build if images are already made
docker-compose up -d -d daemonizes the images

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment