Skip to content

Instantly share code, notes, and snippets.

@hulous
Last active October 21, 2022 15:40
Show Gist options
  • Save hulous/7a1634b93757785f83570a4453f03bc6 to your computer and use it in GitHub Desktop.
Save hulous/7a1634b93757785f83570a4453f03bc6 to your computer and use it in GitHub Desktop.
base rails docker config: only a dockerfile and a docker-compose.yml (with a makefile ;-)) Notice the app_name and application_name must be adapted to your app.
DATABASE_HOST=app_name-db
DATABASE_USER=postgres
DATABASE_PASSWORD=password
version: '3'
services:
app_name-db:
image: postgres:latest
ports:
- "5434:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
env_file:
- .env.local
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
app_name-web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app_name
- bundle-path:/bundle
env_file:
- .env.local
environment:
- BUNDLE_PATH=/bundle/vendor
ports:
- "3001:3000"
depends_on:
- app_name-db
volumes:
postgres-data:
driver: local
bundle-path:
driver: local
FROM docker.io/library/ruby:3.1.2-buster AS app_builder
RUN apt-get update -qq && \
apt-get install -y --no-install-recommends nodejs build-essential libpq-dev graphviz && \
rm -rf /var/lib/apt/lists/*
FROM app_builder AS app_runner
RUN mkdir /app_name
WORKDIR /app_name
COPY Gemfile* ./
RUN gem install bundler
RUN bundle install
COPY . .
APPLICATION_NAME = app_name
WEB_CONTAINER_NAME = $(APPLICATION_NAME)-web
.DEFAULT_GOAL := help
CONTAINER_APP_MANAGER = docker ## Podman vs Docker
# Docker stuff
attach: ## Attach running web container to see logs
$(CONTAINER_APP_MANAGER) attach $(APPLICATION_NAME)_$(WEB_CONTAINER_NAME)_1
up: ## Run containers
$(CONTAINER_APP_MANAGER) compose up -d
down: ## Stop containers
$(CONTAINER_APP_MANAGER) compose down
serve: up ## Run Serve
make attach
ps: ## List containers
$(CONTAINER_APP_MANAGER) compose ps
restart_web: ## Restart Web container
$(CONTAINER_APP_MANAGER) compose restart $(WEB_CONTAINER_NAME)
# Build containers
build: ## Build containers
$(CONTAINER_APP_MANAGER) compose build
clean: down ## Cleanup docker images.
$(CONTAINER_APP_MANAGER) system prune -a
rebuild: clean ## Stop containers, delete docker images not used and build container
make build
# Rails install tools
bundle: ## Run bundle install
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle install
migrate: ## Run rake db migrate
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec rake db:migrate
init_db: ## drop, create and migrate DB (Data will be lost)
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec rake db:drop db:create db:migrate
import_seeds: ## Run Wecasa rake task to import seeds
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec rake db:seeds:wecasa_tech_test:import
# Devs console
bash: ## Run bash in web-container
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bash
console: ## Run Rails console
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec rails c
# Code smells tools
sniff: ## Run code smelling tools (rubocop, reek, brakeman and fasterer only for now)
- make rubocop
- make reek
rubocop: ## Run project rubocop
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec rubocop .
reek: ## Run project reek
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec reek .
brakeman: ## Run project brakeman
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec brakeman .
fasterer: ## Run project fasterer
$(CONTAINER_APP_MANAGER) compose run --rm $(WEB_CONTAINER_NAME) bundle exec fasterer .
# tests
tests: ## Run test locally in web container (with failfast and code coverage option)
$(CONTAINER_APP_MANAGER) compose run -e COVERAGE=true --rm $(WEB_CONTAINER_NAME) bundle exec rspec .
# Other things
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment