<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>docker on luis angel ortega</title><link>https://luisangel.me/tags/docker/</link><description>Recent Docker on luis angel ortega</description><generator>Hugo</generator><language>en-US</language><managingEditor>hey@luisangel.me (Luis Angel Ortega)</managingEditor><webMaster>hey@luisangel.me (Luis Angel Ortega)</webMaster><copyright>2025 Luis Angel Ortega Holguin All rights reserved</copyright><lastBuildDate>Thu, 02 Sep 2021 15:15:49 -0500</lastBuildDate><atom:link href="https://luisangel.me/tags/docker/index.xml" rel="self" type="application/rss+xml"/><item><title>Docker + Rails: A Solution for Your Headaches</title><link>https://luisangel.me/post/2021/09/02/docker-and-rails/</link><pubDate>Thu, 02 Sep 2021 15:15:49 -0500</pubDate><author>hey@luisangel.me (Luis Angel Ortega)</author><guid>https://luisangel.me/post/2021/09/02/docker-and-rails/</guid><description>&lt;p>Have you ever spent a week just trying to run the project you just joined? Or does your application not run in production as it did locally?
There are a multitude of factors that can contribute to this, which is why &lt;a href="https://www.docker.com/">Docker&lt;/a> offers us a solution with which we can have greater control over these variables across the necessary computers.&lt;/p>
&lt;p>With that said, in this article we will see how to make our lives easier by having our entire Ruby on Rails application running on Docker; including any necessary databases.&lt;/p>
&lt;h2 id="prerequisites">Prerequisites&lt;/h2>
&lt;p>To follow this guide, you will need to have &lt;a href="https://docs.docker.com/get-docker/">Docker installed&lt;/a> as well as a project you want to &lt;em>dockerize&lt;/em>. If you just want to practice, you can use &lt;a href="https://github.com/LinkSake/docker-rails">this example project&lt;/a> which requires a connection to &lt;a href="https://en.wikipedia.org/wiki/PostgreSQL">Postgres&lt;/a> and &lt;a href="https://en.wikipedia.org/wiki/Redis">Redis&lt;/a> to function.&lt;/p>
&lt;p>Are you impatient? You can clone &lt;a href="https://github.com/LinkSake/docker-rails/tree/docker">this branch&lt;/a> of the project where the necessary files to run the project inside Docker are already present!&lt;/p>
&lt;h2 id="first-comes-the-dockerfile">First Comes the Dockerfile&lt;/h2>
&lt;p>The first thing we will do is create a custom &lt;a href="https://docs.docker.com/get-started/overview/#docker-objects">image&lt;/a> for our project, so we will create a file at the root of it called &lt;code>Dockerfile&lt;/code>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>touch Dockerfile
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first line of our file will define the image we will base on, in this case, it will be the &lt;a href="https://hub.docker.com/_/ruby">official Ruby image&lt;/a>, but we will use the Alpine version to have a lighter image as a result.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>⚠️ Make sure the Ruby version (ruby:X.X.X-alpine) is the same as in your project, or you will have errors when trying to build the image. You can find the version used by your project in your Gemfile.&lt;/p>
&lt;/blockquote>
&lt;p>Next comes the most difficult part of this Dockerfile, installing the necessary dependencies for the project to function; those shown here are the ones that work for our &lt;a href="https://github.com/LinkSake/docker-rails">example project&lt;/a>, which include the necessary ones to make a connection with Postgres, but you will have to discover which ones are necessary for your project.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> apk add --update --no-cache --virtual run-dependencies &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>build-base &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-client &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-dev &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>yarn &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>git &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>tzdata &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>libpq &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">&amp;amp;&amp;amp;&lt;/span> rm -rf /var/cache/apk/*&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>You can wait to build the image (&lt;code>docker build .&lt;/code>) to check the error printed by Docker, with that you can find out what dependencies are missing 😉&lt;/p>
&lt;/blockquote>
&lt;p>The last line (&lt;code>rm -rf /var/cache/apk/*&lt;/code>) deletes the packages of the dependencies we just installed, this will save space in the image.&lt;/p>
&lt;p>Next, we need to create a directory inside the container where we can copy the code of our application for its execution, we will do this with the following command inside our Dockerfile.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> apk add --update --no-cache --virtual run-dependencies &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>build-base &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-client &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-dev &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>yarn &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>git &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>tzdata &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>libpq &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">&amp;amp;&amp;amp;&lt;/span> rm -rf /var/cache/apk/*&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">WORKDIR&lt;/span>&lt;span style="color:#a6d189"> /docker-rails&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>Remember to change &lt;code>docker-rails&lt;/code> to your project name!&lt;/p>
&lt;/blockquote>
&lt;p>Just as we gave your project a home inside the container we will create, the gems of the same need a folder too. Therefore, we will inform &lt;a href="https://bundler.io/">bundler&lt;/a> where to place them through an environment variable.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> apk add --update --no-cache --virtual run-dependencies &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>build-base &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-client &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-dev &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>yarn &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>git &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>tzdata &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>libpq &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">&amp;amp;&amp;amp;&lt;/span> rm -rf /var/cache/apk/*&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">WORKDIR&lt;/span>&lt;span style="color:#a6d189"> /docker-rails&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">ENV&lt;/span> BUNDLE_PATH /gems&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And although we have already installed the necessary dependencies to run Rails within the container, your project will need some gems and some JavaScript packages to function correctly, we will take care of that as follows.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> apk add --update --no-cache --virtual run-dependencies &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>build-base &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-client &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-dev &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>yarn &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>git &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>tzdata &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>libpq &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">&amp;amp;&amp;amp;&lt;/span> rm -rf /var/cache/apk/*&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">WORKDIR&lt;/span>&lt;span style="color:#a6d189"> /docker-rails&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">ENV&lt;/span> BUNDLE_PATH /gems&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> package.json yarn.lock /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> yarn install&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> Gemfile Gemfile.lock /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> bundle install&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Now that we have everything necessary for your project to function, we will copy the code to the container inside the folder we created with the &lt;code>WORKDIR&lt;/code> command.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> apk add --update --no-cache --virtual run-dependencies &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>build-base &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-client &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-dev &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>yarn &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>git &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>tzdata &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>libpq &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">&amp;amp;&amp;amp;&lt;/span> rm -rf /var/cache/apk/*&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">WORKDIR&lt;/span>&lt;span style="color:#a6d189"> /docker-rails&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">ENV&lt;/span> BUNDLE_PATH /gems&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> package.json yarn.lock /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> yarn install&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> Gemfile Gemfile.lock /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> bundle install&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> . /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>Why do we first copy the manifests (package.json, Gemfile, etc.) and then the rest of the project? This avoids having to reinstall the dependencies (since they stay in the cache) after changing the base code and rebuilding the image; this way only when the manifests change will their installation commands be run again.&lt;/p>
&lt;/blockquote>
&lt;p>Finally, we will tell Docker what command to run when we start our container (&lt;code>rails&lt;/code>), as well as the arguments for it (&lt;code>s -b 0.0.0.0&lt;/code>) and which port to expose so that we can access our application.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-Dockerfile" data-lang="Dockerfile">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">FROM&lt;/span>&lt;span style="color:#a6d189"> ruby:3.0.1-alpine&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> apk add --update --no-cache --virtual run-dependencies &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>build-base &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-client &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>postgresql-dev &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>yarn &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>git &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>tzdata &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>libpq &lt;span style="color:#8caaee">\
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8caaee">&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">&amp;amp;&amp;amp;&lt;/span> rm -rf /var/cache/apk/*&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>WORKROOM /docker-rails&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">ENV&lt;/span> BUNDLE_PATH /gems&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> package.json yarn.lock /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> yarn install&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> Gemfile Gemfile.lock /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">RUN&lt;/span> bundle install&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">COPY&lt;/span> . /docker-rails/&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">ENTRYPOINT&lt;/span> [&lt;span style="color:#a6d189">&amp;#34;bin/rails&amp;#34;&lt;/span>]&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">CMD&lt;/span> [&lt;span style="color:#a6d189">&amp;#34;s&amp;#34;&lt;/span>, &lt;span style="color:#a6d189">&amp;#34;-b&amp;#34;&lt;/span>, &lt;span style="color:#a6d189">&amp;#34;0.0.0.0&amp;#34;&lt;/span>]&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e78284">&lt;/span>&lt;span style="color:#ca9ee6">EXPOSE&lt;/span>&lt;span style="color:#a6d189"> 3000&lt;/span>&lt;span style="color:#e78284">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>The default port over which Rails runs is 3000, but if you have designated another port within your application make sure to expose it correctly.&lt;/p>
&lt;/blockquote>
&lt;p>And with this we have our Dockerfile ready, although we are far from having our application ready. If we build our image with &lt;code>docker build .&lt;/code> and try to run it with &lt;code>docker start docker-rails&lt;/code> we would encounter an error, since Rails does not find the databases it needs to start correctly; but we will soon take care of that.&lt;/p>
&lt;h2 id="then-the-docker-composeyml">Then the docker-compose.yml&lt;/h2>
&lt;p>To coordinate all the services we need for the correct functioning of our application (in this case 2 databases: Postgres and Redis) we will use &lt;a href="https://docs.docker.com/compose/">docker-compose&lt;/a>, this Docker utility will help us create multiple containers from different images, &lt;a href="https://docs.docker.com/compose/networking/">connect them&lt;/a>, give them &lt;a href="https://docs.docker.com/compose/environment-variables/">environment variables&lt;/a> and even &lt;a href="https://docs.docker.com/storage/volumes/">volumes&lt;/a>.&lt;/p>
&lt;p>We will start by creating a file called &lt;code>docker-compose.yml&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>touch docker-compose.yml
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And on its first line we will specify which &lt;a href="https://docs.docker.com/compose/compose-file/#compose-and-docker-compatibility-matrix">version&lt;/a> of the tool we want to use, in this case we will use the most recent at the time of writing this article.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">version&lt;/span>: &lt;span style="color:#a6d189">&amp;#39;3.8&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Next, we will indicate the services we want docker-compose to run, we will do this under the &lt;code>services&lt;/code> label. We will give each service a name, which will be important when we are configuring our image, so make sure to name it in a way that makes sense to you. Let&amp;rsquo;s start with the Postgres service, which we will call &lt;em>db&lt;/em>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">version&lt;/span>: &lt;span style="color:#a6d189">&amp;#39;3.8&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">services&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">db&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: postgres:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">environment&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_DB=docker-rails-dev
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_PASSWORD=password
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">5432&lt;/span>:&lt;span style="color:#ef9f76">5432&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">volumes&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#a6d189">&amp;#39;dbdata:/var/lib/postgresql/data&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>YAML files are sensitive to indentation, so make sure everything is in order and indented correctly.&lt;/p>
&lt;/blockquote>
&lt;p>The &lt;code>db&lt;/code> tag is the name we gave to the service and within which we will specify all the configuration for it.&lt;/p>
&lt;p>The first thing we encounter is &lt;code>image&lt;/code>, which as its name indicates is the name of the image we want to use for that service, in this case it is the official Postgres image in its latest version (you can specify a version by replacing &lt;code>latest&lt;/code> with another valid version).&lt;/p>
&lt;p>Then we encounter &lt;code>container_name&lt;/code>, which is also self-explanatory and which will come in handy when checking our containers with &lt;code>docker ps&lt;/code>.&lt;/p>
&lt;p>&lt;code>environment&lt;/code> refers to the environment variables, and if we refer to the &lt;a href="https://hub.docker.com/_/postgres">Docker Postgres image documentation&lt;/a> we can see that the only mandatory variable is &lt;code>POSTGRES_PASSWORD&lt;/code> but we will also define &lt;code>POSTGRES_DB&lt;/code> to give a custom name to the database that the image creates by default.&lt;/p>
&lt;blockquote>
&lt;p>⚠️ Be sure to choose a secure password for the database!&lt;/p>
&lt;/blockquote>
&lt;p>&lt;code>ports&lt;/code> are the ports that we will need to pass from inside the container to our machine, the ones indicated in the file are the ones that Postgres uses by default.&lt;/p>
&lt;p>Finally, the &lt;code>volumes&lt;/code> are the persistent information we will need so as not to run the migrations every time we turn on the container, this is because Docker deletes all data once we shut down the information, if you want to learn more about this topic I recommend &lt;a href="https://docs.docker.com/storage/volumes/">this&lt;/a> section of the documentation.&lt;/p>
&lt;p>Now, the next service is Redis but we will not delve much into it as it only has a couple of labels which we have already reviewed, for more information you can visit &lt;a href="https://hub.docker.com/_/redis">the official image&lt;/a>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">version&lt;/span>: &lt;span style="color:#a6d189">&amp;#39;3.8&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">services&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">db&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: postgres:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">environment&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_DB=docker-rails-dev
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_PASSWORD=password
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">5432&lt;/span>:&lt;span style="color:#ef9f76">5432&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">volumes&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#a6d189">&amp;#39;dbdata:/var/lib/postgresql/data&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">redis&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: redis:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-redis
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">6379&lt;/span>:&lt;span style="color:#ef9f76">6379&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Our last service we will call &lt;code>web&lt;/code> and it will be the image we have built with our &lt;code>Dockerfile&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">version&lt;/span>: &lt;span style="color:#a6d189">&amp;#39;3.8&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">services&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">db&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: postgres:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">environment&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_DB=docker-rails-dev
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_PASSWORD=password
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">5432&lt;/span>:&lt;span style="color:#ef9f76">5432&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">volumes&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#a6d189">&amp;#39;dbdata:/var/lib/postgresql/data&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">redis&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: redis:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-redis
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">6379&lt;/span>:&lt;span style="color:#ef9f76">6379&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">web&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">build&lt;/span>: .
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: docker-rails
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-web
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">3000&lt;/span>:&lt;span style="color:#ef9f76">3000&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">depends_on&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - redis
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">environment&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_HOST=db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_USER=postgres
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_PASSWORD=password
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - REDIS_URL=redis://redis:6379
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first new tag we encounter is &lt;code>build&lt;/code>, which indicates the directory where our Dockerfile is located; since our Dockerfile is at the root we will only put &lt;code>.&lt;/code>; if your Dockerfile is not at the root or has another name it is advisable to read &lt;a href="https://docs.docker.com/compose/compose-file/compose-file-v3/#build">this&lt;/a> section of the documentation to make sure Compose finds it.&lt;/p>
&lt;p>The &lt;code>image&lt;/code> tag in this case will serve to name the image that Compose will build, since &lt;code>build&lt;/code> is present it will not go to the repository to look for a pre-built image.&lt;/p>
&lt;p>Finally, the &lt;code>depends_on&lt;/code> tag will inform Compose that it should not try to start the container until the &lt;code>db&lt;/code> and &lt;code>redis&lt;/code> services are created, as well as it will connect them internally so that we can access them through a URL (as can be seen in the Redis environment variable) or by their respective credentials (as is the case with Postgres), if you want to learn how Docker handles this you can read about &lt;a href="https://docs.docker.com/compose/networking/">Docker Network&lt;/a>.&lt;/p>
&lt;p>Now that we have finished with the services, the only thing we must do is list the volumes we will use and which we &lt;a href="https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes">named&lt;/a> in the following way.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">version&lt;/span>: &lt;span style="color:#a6d189">&amp;#39;3.8&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">services&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">db&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: postgres:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">environment&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_DB=docker-rails-dev
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_PASSWORD=password
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">5432&lt;/span>:&lt;span style="color:#ef9f76">5432&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">volumes&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#a6d189">&amp;#39;dbdata:/var/lib/postgresql/data&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">redis&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: redis:latest
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-redis
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">6379&lt;/span>:&lt;span style="color:#ef9f76">6379&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">web&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">build&lt;/span>: .
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">image&lt;/span>: docker-rails
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">container_name&lt;/span>: docker-rails-web
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">ports&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#ef9f76">3000&lt;/span>:&lt;span style="color:#ef9f76">3000&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">depends_on&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - redis
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">environment&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_HOST=db
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_USER=postgres
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - POSTGRES_PASSWORD=password
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - REDIS_URL=redis://redis:6379
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">volumes&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - .:/app
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ca9ee6">volumes&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">dbdata&lt;/span>:
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And that&amp;rsquo;s it! Our &lt;code>docker-compose.yml&lt;/code> is ready, now there&amp;rsquo;s only one step left to start creating our Docker-contained application.&lt;/p>
&lt;h2 id="out-of-sight-dockerignore-out-of-mind">Out of Sight, .dockerignore Out of Mind&lt;/h2>
&lt;p>Many times we do not want certain files to be in our Docker image because they are not necessary for its construction (or are generated in it) and only end up making the project larger than it needs to be, such as the &lt;code>node_modules&lt;/code> and &lt;code>.git&lt;/code> folder.
To save this space, we will create a file called &lt;code>.dockerignore&lt;/code> at the root of our project and add these two folders:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#99d1db">echo&lt;/span> &lt;span style="color:#a6d189">&amp;#34;.git \n node_modules&amp;#34;&lt;/span> &amp;gt;&amp;gt; .dockerignore
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For more information on what a &lt;code>.dockerignore&lt;/code> file can contain, you can consult the &lt;a href="https://docs.docker.com/engine/reference/builder/#dockerignore-file">official Docker documentation&lt;/a>&lt;/p>
&lt;h2 id="databases-2-go">Databases 2 Go&lt;/h2>
&lt;p>Before running the project it will be necessary to create the database that Rails expects, and creating it is so simple that it can be done in a single command.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>docker-compose run web db:create
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This command tells Docker to use the image (which it will build) to run a command, in this case &lt;code>db:create&lt;/code>. Docker, with what is specified in the &lt;code>docker-compose.yml&lt;/code>, knows that since &lt;em>web&lt;/em> depends on &lt;em>db&lt;/em> it will have to run the Postgres instance first, so the database will be created in this container.&lt;/p>
&lt;blockquote>
&lt;p>Why only &lt;code>db:create&lt;/code> and not &lt;code>rails db:create&lt;/code> or &lt;code>rake db:create&lt;/code>? In our &lt;code>Dockerfile&lt;/code> we gave &lt;code>rails&lt;/code> as the entry point, so only the parameters are necessary. If we wanted to perform another command inside the container this would have to be through &lt;a href="https://docs.docker.com/engine/reference/commandline/exec/">docker exec&lt;/a>.&lt;/p>
&lt;/blockquote>
&lt;h2 id="our-application-in-a-container">Our Application in a Container&lt;/h2>
&lt;p>With the database created, there&amp;rsquo;s only one command left that will run the containers in &lt;a href="https://docs.docker.com/compose/reference/up/">detached mode&lt;/a> and we will be able to see the fruit of our labor.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>docker-compose up -d
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And that&amp;rsquo;s it! You can now access through your browser to &lt;a href="http://localhost:3000/">localhost:3000&lt;/a> and see the Rails welcome page.&lt;/p>
&lt;p>&lt;img src="https://luisangel.me/images/post/docker-rails-1.png" alt="Welcome to Rails!">&lt;/p>
&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;p>This whole process may be a bit intimidating at first, especially if you are not familiar with Docker, but the result is a much simpler development environment for everyone involved in the project, as now just by having Docker installed they can start programming; not to mention the benefits that this technology can bring to your production environment when combined with Kubernetes or Docker Swarm.&lt;/p>
&lt;p>I hope you found this useful, anything you can &lt;a href="https://luisangel.me/es/about">contact me&lt;/a> and I will respond as soon as possible.&lt;/p>
&lt;h2 id="references">References&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://docs.docker.com/samples/rails/">Docker Docs&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://gorails.com/episodes/docker-basics-for-gorails">Go Rails&lt;/a>&lt;/li>
&lt;/ul></description></item></channel></rss>