Skip to content

Instantly share code, notes, and snippets.

@pablomarti
Forked from oinak/Dockerfile
Created April 8, 2020 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablomarti/1aca6fa0921fcd028e3d008d5bb0da41 to your computer and use it in GitHub Desktop.
Save pablomarti/1aca6fa0921fcd028e3d008d5bb0da41 to your computer and use it in GitHub Desktop.
Example docker config for rails develompent

Rails dev environmnent:

First time

This will build the local Dockerfile:

docker-compose build

This will download images once, then install bundle dependencies:

docker-compose run app --rm bundle install 

Modify to suit whatever data initialization your rails project has.

docker-compose run app --rm bundle exec rake db:create db:migrate db:seed

Normal use

Get the rails app running on the background:

 docker-compose up -d

Enter rails console (within running container)

docker-compose exec -ti app bundle exec rails console

To run the test suite:

docker-compose exec -ti app bundle exec rake test   # rails 4
docker-compose exec -ti app bundle exec rails test  # rails 5

To inspect logs:

docker-compose logs -f

To restart just one container

docker-compose restart app

To stop (but not destroy) the cluster

docker-compose stop

To start (a stopped) cluster

docker-compose start

To destroy the cluster (usefull if config or state gets messed up)

docker-compose down
version: '2'
services:
app: &app_base
build: .
working_dir: /usr/src/app
volumes:
- .:/usr/src/app
# to be able to forward ssh-agent to github throught capistrano (bundle on server)
- "~/.ssh/id_rsa:/root/.ssh/id_rsa"
- $SSH_AUTH_SOCK:$SSH_AUTH_SOCK
environment: &app_environment
TZ: Europe/Madrid
# to keep bundle effect between container restarts (withou rebuild):
BUNDLE_PATH: /usr/src/app/.bundle
BUNDLE_APP_CONFIG: /usr/src/app/.bundle
DATABASE_HOST: db
MEMCACHED_HOST: cache
SSH_AUTH_SOCK: # this left empty copies from outside env
command: 'bin/start'
ports:
- "3000:3000"
depends_on:
- db
- cache
- queue
- mail
queue:
# import app's config to run delayed_job workers
<<: *app_base
ports: [] # dont open 300 also, it would collide with app's
depends_on:
- db
- cache
- mail
environment:
<<: *app_environment
QUEUE: default
command: 'bin/start_queue'
cache:
image: memcached
ports:
- "11211"
mail:
image: hinshun/mailcatcher
ports:
- 1080:1080 # open browser here to warch mails sent by rails, use smtp to 1025 as config
db:
image: mysql:5.5
environment:
MYSQL_DATABASE: my_project_development
MYSQL_ROOT_PASSWORD: root
volumes:
# max_allowed_packet=32M' or any other custom config for mysql
- './config/mysql:/etc/mysql/conf.d/'
ports:
# expose the port on localhost to use db GUI
- "3306:3306"
## In case of postgres
# db:
# image: postgres
# ports:
# - "5432:5432"
# environment:
# POSTGRES_DB: my_project_development
# POSTGRES_USER: root
# POSTGRES_PASSWORD: root
FROM ruby:2.4
## In case of postgresql for heroku:
# RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" >> /etc/apt/sources.list.d/postgeresql.list \
# && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
# && apt-get update \
# && apt-get update \
# && apt-get install -y --no-install-recommends \
# postgresql-client-9.6 pv ack-grep ccze unp htop vim \
# && rm -rf /var/lib/apt/lists/* \
# && apt-get purge -y --auto-remove
## In case of mysql for amazon RDS
RUN apt-get update \
&& apt-get update \
&& apt-get install -y --no-install-recommends mysql-client nodejs pv \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge -y --auto-remove
ENV BUNDLER_VERSION 1.15.3
RUN gem install bundler --version "$BUNDLER_VERSION"
WORKDIR /usr/src/app
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
#!/usr/bin/env bash
# bin/start
echo "Removing old server pid's if any..."
rm -f tmp/pids/server.pid
echo "Checking bundle dependencies..."
bundle check || bundle install
echo "Booting up..."
bundle exec rails s -p 3000 -b 0.0.0.0 -e development
#!/usr/bin/env bash
# bin/start_queue
echo "Removing old server pid's if any..."
rm -f tmp/pids/delayed*.pid
echo "Checking bundle dependencies..."
bundle check || bundle install
echo "Booting up..."
bundle exec bin/delayed_job run -n 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment