Setting Up a Rails Application with PostgreSQL and Docker

Setting Up a Rails Application with PostgreSQL and Docker

We'll take the following actions to create a Rails application with PostgreSQL and use Docker to run it:

  1. Make a brand-new Rails program.
  2. Create a PostgreSQL database.
  3. Put the gem dockerfile-rails in place.
  4. Create files for Docker and Docker Compose.
  5. Configure the file *.env.
  6. Complete the Docker container build and run.


Let's examine each stage in more depth.

Step 1: Create a New Rails Application

First, create a new Rails application with PostgreSQL as the database:

rails new myapp --database=postgresql
cd myapp        

Step 2: Add PostgreSQL as the Database

Ensure your Gemfile includes the PostgreSQL gem:

# Gemfile
gem 'pg', '>= 0.18', '< 2.0'        

Run bundle install to install the gem:

bundle install        

Step 3: Install the dockerfile-rails Gem

Add the dockerfile-rails gem to your Gemfile in the development group:

# Gemfile

group :development do

  gem 'dockerfile-rails', '~> 1.6', '>= 1.6.17'

end        

Run bundle install to install the gem:

bundle install        

Step 4: Generate Docker and Docker Compose Files

Generate the Docker and Docker Compose files using the dockerfile-rails generator:

bin/rails generate dockerfile --compose --postgresql        

This will create the necessary Docker and Docker Compose files.

Step 5: Set Up the .env File

Create a .env file in the root of your project to store environment variables:

touch .env        

Add the following content to the .env file:

POSTGRES_USER=postgres

POSTGRES_PASSWORD=yourpassword

POSTGRES_DB=myapp_development

DATABASE_URL=postgres://postgres:yourpassword@db:5432/myapp_development

SECRET_KEY_BASE=$(rails secret)        

Make sure to replace yourpassword with a secure password.

Step 6: Build and Run the Docker Containers

Build and run the Docker containers using Docker Compose:

docker-compose build

docker-compose up        

This will start your Rails application along with a PostgreSQL database in Docker containers.

Example Docker Compose File

Here is an example of what your docker-compose.yml file might look like:

version: '3.8'

services:

  db:

    image: postgres:13

    volumes:

      - postgres_data:/var/lib/postgresql/data

    environment:

      POSTGRES_USER: ${POSTGRES_USER}

      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

      POSTGRES_DB: ${POSTGRES_DB}

  web:

    build: .

    command: bash -c "rm -f tmp/pids/server.pid && bin/rails server -b 0.0.0.0"

    volumes:

      - .:/myapp

    ports:

      - "3000:3000"

    depends_on:

      - db

    environment:

      DATABASE_URL: ${DATABASE_URL}

      SECRET_KEY_BASE: ${SECRET_KEY_BASE}

volumes:

  postgres_data:        

Example Dockerfile

Here is an example of what your Dockerfile might look like:

# Dockerfile

FROM ruby:3.3.4

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

WORKDIR /myapp

COPY Gemfile /myapp/Gemfile

COPY Gemfile.lock /myapp/Gemfile.lock

RUN bundle install

COPY . /myapp

EXPOSE 3000

CMD ["bash", "-c", "rm -f tmp/pids/server.pid && bin/rails server -b 0.0.0.0"]        

Final Steps

1. Migrate the database:

  docker-compose run web rake db:create db:migrate        

2. Access your application:

Open your browser and navigate to https://localhost:3000




Resources

1. Ruby on Rails Guides:

Getting Started with Rails

Rails Command Line

2. Docker Documentation:

Docker Overview

Docker Compose


Author

Edward Odhiambo

(Portfolio | GitHub)


要查看或添加评论,请登录

Edward Odhiambo的更多文章

社区洞察

其他会员也浏览了