Skip to content

Instantly share code, notes, and snippets.

@bondarolik
Last active July 15, 2024 15:00
Show Gist options
  • Save bondarolik/4b88d92ebe5b9676d5e783a43b73d5e4 to your computer and use it in GitHub Desktop.
Save bondarolik/4b88d92ebe5b9676d5e783a43b73d5e4 to your computer and use it in GitHub Desktop.
Docker & Docker Compose for Ruby on Rails
services:
db:
image: postgres:15.2-alpine # or define exact version, postgres:14
container_name: the_office_db
ports:
- "5432:5432"
environment:
- POSTGRES_DB=the_office_db
- POSTGRES_USER=db_user
- POSTGRES_PASSWORD=super_secure_password # change to secure password
volumes:
- pg_data:/var/lib/postgresql/data # where we will store data
redis:
image: redis:7.0.8-alpine # or define exact version, redis:6
container_name: the_office_redis
volumes:
- redis_data:/data # where we will store data
app:
container_name: the_office_app
build: .
# command: bin/rails s -b "0.0.0.0"
command: bin/dev
ports:
- "3000:3000"
stdin_open: true
tty: true
environment:
- HISTFILE=/app/log/.bash_history
- DATABASE_URL=postgresql://db_user:super_secure_password@db
- REDIS_URL=redis://redis:6379
- DOCKER=true
- HOST=localhost:3000
- SITE=localhost
- EMAIL=no-reply@yourmail.com
- NAME=Name of you app
depends_on:
- db
- redis
volumes:
- .:/app
- bundle:/gems
tmpfs:
# Use temporary file system, instead of manually removing files through `bash -c "rm -f tmp/pids/server.pid"`
- /tmp
- /app/tmp/pids
volumes:
pg_data:
redis_data:
bundle:
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
echo "Remove a potentially pre-existing server.pid for Rails..."
rm -f /usr/src/app/tmp/pids/server.pid
echo "Run bundle install..."
bundle check || bundle install --jobs 4
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
ARG RUBY_VERSION=3.2.1
FROM ruby:$RUBY_VERSION-slim
# Install essentials for running gems and app
RUN apt-get update -qq && apt-get install -yq --no-install-recommends \
build-essential \
postgresql-client \
gnupg2 \
less \
git \
git-core \
curl \
libssl-dev \
libreadline-dev \
libyaml-dev \
libsqlite3-dev \
libpq-dev \
libcurl4-openssl-dev \
postgresql-client \
libffi-dev \
libvips \
imagemagick \
dirmngr \
tzdata \
vim-tiny \
&& apt-get clean autoclean \
&& apt-get autoremove -y \
&& rm -rf \
/var/lib/apt \
/var/lib/dpkg \
/var/lib/cache \
/var/lib/log \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Update Gems and install Bundler
RUN gem update --system
RUN gem install bundler
# Add Bundle to $PATH
ENV BUNDLE_PATH /gems
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
# Setup workdir and copy code from local machine to container working directory
WORKDIR /app
COPY . /app/
# Copy Gemfile to container
COPY Gemfile Gemfile.lock /app/
ENTRYPOINT ["./bin/docker-entrypoint.sh"]
CMD ["bundle", "exec", "rails", "s", "-b", "0.0.0.0", "-p", "3000"]
# Expose port to defined
EXPOSE 3000
.git
.git/
.gitignore
#
# OS X
#
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
#
# Rails
#
.env
.env.sample
*.rbc
capybara-*.html
.rspec
log
tmp
db/*.sqlite3
db/*.sqlite3-journal
public/system
coverage/
spec/tmp
**.orig
rerun.txt
pickle-email-*.html
db/*.sqlite3
db/*.sqlite3-journal
.rvmrc
.byebug_history
node_modules
yarn-error.log
# if using bower-rails ignore default bower_components path bower.json files
vendor/assets/bower_components
*.bowerrc
bower.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment