<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mykyta Didenko</title>
    <description>The latest articles on DEV Community by Mykyta Didenko (@mdidenko).</description>
    <link>https://dev.to/mdidenko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1015262%2F8231f43f-2ab8-4039-be32-c81ee9ba452d.jpeg</url>
      <title>DEV Community: Mykyta Didenko</title>
      <link>https://dev.to/mdidenko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdidenko"/>
    <language>en</language>
    <item>
      <title>Creating Rails API from scratch (with PostgreSQL and Docker)</title>
      <dc:creator>Mykyta Didenko</dc:creator>
      <pubDate>Thu, 26 Jan 2023 22:06:32 +0000</pubDate>
      <link>https://dev.to/mdidenko/creating-rails-api-with-postgresql-and-docker-4dp1</link>
      <guid>https://dev.to/mdidenko/creating-rails-api-with-postgresql-and-docker-4dp1</guid>
      <description>&lt;p&gt;To create a Rails API app that uses a PostgresQL database and run it using Docker Compose, you will need to complete the following steps:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Must be installed ruby 3.0.x and docker&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;1&lt;/strong&gt; Install rails gem&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem install rails
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2&lt;/strong&gt; Create a new Rails API app by running the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails new my_api --api -d postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3&lt;/strong&gt; Check your ruby version in Gemfile. The Rails version you are using may not support the latest Ruby version. Updating the Ruby version specified in the Gemfile to '~&amp;gt; 3.0' allows you to use a version of Ruby that is compatible with the version of Rails you are using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ruby '~&amp;gt; 3.0'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4&lt;/strong&gt; Create a &lt;code&gt;Dockerfile&lt;/code&gt; file in the root of your app with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ruby:3.0

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

RUN mkdir /my_api
WORKDIR /my_api

COPY Gemfile /my_api/Gemfile
COPY Gemfile.lock /my_api/Gemfile.lock

RUN bundle install

COPY . /my_api

CMD ["rails", "server", "-b", "0.0.0.0"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;FROM&lt;/code&gt; instruction sets the base image for the &lt;code&gt;Dockerfile&lt;/code&gt;, in this case &lt;code&gt;ruby:3.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;RUN&lt;/code&gt; instruction runs a command in a new layer added to the image. The first &lt;code&gt;RUN&lt;/code&gt; command updates the package lists and installs &lt;code&gt;nodejs&lt;/code&gt; and &lt;code&gt;postgresql-client&lt;/code&gt; packages.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;RUN&lt;/code&gt; command that follows creates a new directory called &lt;code&gt;/my_api&lt;/code&gt;. The &lt;code&gt;WORKDIR&lt;/code&gt; instruction sets the working directory for any &lt;code&gt;RUN&lt;/code&gt;, &lt;code&gt;CMD&lt;/code&gt;, &lt;code&gt;ENTRYPOINT&lt;/code&gt;, &lt;code&gt;COPY&lt;/code&gt; and &lt;code&gt;ADD&lt;/code&gt; instructions that follow it in the &lt;code&gt;Dockerfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;COPY&lt;/code&gt; instruction copies files from the host file system to the container file system. The first &lt;code&gt;COPY&lt;/code&gt; command copies the &lt;code&gt;Gemfile&lt;/code&gt; and &lt;code&gt;Gemfile.lock&lt;/code&gt; from the host to the &lt;code&gt;/my_api&lt;/code&gt; directory in the container.&lt;/p&gt;

&lt;p&gt;The next &lt;code&gt;RUN&lt;/code&gt; command runs &lt;code&gt;bundle install&lt;/code&gt; command to install the dependencies specified in the &lt;code&gt;Gemfile&lt;/code&gt; and &lt;code&gt;Gemfile.lock&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;The second &lt;code&gt;COPY&lt;/code&gt; command copies all the files from the current directory on the host to the &lt;code&gt;/my_api&lt;/code&gt; directory in the container.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;CMD&lt;/code&gt; instruction provides default command to execute when a container is created from the image. The command &lt;code&gt;["rails", "server", "-b", "0.0.0.0"]&lt;/code&gt; runs the rails server command with the option &lt;code&gt;-b&lt;/code&gt; set to &lt;code&gt;0.0.0.0&lt;/code&gt;, which means that the server will bind to all available network interfaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5&lt;/strong&gt; Create a &lt;code&gt;docker-compose.yml&lt;/code&gt; file in the root of your app as well. It should contain the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'
services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: my_api
      POSTGRES_PASSWORD: my_api
      POSTGRES_DB: my_api_development
    ports:
      - "5432:5432"
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid &amp;amp;&amp;amp; bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      POSTGRES_USER: my_api
      POSTGRES_PASSWORD: my_api
      POSTGRES_DB: my_api_development
    volumes:
      - .:/my_api
    ports:
      - "3000:3000"
    depends_on:
      - db

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;version&lt;/code&gt; field specifies the version of the Compose file format. In this case, it is version 3.&lt;/p&gt;

&lt;p&gt;The services field defines all the services that make up the application. It has two services defined: &lt;code&gt;db&lt;/code&gt; and &lt;code&gt;web&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;db&lt;/code&gt; service uses the &lt;code&gt;postgres&lt;/code&gt; image and sets environment variables for the PostgreSQL user, password, and database name to be used. The &lt;code&gt;ports&lt;/code&gt; field maps the host's port 5432 to the container's port 5432, allowing access to the PostgreSQL instance running in the container.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;web&lt;/code&gt; service uses the &lt;code&gt;build&lt;/code&gt; field to specify that the image should be built from the current directory (&lt;code&gt;.&lt;/code&gt;) and the &lt;code&gt;command&lt;/code&gt; field to specify command that will be executed when the container starts. This command removes the server.pid file and runs the &lt;code&gt;rails server&lt;/code&gt; command with options to listen on port 3000 and bind to all available network interfaces. It also sets environment variables for the PostgreSQL user, password, and database name to be used, which should match with the &lt;code&gt;db&lt;/code&gt; service's environment variables.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;volumes&lt;/code&gt; field mount the current directory on the host to the &lt;code&gt;/my_api&lt;/code&gt; directory in the container, allowing changes made on the host to be reflected in the container.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ports&lt;/code&gt; field maps the host's port 3000 to the container's port 3000, allowing access to the Rails application running in the container.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;depends_on&lt;/code&gt; field specifies that the &lt;code&gt;web&lt;/code&gt; service depends on the &lt;code&gt;db&lt;/code&gt; service. This means that Compose will ensure that the &lt;code&gt;db&lt;/code&gt; service is running before starting the &lt;code&gt;web&lt;/code&gt; service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6&lt;/strong&gt; Update the &lt;code&gt;config/database.yml&lt;/code&gt; file to include the necessary information to connect to your PostgresQL database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;development:
  &amp;lt;&amp;lt;: *default
  database: &amp;lt;%= ENV['POSTGRES_DB'] %&amp;gt;
  username: &amp;lt;%= ENV['POSTGRES_USER'] %&amp;gt;
  password: &amp;lt;%= ENV['POSTGRES_PASSWORD'] %&amp;gt;
  host: db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7&lt;/strong&gt; Add this simple endpoint to &lt;code&gt;config/routes.rb&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.routes.draw do
  get '/status', to: 'status#index'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;8&lt;/strong&gt; Create a controller &lt;code&gt;status_controller.rb&lt;/code&gt; in &lt;code&gt;app/controllers&lt;/code&gt; with this content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StatusController &amp;lt; ApplicationController
  def index
    render json: {status: 'OK'}
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;docker-compose build&lt;/code&gt; to build the images for your app and database&lt;br&gt;
Run &lt;code&gt;docker-compose up&lt;/code&gt; to start your app and database.&lt;/p&gt;

&lt;p&gt;Check the first API endpoint in your Rails app: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CaiXVKgL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dguesg2ejinz68x789jt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CaiXVKgL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dguesg2ejinz68x789jt.png" alt="the first API endpoint in your Rails app" width="323" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
