<?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: Lorna Tumuhairwe</title>
    <description>The latest articles on DEV Community by Lorna Tumuhairwe (@lornatumuhairwe).</description>
    <link>https://dev.to/lornatumuhairwe</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%2F75075%2F631110cf-116b-406f-a970-fbde6e50b417.jpeg</url>
      <title>DEV Community: Lorna Tumuhairwe</title>
      <link>https://dev.to/lornatumuhairwe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lornatumuhairwe"/>
    <language>en</language>
    <item>
      <title>Setting up a new Rails project without having ruby/rails on your system.</title>
      <dc:creator>Lorna Tumuhairwe</dc:creator>
      <pubDate>Wed, 07 Aug 2019 21:59:17 +0000</pubDate>
      <link>https://dev.to/lornatumuhairwe/setting-up-a-new-rails-project-without-having-ruby-rails-on-your-system-3ge7</link>
      <guid>https://dev.to/lornatumuhairwe/setting-up-a-new-rails-project-without-having-ruby-rails-on-your-system-3ge7</guid>
      <description>&lt;p&gt;There are many articles on the internet about &lt;em&gt;setting up a rails app with docker&lt;/em&gt;. You might be wondering, "why write another one?". I asked myself the same question. The reason I went ahead to do it is because as I learned how to use Docker, I tried to set up a rails app in it. Most of the resources I came across were using Docker Compose. That didn't help me with practising Docker very much because I wanted to be able to write those long docker commands and follow exactly what happens in that &lt;code&gt;docker-compose.yml&lt;/code&gt; file. I write this article for me and anyone who might have the same need. In case you get stuck on the quest, you might pick up a cent or two from here. Let's dive into it!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create the directory where you will be working from and &lt;code&gt;cd&lt;/code&gt; into the &lt;br&gt;
directory. I am going to name the app &lt;code&gt;dockerapp&lt;/code&gt;.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir dockerapp &amp;amp;&amp;amp; cd $_
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Then create the initial files.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch {Gemfile,Dockerfile}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the &lt;code&gt;Gemfile&lt;/code&gt;, put in the code below.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source 'https://rubygems.org'

gem 'rails', '~&amp;gt; 5.2'
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM ruby:2.6-alpine

RUN apk update -qq &amp;amp;&amp;amp; apk add --update alpine-sdk postgresql-dev nodejs yarn tzdata

RUN mkdir /app WORKDIR /app

COPY Gemfile Gemfile

RUN bundle install

COPY . .

LABEL maintainer="Lorna Tumuhairwe &amp;lt;lornatumuhairwe@gmail.com&amp;gt;" version="1.0"

EXPOSE 3000

CMD rails s --port=3000 -b='0.0.0.0'
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Build the image. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker image build -t dockerapp .
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now your image has both ruby and rails installed. So we now use &lt;br&gt;
the installed rails to generate a new rails project.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker container run -it -v ${PWD}:/app dockerapp rails new . --database=postgresql -f -T
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;At this point you should have the rails app in the folder since you &lt;br&gt;
mounted the volume with &lt;code&gt;-v ${PWD}:/app&lt;/code&gt;. Update the &lt;br&gt;
&lt;code&gt;config/database.yml&lt;/code&gt; file to contain the code below. I have made &lt;br&gt;
changes to the &lt;code&gt;hostname&lt;/code&gt;, &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;database&lt;/code&gt; names to match &lt;br&gt;
the postgres name we are going to use when we run the postgresql &lt;br&gt;
container and the application name respectively. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default: &amp;amp;default
  adapter: postgresql
  encoding: unicode
  host: postgres
  username: postgres
  pool: &amp;lt;%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %&amp;gt;

development:
  &amp;lt;&amp;lt;: *default
  database: dockerapp_development

test:
  &amp;lt;&amp;lt;: *default
  database: dockerapp_test

production:
  &amp;lt;&amp;lt;: *default
  database: dockerapp_production
  username: app
  password: &amp;lt;%= ENV['APP_DATABASE_PASSWORD'] %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Rebuild your docker image since the Gemfile has changed. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker image build -t dockerapp .
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a network that you'll use to connect the app container and the &lt;br&gt;
postgres container. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker network create dockerapp-network
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run a postgresql container with the command below. Even if you don't &lt;br&gt;
have it pulled already, it will be downloaded and used. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker container run -it --rm -v pgdata:/var/lib/postgresql/data --name postgres --net dockerapp-network -p 5432:5432 postgres:9.6-alpine
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the app as well in the same network. &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker container run --rm -it -v ${PWD}:/app -p 3000:3000 --name dockerapp --net dockerapp-network dockerapp
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open another terminal and create the database.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker exec dockerapp rails db:create
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In your browser visit &lt;code&gt;localhost:3000&lt;/code&gt;, you should be on the rails &lt;br&gt;
welcome page!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You might have already noticed, you always have to run two long commands to run your application (step 10 and 11). But to do all that in one command, you can use Docker compose. &lt;/p&gt;

&lt;h4&gt;
  
  
  Docker compose to the rescue.
&lt;/h4&gt;

&lt;p&gt;Create a docker-compose.yml file. &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'
services:
  postgres:
    image: postgres:9.6-alpine
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  dockerapp:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - postgres
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To follow from the above, make sure your containers are all stopped. By running,&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker container stop dockerapp postgres
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the same directory, run your app with &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker-compose up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will run both the postgresql and application image at the same time. In case you run into any problems with the dockerapp container exiting, please ensure you don't have a server.pid file in &lt;code&gt;temp/pids/server.pid&lt;/code&gt;. That's a rails specific issue that can be resolved by deleting the file. Or you can write a script to always delete it if it exists when the app is started. That's well documented in the &lt;a href="https://docs.docker.com/compose/rails/"&gt;docker documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading! &lt;br&gt;
&lt;em&gt;The steps outlined in this article are only tested on a MacOS.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On a light note, this is my first post on the platform, I have learned a lot being here, I glad to share what I have learned as well. I hope it helps someone. ❤️&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
