Last active
March 1, 2022 14:16
-
-
Save ilude/2d072747a4363160b9600140e6ed8a0f to your computer and use it in GitHub Desktop.
Notes on docker and building a new rails project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notes on docker and building a new rails project |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat > options << EOF | |
--force | |
--skip-action-cable | |
--skip-action-mailbox | |
--skip-action-text | |
--skip-active-record | |
--skip-active-storage | |
--skip-bootsnap | |
--skip-bundle | |
--skip-git | |
--skip-javascript | |
--skip-system-test | |
--skip-test | |
--skip-turbolinks | |
--asset-pipeline=propshaft | |
EOF | |
cat > Dockerfile << EOF | |
FROM ruby:alpine | |
RUN apk --no-cache add build-base git | |
RUN gem install rails | |
RUN mkdir /app | |
WORKDIR /app | |
COPY options /tmp/options | |
CMD rails new . --rc=/tmp/options | |
EOF | |
sudo rm -r base | |
mkdir base | |
docker build -t builder . | |
docker run --rm -it -v $(pwd)/base:/app --user $(id -u):$(id -g) builder | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export APPNAME=<your_app_name> | |
mkdir -p $APPNAME/base | |
cd $APPNAME | |
docker run --rm -v $(pwd)/base:/app -w /app -ti ruby:alpine sh | |
# install a basic build env | |
apk --no-cache add build-base | |
echo "source 'https://rubygems.org'" > Gemfile | |
echo git_source(:github) { |repo| "https://github.com/#{repo}.git" } > Gemfile | |
echo "gem 'rails'" >> Gemfile | |
bundle install | |
# rails minimal options | |
# https://www.bootrails.com/blog/rails-new-options/ | |
bundle exec rails new . --minimal --force --skip-bundle --skip-git --no-skip-action-mailer | |
exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker run --rm -v $(pwd):/app -w /app -ti ruby:alpine sh | |
# install a basic build env | |
apk --no-cache add build-base freetds-dev git | |
export APP=app_name | |
# https://stackoverflow.com/a/52979051/1973777 | |
# set a specific rails version | |
mkdir $APP | |
cd $APP | |
echo "source 'https://rubygems.org'" > Gemfile | |
echo "gem 'rails', '~> 5.2'" >> Gemfile | |
bundle install | |
bundle exec rails new . --force --skip-bundle --database=sqlserver | |
bundle update | |
# old stuff | |
# gem install rails -v '~> 5.2' | |
# rails new $APP --database=sqlserver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# build docker image named direwolf-image | |
docker build -t direwolf-image . | |
docker ps | |
docker stop direwolf | |
docker rm direwolf | |
docker rmi direwolf-image | |
# remove untagged images | |
docker rmi $( docker images -q -f dangling=true) | |
# or | |
docker rmi $(docker images | grep "^<none>" | awk "{print $3}") | |
# remove all stopped containers | |
docker rm $(docker ps -a -q) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bring up compose file services in daemon mode | |
docker-compose up -d | |
# stop running image | |
docker-compose stop | |
# pull new image | |
docker-compose pull | |
# watch logs | |
docker logs -f direwolf | |
# update an image | |
docker-compose pull | |
docker-compose up -d --remove-orphans |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ignore version control files: | |
.git/ | |
.gitignore | |
.gitattributes | |
# Ignore docker and environment files: | |
Dockerfile* | |
docker-compose*.yml | |
*.env | |
.dockerignore | |
.env | |
.drone.yml | |
README.* | |
Makefile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ignore enviroment files | |
.env | |
# Ignore bundler config. | |
/.bundle | |
# Ignore all logfiles and tempfiles. | |
/log/* | |
/tmp/* | |
!/log/.keep | |
!/tmp/.keep |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lets try the new docker build system | |
# https://docs.docker.com/develop/develop-images/build_enhancements/ | |
# https://www.docker.com/blog/faster-builds-in-compose-thanks-to-buildkit-support/ | |
export DOCKER_BUILDKIT := 1 | |
export DOCKER_SCAN_SUGGEST := false | |
export COMPOSE_DOCKER_CLI_BUILD := 1 | |
export BUILDKIT_PROGRESS=plain | |
# define docker-compose flags for environments | |
development_FLAGS := | |
staging_FLAGS := -f ./docker-compose.yml | |
production_FLAGS := -f ./docker-compose.yml | |
# include .env if present | |
ifneq (,$(wildcard ./.env)) | |
include .env | |
export | |
endif | |
# include app.env if present | |
ifneq (,$(wildcard ./app.env)) | |
include app.env | |
export | |
endif | |
# get current timestamp | |
export DATE := $(shell date '+%Y-%m-%d-%H.%M.%S') | |
export PUID := $(shell id -u) | |
export PGID := $(shell id -g) | |
# get the project name from the current directory | |
export PROJECT_NAME := $(notdir $(shell pwd)) | |
# if username not set we are in a drone environment | |
ifndef USERNAME | |
USERNAME := drone | |
endif | |
export USERNAME | |
# check if we are in a drone environment | |
ifdef GIT_COMMITTER_EMAIL | |
EMAIL := $(GIT_COMMITTER_EMAIL) | |
else | |
EMAIL := $(shell git config user.email) | |
endif | |
export EMAIL | |
ifdef DRONE_REPO_BRANCH | |
local_branch := $(DRONE_REPO_BRANCH) | |
else | |
local_branch = $(shell git rev-parse --abbrev-ref HEAD | tr -d '\n') | |
endif | |
ifeq (master,$(local_branch)) | |
ENV_CONTEXT := production | |
else | |
ENV_CONTEXT := $(local_branch) | |
endif | |
# determine the name of the first service in the docker-compose.yml | |
export ATTACH_HOST := $(or $(shell awk 'f{print;f=0} /service/{f=1}' docker-compose.yml | sed -r 's/\s+|\://g'), webapp) | |
# If the first argument is "deploy"... | |
ifeq (deploy,$(firstword $(MAKECMDGOALS))) | |
ENV_CONTEXT := production | |
PASSED_COMMAND := $(word 2,$(MAKECMDGOALS)) | |
DEPLOY_STAGE := $(or $(PASSED_COMMAND),$(ENV_CONTEXT)) | |
# If the first argument is "test"... | |
else ifeq (test,$(firstword $(MAKECMDGOALS))) | |
# default DEPLOY_STAGE & ENV_CONTEXT to test | |
ENV_CONTEXT := $(or $(word 2,$(MAKECMDGOALS)),test) | |
DEPLOY_STAGE := $(or $(word 2,$(MAKECMDGOALS)),test) | |
else | |
# default DEPLOY_STAGE to development | |
ENV_CONTEXT := $(or $(word 2,$(MAKECMDGOALS)),development) | |
DEPLOY_STAGE := $(or $(word 2,$(MAKECMDGOALS)),development) | |
endif | |
export DEPLOY_STAGE | |
export ENV_CONTEXT | |
# set the docker-compose FLAGS based on the DEPLOY_STAGE value | |
FLAGS = $($(DEPLOY_STAGE)_FLAGS) | |
# use the rest as arguments as empty targets | |
EMPTY_TARGETS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) | |
$(eval $(EMPTY_TARGETS):;@:) | |
up: build | |
@-rm -f tmp/pids/*.pid # - ignore errors | |
docker-compose $(FLAGS) up --force-recreate --abort-on-container-exit --remove-orphans | |
env: | |
env | sort | |
echo: | |
@echo ========================================= | |
@echo = PROJECT_NAME: $(PROJECT_NAME) | |
@echo = USERNAME: $(USERNAME) | |
@echo = ENV_CONTEXT: $(ENV_CONTEXT) | |
@echo = DEPLOY_STAGE: $(DEPLOY_STAGE) | |
@echo = FLAGS: $(FLAGS) | |
@echo ========================================= | |
start: build | |
docker-compose $(FLAGS) up -d | |
down: | |
docker-compose $(FLAGS) down | |
restart: down | |
test: build | |
docker-compose $(FLAGS) run --rm $(ATTACH_HOST) bundle exec rspec | |
bash: build | |
docker-compose $(FLAGS) run --rm $(ATTACH_HOST) bash -l | |
build: .env | |
docker-compose $(FLAGS) build | |
.env: | |
echo "RAILS_EMAIL_OVERRIDE=${EMAIL}" > .env | |
logs: | |
docker-compose $(FLAGS) logs -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment