Docker & PostgreSQL

Ansif
3 min readFeb 20, 2024

What is Docker good for?

Docker is good for developing, packaging, and deploying applications regardless of the any operating system.

What is the difference between Docker and virtualization technologies like Virtualbox?

Docker containers do have an operating system, but they share the host system’s operating system kernel. Each Docker container contains the necessary components to run an application, such as libraries and binaries, but they don’t include a full separate operating system like virtual machines do.

What are the advantages/disadvantages of Docker containers compared to classic virtualization techniques? Which one is more lightweight?

Docker containers are lightweight and efficient, as they leverage the host system’s resources and avoid the need to duplicate an entire operating system ike VirtualBox

What is docker-compose good for?

  • Run Multi-Container Applications like app servers, databases etc
  • Easy Configuration via YAML file
  • Easy to use commands like docker-up, docker-down, docker-compose etc
  • Installer is available on Windows, Linux etc
  • Aids in creating development environments and integrates with CI/CD pipelines

What is the difference between Docker and docker-compose?

  • Docker is a tool to create and manage individual containers. It allows to package applications and their dependencies into separate containers and run it
  • Docker Compose is a tool to manage multiple containers for building an application. It uses the YAML file to define and configure all the different containers and their settings

Using PostgreSQL with Ruby Rails project inside Docker Container

To add PostgreSQL to your Ruby on Rails project and access a Docker PostgreSQL container, you can follow these steps:

Install Docker

If you haven’t already, install Docker on your development machine. You can download it from the [Docker website](https://www.docker.com/get-started).

Create a Docker PostgreSQL Container

Run the following command to create a Docker container running PostgreSQL:

docker run - name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

This command creates a PostgreSQL container named `my-postgres`, sets the password to `mysecretpassword`, and maps port 5432 from the container to your local machine.

Update Your Rails Project Configuration:

Open your Rails project’s `config/database.yml` file and configure it to use the Docker PostgreSQL container. Replace the contents with the following:

default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password: mysecretpassword
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production

Make sure to replace `myapp_development`, `myapp_test`, and `myapp_production` with your actual database names.

4. Install the PostgreSQL Gem:

In your Rails project’s `Gemfile`, make sure you have the `pg` gem included:

gem 'pg'

Save the `Gemfile` and run `bundle install` to install the gem.

5. Create the Databases:

Run the following commands to create and migrate the development and test databases:

rails db:create
rails db:migrate

This will create the PostgreSQL databases as specified in your `database.yml` file.

6. Start Your Rails Server:

Start your Rails server using the following command:

rails server

Your Rails application should now be using the Docker PostgreSQL container as its database.

7. Access PostgreSQL Container (Optional):

If you want to access the PostgreSQL container directly, you can do so using the `psql` command:

docker exec -it my-postgres psql -U postgres

This command logs you into the PostgreSQL container as the `postgres` user.

Remember to stop the PostgreSQL container when you’re done with your development work: docker stop my-postgres

This setup allows you to use a Docker PostgreSQL container as your development database for your Ruby on Rails project.

--

--