
March 25, 2025
Introduction
When building a new Ruby gem, setting up a consistent and isolated development environment is crucial. In this article, I’ll walk you through how I set up a Docker-based scaffold for a Ruby gem, ensuring a clean and reproducible setup with PostgreSQL integration.
💎 Build & Distribute Your Own Ruby Gem! 🚀
Do you want to create reusable code packages and seamlessly integrate them into multiple applications? With the right setup, you can package your code, ensure maintainability, and share it across projects or the community.
📦 Start building today! If you need guidance or have questions,
👉 Get in Touch
Why Use Docker for Gem Development?
Docker allows you to:
- Keep dependencies isolated
- Ensure consistent environments across machines
- Avoid conflicts with system-wide Ruby versions and libraries
Setting Up the Environment
To containerize the development environment, we use a Dockerfile and docker-compose.yml.
Dockerfile
This Dockerfile sets up a lightweight Alpine-based Ruby environment:
FROM ruby:3.4.2-alpine
RUN apk update && apk upgrade && apk add --no-cache \
bash build-base libxml2-dev libxslt-dev postgresql-client postgresql-dev \
npm yarn tzdata shared-mime-info git imagemagick imagemagick-dev imagemagick-libs \
curl yaml-dev && \
gem install nokogiri
WORKDIR /app
COPY . .
RUN bundle install --jobs=10
RUN yarn install --modules-folder /node_modules
ENV PATH /usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/node_modules/.bin
docker-compose.yml
The docker-compose.yml file orchestrates the PostgreSQL database and the Ruby environment:
services:
db:
container_name: gem-rails.db
image: postgres:latest
environment:
- POSTGRES_USER=psql
- POSTGRES_PASSWORD=psql
ports:
- "5432:5432"
volumes:
- ${PWD}/local_development/init-database:/docker-entrypoint-initdb.d/
- db_store:/var/lib/postgresql/data
rails:
build:
dockerfile: Dockerfile
container_name: gem-rails.rails
depends_on:
- db
environment:
- BASE_URL=http://localhost:3000
- POSTGRES_USER=psql
- POSTGRES_PASSWORD=psql
- RAILS_ENV=development
- BUNDLE_GEM__TEST=rspec
- BUNDLE_GEM__CI=github
- BUNDLE_GEM__MIT=true
- BUNDLE_GEM__COC=true
- BUNDLE_GEM__CHANGELOG=true
- BUNDLE_GEM__LINTER=false
ports:
- "3000:3000"
tty: true
volumes:
- ${PWD}:/app
- ${PWD}/.root:/root
platform: linux/amd64
volumes:
db_store:
Creating the Gem Scaffold
Once the environment is ready, you can generate the gem scaffold by running the following commands:
# git clone https://github.com/ggerman/create_gem # cd create_gem # docker-compose -f docker-compose.yml run --rm rails bash # bundler gem ai --test=rspec
This will create a gem scaffold named ai with RSpec for testing.
Alternatively, you can use the create_gem repository to streamline the gem creation process. This tool provides a structured and efficient way to scaffold a new Ruby gem with best practices built-in.
Conclusion
By using Docker, we ensure that our gem development environment remains stable, version-controlled, and easily replicable. This setup is especially useful when working with teams or switching between projects without affecting your local system.
Let me know your thoughts or if you have suggestions for improving this setup!
