Ruby

10 de Fevereiro de 2024 - @Walmir Neto


Antes de tudo você precisa instalar o docker na sua maquina

No mac é só baixar um executável e dá dois clicks, já no ubuntu tem a opção de baixar um DEB package ou se você usa outra distribuição linux ou Ruindows Windows dá uma olhada nas instruções do link abaixo

Install Docker Engine

Depois de instalado é só criar o arquivo docker-compose.yml no root do seu projeto.

version: "3.9"

name: "rails-demo"

services:
  web:
    build: .
    command: bin/rails server -b 0.0.0.0
    env_file: .env
    volumes:
      - .:/rails
    links:
      - postgres
    ports:
      - "3000:3000"

  postgres:
    image: postgres:16.1-alpine
    restart: always
    env_file:
      - .env
    environment:
      - POSTGRES_USER=${DATABASE_USERNAME}
      - POSTGRES_PASSWORD=${DATABASE_PASSWORD}
    ports:
      - "5432:5432"
    volumes:
      - ./tmp/postgresql-data:/var/lib/postgresql/data

O Dockerfile também é necessário, porém nas novas versão do rails, quando você executa rails new <app-name> já é gerado um para você, caso não tenha um, segue o que foi gerado quando criei a aplicação

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.3
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \\
    BUNDLE_DEPLOYMENT="1" \\
    BUNDLE_PATH="/usr/local/bundle" \\
    BUNDLE_WITHOUT="development"

# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems
RUN apt-get update -qq && \\
    apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \\
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \\
    bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \\
    apt-get install --no-install-recommends -y curl libvips postgresql-client && \\
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \\
    chown -R rails:rails db log storage tmp
USER rails:rails

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

Além do docker-compose.yml e Dockfile nesse meu caso é preciso o .env com as seguintes variáveis:

DATABASE_PASSWORD=SuperSecretPassword
DATABASE_NAME=rails_demo_development

Pois isso está sendo usado no docker-compose.yml lá na parte do postgres:

    env_file:
      - .env
    environment:
      - POSTGRES_USER=${DATABASE_USERNAME}
      - POSTGRES_PASSWORD=${DATABASE_PASSWORD}

Agora é só rodar um docker-compose up para baixar todas as imagens e levantar o container e se deu tudo certo, é só acessar o http://localhost:3000 e ser feliz

O código desse exemplo → https://github.com/owalmirneto/rails-demo/pull/3

Referências