Skip to content

Instantly share code, notes, and snippets.

@ashaninBenjamin
Last active March 8, 2023 08:57
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 ashaninBenjamin/b14b73d57c7af9a5cb56f736b8931413 to your computer and use it in GitHub Desktop.
Save ashaninBenjamin/b14b73d57c7af9a5cb56f736b8931413 to your computer and use it in GitHub Desktop.
Docker + Docker-compose + Rails new
start new rails project with docker

To do:

docker-compose build - creating images

docker-compose up --no-start - creating containers

docker-compose run --rm web bash -c 'bundle && bundle exec rails new . -T -f --skip-turbolinks --skip-action-cable --skip-coffee --skip-git -d postgresql && bundle --jobs=4 && bundle exec rails g rspec:install' - run bin/rails new

Following lines have to exist in database.yml file:

host: db
username: postgres

Then run: docker-compose run --rm web bin/rails db:create db:migrate db:seed

That's all!

version: "3"
volumes:
bundle:
services:
db:
image: postgres:12
ports:
- "5432"
environment:
POSTGRES_HOST_AUTH_METHOD: 'trust'
web:
build: .
image: app_name/web
command: bash -c "rm -f tmp/pids/server.pid && bin/rails server -b 0.0.0.0 -p 8080"
volumes: &web-volumes
- ./:/myapp
- ~/.ssh:/root/.ssh
- ~/.bash_history:/root/.bash_history
- bundle:/usr/local/bundle
ports:
- "8080:8080"
links:
- db
- spring
environment: &web-environment
RAILS_ENV: development
spring:
image: app_name/web
container_name: app_name-spring
volumes: *web-volumes
environment: *web-environment
command: bash -c "rm -f tmp/spring.sock && bin/spring server"
FROM ruby:2.6.5-slim-buster
RUN apt-get update -qq && apt-get install -y curl
RUN curl -sL http://deb.nodesource.com/setup_12.x | bash -
RUN curl -sS http://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb http://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update -qq
RUN apt-get install -y build-essential postgresql-client libpq-dev nodejs
RUN apt-get -y -o Dpkg::Options::="--force-overwrite" install yarn
RUN mkdir /myapp
WORKDIR /myapp
ADD . /myapp
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
gem 'rails'
gem 'rspec-rails'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment