Beginners Guide to Docker for Ruby on Rails, PostgreSQL Development

Written by ngodi-albert | Published 2023/07/18
Tech Story Tags: docker | ruby-on-rails | postgresql | software-development | software-engineering | software-architecture | docker-compose | coding

TLDRIn this beginner's guide, we will walk you through the process of dockerizing a Ruby on Rails application with PostgreSQL. By containerizing your Rails app and its associated database, you can achieve consistency, portability, and ease of deployment. This step-by-step tutorial will provide code samples and instructions to help you get started with Docker and PostgreSQL in your Rails project.via the TL;DR App

Docker has revolutionized the way we build, deploy, and manage applications. In this beginner's guide, we will walk you through the process of dockerizing a Ruby on Rails application with PostgreSQL. By containerizing your Rails app and its associated database, you can achieve consistency, portability, and ease of deployment. This step-by-step tutorial will provide code samples and instructions to help you get started with Docker and PostgreSQL in your Rails project.
Prerequisites:
Before we begin, ensure you have Docker installed on your machine. Additionally, you should have a basic understanding of Ruby on Rails and PostgreSQL.
Step 1: Setting Up Docker Compose
Docker Compose allows us to define and manage multi-container Docker applications. Create a new file named docker-compose.yml in the root directory of your Rails application. This file will contain the configuration for running both the Rails app and PostgreSQL. Add the following content to docker-compose.yml:
version: '3' 
 services: 
  db: 
   image: postgres:12 
   volumes: 
    - ./tmp/db:/var/lib/postgresql/data 
   Environment:
    - POSTGRES_USER=your_postgres_user 
    - POSTGRES_PASSWORD=your_postgres_password 
  web: 
   build: 
    context:    dockerfile: Dockerfile 
   volumes: 
    - .:/app 
   ports: 
    - '3000:3000' 
   depends_on: 
    - db 
   Environment:
      -DATABASE_URL=postgresql://your_postgres_user:[email protected]:5432/your_app_name
Replace your_postgres_user, your_postgres_password, and your_app_name with your desired values.
Step 2: Update the Database Configuration
To ensure the Rails application connects to the PostgreSQL container, modify the config/database.yml file in your Rails project. Replace the default section with the following:
default: &default
 adapter: postgresql 
 encoding: unicode 
 host: db 
 port: 5432 
 pool: 5 
 username: your_postgres_user 
 password: your_postgres_password
Again, replace your_postgres_user and your_postgres_password with the corresponding values from the docker-compose.yml file.
Step 4: Set Up the Database
To create the PostgreSQL database and run migrations, run the following command:
docker-compose build
This command will build the Docker containers based on the configuration defined in the docker-compose.yml file. It may take a few minutes to complete.
Step 4: Set Up the Database
To create the PostgreSQL database and run migrations, run the following command:
docker-compose run web rake db:setup
This command sets up the database and creates the necessary tables based on your Rails application's migration files.
Step 5: Start the Containers
Start the Docker containers by running the following command:
docker-compose up
This command starts the Rails application and PostgreSQL database containers, making your Rails app accessible at http://localhost:3000.
Conclusion:
Congratulations! You have successfully dockerized your Ruby on Rails application with PostgreSQL. By leveraging Docker and Docker Compose, you can ensure consistency and portability across different environments. The use of PostgreSQL as a containerized database allows for easy management and simplified deployment. You can now enjoy the benefits of containerization, such as scalability, reproducibility, and isolated environments. Docker has transformed the way developers work, and with this beginner's guide, you are well on your way to leveraging its power in your Rails projects. Happy coding!

Published by HackerNoon on 2023/07/18