DEV Community

radin reth
radin reth

Posted on

Update postgres to sqlite3 for rails 6 using Docker Compose

Today I am gonna show how to switch database from postgres to sqlite3 in rails application.

  1. Update Gemfile
gem 'sqlite3', '~> 1.3', '>= 1.3.3'
Enter fullscreen mode Exit fullscreen mode
  1. Open Dockerfile and install sqlite3 package
FROM ruby:3.0.0

LABEL maintainer="Radin <radin@instedd.org>"

RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
  echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
  curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  apt-get update -qq && \
  apt-get install -y vim nodejs sqlite3 libsqlite3-dev yarn && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /app
WORKDIR /app

COPY Gemfile* package.json yarn.lock /app/

RUN gem install bundler -v 2.1.4 && \
  bundle install --jobs 20

COPY . /app
Enter fullscreen mode Exit fullscreen mode
  1. Update docker-compose.yml
version: '3'

volumes:
  bundle:
  db:

services:
  db:
    image: nouchka/sqlite3:latest
    volumes:
      - db:/root/db
    stdin_open: true
    tty: true

  web: &rails
    build:
      context: .
      dockerfile: Dockerfile
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
    volumes:
      - .:/app
      - bundle:/usr/local/bundle
      - ./node_modules:/app/node_modules
    tmpfs: /app/tmp
    depends_on:
      - db
    env_file: app.env
    tty: true
    stdin_open: true
    ports:
      - "3000:3000"
Enter fullscreen mode Exit fullscreen mode
  1. And finally, Update config/database.yml
default: &default
  adapter: sqlite3
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: db/mydb_development.sqlite3

test:
  <<: *default
  database: db/mydb_test.sqlite3
Enter fullscreen mode Exit fullscreen mode

Cheer!

Top comments (0)