Last active
December 22, 2021 09:07
-
-
Save wildeng/e75a698b863d61e033eb50c7ec505acc to your computer and use it in GitHub Desktop.
Dockerfile and docker-compose for Rails Development
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
version: '3.7' | |
services: | |
rails: &rails | |
build: | |
context: . | |
dockerfile: ./dockerfiles/Dockerfile-dev | |
args: | |
RUBY_VERSION: '3.0.0' | |
BUNDLER_VERSION: '2.2.3' | |
uid: $uid | |
gid: $gid | |
PORT: $PORT | |
environment: &environment | |
REDIS_URL: $REDIS_URL | |
PORT: $PORT | |
ports: | |
- $PORT:$PORT | |
dev: &dev | |
<<: *rails | |
tty: true | |
stdin_open: true | |
command: bundle exec rails s -p 3000 -b '0.0.0.0' | |
tty: true | |
stdin_open: true | |
user: $uid:$gid | |
working_dir: '/var/www' | |
restart: on-failure | |
volumes: | |
- type: bind | |
source: ./ | |
target: /var/www | |
ports: | |
- $PORT:$PORT | |
environment: | |
RAILS_ENV: "development" | |
DB_DEV_HOST: "db_dev" | |
REDIS_URL: $REDIS_URL | |
POSTGRES_USER: $POSTGRES_USER | |
depends_on: | |
- db_dev | |
- redis | |
- sidekiq | |
test: &test | |
<<: *dev | |
working_dir: '/var/www' | |
user: $uid:$gid | |
environment: | |
REDIS_URL: 'redis://redis:6379/0' | |
RAILS_ENV: "test" | |
CODECOV: 'true' | |
depends_on: | |
- redis | |
redis: | |
image: redis | |
sidekiq: | |
build: | |
context: . | |
dockerfile: ./dockerfiles/Dockerfile-dev | |
depends_on: | |
- 'db_dev' | |
- 'redis' | |
command: bundle exec sidekiq | |
volumes: | |
- type: bind | |
source: ./ | |
target: /var/www | |
environment: | |
REDIS_URL_SIDEKIQ: $REDIS_URL | |
db: &db | |
image: postgres:latest | |
restart: always | |
ports: | |
- 5432:5432 | |
db_dev: | |
<<: *db | |
environment: | |
POSTGRES_USER: "postgres" | |
POSTGRES_DATABASE: "maildigest_development" | |
POSTGRES_HOST_AUTH_METHOD: trust |
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
ARG RUBY_VERSION | |
FROM ruby:$RUBY_VERSION | |
ARG BUNDLER_VERSION | |
ARG uid | |
ARG gid | |
ARG PORT | |
ARG REDIS_URL | |
# set some env variables | |
ENV REDIS_URL=${REDIS_URL} | |
ENV PORT=${PORT} | |
# The qq is for silent output in the console | |
RUN apt-get update -qq && \ | |
apt-get install -y build-essential openssl libssl-dev nodejs less vim libsasl2-dev | |
#RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - &&\ | |
# tee /etc/apt/sources.list.d/yarn.list &&\ | |
# apt-get update -qq && apt-get install -y yarn | |
# Creating a user to overcome the fact that on Linux | |
# Docker runs as a daemon with root permissions and | |
# takes ownership of your mounted folder | |
RUN groupadd -g $gid webuser && \ | |
useradd -l -ms /bin/bash -u $uid -g $gid webuser | |
# Sets the path where the app is going to be installed | |
ENV WORK_ROOT /var | |
ENV RAILS_ROOT $WORK_ROOT/www/ | |
ENV LANG C.UTF-8 | |
ENV GEM_HOME $WORK_ROOT/bundle | |
ENV BUNDLE_BIN $GEM_HOME/gems/bin | |
ENV PATH $GEM_HOME/bin:$BUNDLE_BIN:$PATH | |
RUN gem install bundler -v $BUNDLER_VERSION && \ | |
mkdir -p $RAILS_ROOT && mkdir -p /var/www/tmp | |
# This is given by the Ruby Image. | |
# This will be the de-facto directory that | |
# all the contents are going to be stored. | |
RUN chown -R $uid:$gid $GEM_HOME &&\ | |
bundle config --path=$GEM_HOME | |
WORKDIR $RAILS_ROOT | |
# We are copying the Gemfile first, so we can install | |
# all the dependencies without any issues | |
# Rails will be installed once you load it from the Gemfile | |
# This will also ensure that gems are cached and onlu updated when | |
# they change. | |
COPY Gemfile ./ | |
COPY Gemfile.lock ./ | |
RUN chown -R webuser:webuser $RAILS_ROOT | |
USER webuser | |
# Installs the Gem File. | |
RUN bundle install | |
# We copy all the files from the current directory to our | |
# /app directory | |
# Pay close attention to the dot (.) | |
# The first one will select ALL The files of the current directory, | |
# The second dot will copy it to the WORKDIR! | |
COPY . $RAILS_ROOT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment