Skip to content

Instantly share code, notes, and snippets.

@swilgosz
Last active May 2, 2022 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swilgosz/59f3aaa5bcebaddd90948f3ee7f703ae to your computer and use it in GitHub Desktop.
Save swilgosz/59f3aaa5bcebaddd90948f3ee7f703ae to your computer and use it in GitHub Desktop.
Development setup for dockerized Rails API applications.
RAILS_ENV=development
POSTGRES_ADAPTER=postgresql
POSTGRES_USER=postgres
POSTGRES_DB_NAME=sample_api_dev
POSTGRES_HOST=db
DB_PASSWORD=postgres
RAILS_LOG_TO_STDOUT=true
APP_HOME=/app/api
MEMCACHED_HOST=memcached
REDIS_URL=redis://redis:6379/0

Development setup for dockerized Rails API applications.

You can read a detailed description of the code in Driggl's blog.

Pre-requirements:

  1. Docker
  2. docker-compose
  3. docker-sync

Put files from this gist in your brand new rails project, then run:

# build images
docker build -t Dockerfile sample/api:dev .
docker build -t Dockerfile.test sample/api:test .

# EITHER
# run the whole thing at once
docker-sync-stack start

# OR 
# run it separately

# run the file synchronization
docker-sync start

# run the whole app stack
docker-compose up -d

References

Author: Sebastian Wilgosz

version: "3"
volumes:
sample-db-data:
sample-redis-data:
sample-api-sync:
external: true
services:
db:
image: "postgres:10.5"
volumes:
- sample-db-data:/var/lib/postgresql/data
redis:
image: "redis:alpine"
volumes:
- sample-redis-data:/var/lib/redis/data
memcached:
image: memcached:alpine
command: "memcached -m 500 -I 2m"
sidekiq:
image: sample/api:dev #can be changed to production later
depends_on:
- "db"
- "redis"
- "memcached"
command: sidekiq -C config/sidekiq.yml
env_file:
- .env_api_dev
volumes:
- "sourcer-api-sync:/app/api:nocopy"
- /app/api/tmp
api:
image: sample/api:dev
env_file:
- .env_api_dev
command: /app/api/bin/start
# command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]
volumes:
- "sourcer-api-sync:/app/api:nocopy"
- /app/api/tmp
ports:
- "3031:3000"
depends_on:
- "db"
- "redis"
- "memcached"
specs:
image: sample/api:test
env_file:
- .env_api_test
volumes:
- "sample-api-sync:/app/api:nocopy"
- /app/api/tmp
depends_on:
- "db"
- "redis"
- "memcached"
version: "2"
options:
compose-file-path: "docker-compose.yml"
verbose: false
cli_mode: "auto"
max_attempt: 5
project_root: "config_path"
syncs:
sample-api-sync:
src: "."
notify_terminal: true
sync_excludes: ["log/*", ".sass-cache", ".gitignore", ".git", "*.dump"]
# Base image
FROM ruby:2.6.5
# Installation of dependencies
RUN apt-get update -yqq \
&& apt-get install -yqq --no-install-recommends \
build-essential \
libpq-dev \
nodejs \
locales \
&& apt-get clean autoclean \
&& apt-get autoremove -y \
&& rm -rf \
/var/lib/apt \
/var/lib/dpkg/* \
/var/lib/cache \
/var/lib/log
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
# Add our Gemfile
# and install gems
COPY Gemfile* /tmp/
WORKDIR /tmp
RUN bundle install
# Create a directory for our application
# and set it as the working directory
ENV APP_HOME /app/api
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
# Copy over our application code
COPY . $APP_HOME
EXPOSE 3000
CMD bin/start
# Base image
FROM ruby:2.6.5
# Installation of dependencies
RUN apt-get update -yqq \
&& apt-get install -yqq --no-install-recommends \
build-essential \
libpq-dev \
nodejs \
locales \
&& apt-get clean autoclean \
&& apt-get autoremove -y \
&& rm -rf \
/var/lib/apt \
/var/lib/dpkg/* \
/var/lib/cache \
/var/lib/log \
&& locale-gen en_US.UTF-8
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
# Add our Gemfile
# and install gems
COPY Gemfile* /tmp/
WORKDIR /tmp
RUN bundle install
# Create a directory for our application
# and set it as the working directory
ENV APP_HOME /app/api
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
# Copy over our application code
COPY . $APP_HOME
EXPOSE 3000
CMD bin/start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment