Docker Development Environment

https://www.docker.com/get-started

Containers are great for making a fast and workable development environment. It can help isolate various versions of dependencies and make you more productive from day one. This template will create the necessary components so you can get going quickly.
development docker 1,348 3


Dave Kimura
Dave Kimura

May 18, 2020

Copy and paste this code into your terminal

Modify the docker-compose.yml to include additional services as needed. The Ruby on Rails application will be mounted into the docker container, so any changes you make locally will be reflected within the docker container.

DISCLAIMER: You should always review templates before running them. By running the template, you are agreeing to the terms of use.

The contents of this script as show. Any updates will be reflected in the below code and the snippet.

def print_green(heredoc)
  puts set_color heredoc, :green
end

def ask_with_default(prompt, default)
  value = ask("#{prompt} [#{default}]")
  value.blank? ? default : value
end

create_file './.dockerignore', <<~EOF
.git*
db/*.sqlite3
db/*.sqlite3-journal
log/*
tmp/*
tmp/pid/server.pid
Dockerfile
README.rdoc
EOF

create_file './docker-compose.yml' do
  ruby_verison = ask_with_default("What version of Ruby do you want to use?", '2.6.6')
  node_version = ask_with_default("What version of Node do you want to use?", '13')
  bundle_version = ask_with_default("What version of Bundle do you want to use?", '2.1.4')
  postgres_version = ask_with_default("What version of Postgres do you want to use?", '12')

<<~EOF
version: '2.4'
services:
  base: &app
    build:
      context: .
      dockerfile: ./Dockerfile
      args:
        RUBY_VERSION: '#{ruby_verison}'
        NODE_MAJOR: '#{node_version}'
        BUNDLE_VERSION: '#{bundle_version}'
        PG_MAJOR: '#{postgres_version}'

    tmpfs:
      - /tmp

  backend: &backend
    <<: *app
    stdin_open: true
    tty: true
    volumes:
      - .:/app:cached
      - rails_cache:/app/tmp/cache
      - bundle:/usr/local/bundle
      - node_modules:/app/node_modules
      - packs:/app/public/packs
    environment:
      - PSQL_HISTFILE=/app/log/.psql_history
      - DATABASE_URL=postgres://postgres:postgres@postgres:5432
      - BOOTSNAP_CACHE_DIR=/bundle/bootsnap
      - WEBPACKER_DEV_SERVER_HOST=webpacker
    depends_on:
      - postgres
      - redis

  app:
    <<: *backend
    command: bundle exec rails server -b 0.0.0.0
    ports:
      - '3000:3000'

  webpacker:
    <<: *app
    command: ./bin/webpack-dev-server
    ports:
      - '3035:3035'
    volumes:
      - .:/app:cached
      - bundle:/usr/local/bundle
      - node_modules:/app/node_modules
      - packs:/app/public/packs
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - RAILS_ENV=${RAILS_ENV:-development}
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0

  sidekiq:
    <<: *backend
    command: bundle exec sidekiq -C config/sidekiq.yml

  redis:
    image: redis:latest

  postgres:
    image: postgres:#{postgres_version}
    volumes:
      - .psqlrc:/root/.psqlrc:ro
      - postgres:/var/lib/postgresql/data
      - ./log:/root/log:cached
    environment:
      - PSQL_HISTFILE=/root/log/.psql_history
    ports:
      - 5432
    healthcheck:
      test: pg_isready -U postgres -h 127.0.0.1
      interval: 5s

volumes:
  postgres:
  redis:
  bundle:
  node_modules:
  rails_cache:
  packs:
EOF
end

create_file './Dockerfile', <<~EOF
ARG RUBY_VERSION
FROM ruby:$RUBY_VERSION
LABEL [email protected]

ARG NODE_MAJOR
ARG PG_MAJOR
ARG BUNDLE_VERSION
RUN apt-get update
RUN apt-get install -y build-essential

# Add PostgreSQL to sources 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' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list

RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN apt-get -y update
RUN apt-get -y install nodejs
RUN apt-get -y install yarn
RUN yarn config set "strict-ssl" false

RUN bundle config --global frozen 1

COPY Gemfile Gemfile.lock ./
RUN gem install bundler -v $BUNDLE_VERSION
RUN bundle install -j $(nproc)

RUN mkdir -p /app
WORKDIR /app
EOF

print_green <<~EOF
Your application is now configured with Docker. To start running docker,
you will need Docker and Docker Compose (https://www.docker.com/get-started)

The docker-compose.yml file has been created to house the various services
that will be used. Feel free to comment out the services not needed by your
application.

- app <- the containers for the rails application
- webpacker
- sidekiq
- redis
- postgres

To get started, type "docker-compose up"
When finished, type "docker-compose down"
If you need to run migrations, you can run "docker-compose run app rails db:migrate"
You can also get into the rails console with "docker-compose run app rails console"
EOF

A place where you can thank the author, post problems, give constructive feedback, etc. Be nice!

  • moktardev
    thanks 
    by moktardev at 6:59PM, 21 May 2020