Skip to content

Instantly share code, notes, and snippets.

@frankyston
Last active June 19, 2023 04:21
Show Gist options
  • Save frankyston/b2d785d155d5698159b715e1d78267e2 to your computer and use it in GitHub Desktop.
Save frankyston/b2d785d155d5698159b715e1d78267e2 to your computer and use it in GitHub Desktop.
Dockerizar um projeto rails ou criar um novo projeto rails com docker

Setup of project

  1. Create file .env and env.test off file .env.sample.

  2. Add values in the variables empties

  3. Add value below on file .env

DATABASE_URL=postgres://postgres:postgres@db:5432/projeto_development
  1. Add value below on file .env.test
DATABASE_URL=postgres://postgres:postgres@db:5432/projeto_test
  1. Create file .env.sidekiq and add values below:
REDIS_URL=redis://redis:6379/0
SIDEKIQ_WORKERS=5
DATABASE_URL=postgres://postgres:postgres@db:5432/projeto_development
  1. Execute the command below for build image
docker-compose up -d --no-deps --build app
  1. Execute the command below for start project
docker compose up -d
  1. Execute the command below for access bash
docker compose run --rm app bash
  1. Execute the command below for create database
rails db:create
  1. Execute the command below for migration database
rails db:migrate
  1. Execute the command below for populate database
rails db:seed

Command extras

Reset stats sidekiq

Sidekiq::Stats.new.reset

For the test rspec, execute the command below

ENV_FILE=.env.test docker compose run --rm app rspec

For execute rubocop

docker compose run --rm app rubocop

For debug with binding.pry

docker attach --detach-keys="ctrl-c" <id_do_container_app>
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV["DATABASE_URL"] %>
development:
<<: *default
test:
<<: *default
homolog:
<<: *default
production:
<<: *default
version: "2"
services:
app:
build:
context: .
image: nome_da_imagem
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
- projeto_bundle:/usr/local/bundle
ports:
- 3000:3000
networks:
- projeto_network
stdin_open: true
tty: true
env_file:
- ${ENV_FILE:-.env}
depends_on:
- db
db:
image: postgres:13.2-alpine
container_name: projeto_db
volumes:
- pg_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
networks:
- projeto_network
redis:
image: redis:6.0.12-alpine
container_name: projeto_redis
command: redis-server
volumes:
- redis_data:/data
ports:
- "6379:6379"
logging:
driver: none
networks:
- projeto_network
sidekiq:
image: nome_da_imagem_do_app
container_name: projeto_sidekiq
depends_on:
- app
- db
- redis
#mem_limit: 512m
volumes:
- .:/app
- alfasystem_bundle:/usr/local/bundle
command: dotenv -f ".env.sidekiq" bundle exec sidekiq -C config/sidekiq.yml
networks:
- projeto_network
mailcatcher:
restart: on-failure
image: dockage/mailcatcher:0.8.2
ports:
- "1080:1080"
- "1025:1025"
volumes:
pg_data:
redis_data:
projeto_bundle:
networks:
projeto_network:
name: projeto_network
FROM ruby:3.1.2
ARG NODE_MAJOR_VERSION=19
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR_VERSION.x | bash -
RUN apt-get update -qq && \
apt-get install -y build-essential libvips nodejs libpq-dev postgresql-client && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man && \
npm install -g yarn
RUN mkdir /app
WORKDIR /app
ADD . /app
RUN gem install bundler -v 2.3.26 # aqui você pode especificar com o que está no final do Gemfile.lock
RUN bundle install
@frankyston
Copy link
Author

frankyston commented Jan 19, 2023

Para novos projetos com rails 7 usar o comando abaixo:

 docker compose run --no-deps app rails new . -d postgresql -c tailwind -j esbuild

Alterar no docker-compose o command do app para chamar pelo bin/dev

Alterar o Procfile.dev para o rails server ter o -b '0.0.0.0'

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