<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.3">Jekyll</generator><link href="http://blog.jedrychowski.org/feed.xml" rel="self" type="application/atom+xml" /><link href="http://blog.jedrychowski.org/" rel="alternate" type="text/html" /><updated>2019-03-17T16:56:46+00:00</updated><id>http://blog.jedrychowski.org/</id><title type="html">Rails and WebOps notes</title><subtitle>Personal blog about web development using Rails and Kubernetes</subtitle><entry><title type="html">Caching Docker Rails images - supplement</title><link href="http://blog.jedrychowski.org/2018/caching-docker-rails-supplement/" rel="alternate" type="text/html" title="Caching Docker Rails images - supplement" /><published>2018-10-30T22:20:00+00:00</published><updated>2018-10-30T22:20:00+00:00</updated><id>http://blog.jedrychowski.org/2018/caching-docker-rails-supplement</id><content type="html" xml:base="http://blog.jedrychowski.org/2018/caching-docker-rails-supplement/">&lt;p&gt;In &lt;a href=&quot;/2018/caching-docker-rails-images/&quot;&gt;my previous post, Caching Docker Rails images&lt;/a&gt;. Please read it first. This post is a continuation describing new exciting Docker (&lt;a href=&quot;https://www.linux.com/news/what-dockers-moby-project&quot;&gt;aka Moby&lt;/a&gt;) addition: volume mounts during build, added in &lt;a href=&quot;https://github.com/moby/buildkit/pull/442&quot;&gt;buildkit#442&lt;/a&gt;. It’s part of Buildkit project, future replacement for current Docker build engine. You can learn more about Buildkit by reading &lt;a href=&quot;https://blog.mobyproject.org/introducing-buildkit-17e056cc5317&quot;&gt;Tõnis Tiigi’s post, Introducing BuildKit&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;proper-syncing&quot;&gt;Proper syncing&lt;/h2&gt;

&lt;p&gt;To update source code in the repository my initial approach from previous post uses &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt; command.
&lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt; command has one fundamental problem for this use case.
&lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt; works similiarly to Unix &lt;code class=&quot;highlighter-rouge&quot;&gt;cp&lt;/code&gt; command - it only adds new files.
It is not capable of doing full sync and &lt;em&gt;removing&lt;/em&gt; files from image when they are removed in the source.&lt;/p&gt;

&lt;p&gt;Thanks to the latest update to Buildkit in &lt;a href=&quot;https://github.com/moby/buildkit/pull/442&quot;&gt;buildkit#442&lt;/a&gt; it is now possible to mount a volume during mount, including local filesystem. Combined with &lt;code class=&quot;highlighter-rouge&quot;&gt;rsync&lt;/code&gt; real syncing is finally available.&lt;/p&gt;

&lt;p&gt;This feature is still in the proposal stage and there’s no stable version available. In order to build images you need to use custom &lt;code class=&quot;highlighter-rouge&quot;&gt;dockerd&lt;/code&gt; version. The good news is that images built this way are fully compatible with current stable Docker. This means that you only need the custom setup on a machine that creates images. However as it potentially is unstable use it carefully.&lt;/p&gt;

&lt;h2 id=&quot;preparing-docker-for-building-images&quot;&gt;Preparing Docker for building images&lt;/h2&gt;

&lt;p&gt;In order to build Docker image using new feature you have to install Docker version that includes the latest change from &lt;a href=&quot;https://github.com/moby/buildkit/pull/655&quot;&gt;buildkit#655&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The easiest way to do that is to build Docker yourself from source code. I use &lt;code class=&quot;highlighter-rouge&quot;&gt;master&lt;/code&gt; branch version to include the latest patches. Building process itself is very easy as Moby actually uses Docker to bootstrap itself and make the process even easier.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Clone &lt;code class=&quot;highlighter-rouge&quot;&gt;moby&lt;/code&gt; repository. What is Moby? &lt;a href=&quot;https://www.linux.com/news/what-dockers-moby-project&quot;&gt;The Moby Project is to Docker what Fedora is to Red Hat Enterprise Linux. - Solomon Hykes, Docker CTO/Founder&lt;/a&gt;
    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone git@github.com:moby/moby.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Run make to compile dockerd and all related processes. Make sure that you have running Docker. Moby actually uses Docker to build itself, so you don’t have to worry about any dependencies.
    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;make
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the process finishes new binaries are available at &lt;code class=&quot;highlighter-rouge&quot;&gt;bundles/binary-daemon&lt;/code&gt;. These are portable go binray files, so you can install them by copying or adding to &lt;code class=&quot;highlighter-rouge&quot;&gt;$PATH&lt;/code&gt;. It’s important to copy all the binaries, not only &lt;code class=&quot;highlighter-rouge&quot;&gt;dockerd&lt;/code&gt; as they are direct dependencies.&lt;/p&gt;

&lt;p&gt;The final step is to start &lt;code class=&quot;highlighter-rouge&quot;&gt;dockerd&lt;/code&gt; in experimental mode. In order to do that add &lt;code class=&quot;highlighter-rouge&quot;&gt;--experimental&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;To save your time I created a Vagrantfile with all necessary steps included.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Vagrantfile&quot;&gt;Vagrant.configure(&quot;2&quot;) do |config|
  config.vm.box = &quot;ubuntu/xenial64&quot;

  config.vm.provider &quot;virtualbox&quot; do |v|
    v.memory = 2048 # additional memory is required
  end

  config.vm.provision &quot;shell&quot;, privileged: false, inline: &amp;lt;&amp;lt;-SHELL
    sudo apt-get update
    sudo apt-get install -y \
      apt-transport-https \
      ca-certificates \
      curl \
      software-properties-common \
      make
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository \
      &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) \
      stable&quot;
    sudo apt-get update
    sudo apt-get -y install docker-ce
    
    sudo gpasswd -a vagrant docker
     
    git clone https://github.com/moby/moby.git
    
    cd moby
    sg docker -c &quot;make&quot;
    sudo service docker stop
    sudo bundles/binary-daemon/* /usr/bin
    sudo mkdir -p /etc/systemd/system/docker.service.d/
    echo -e &quot;[Service]\nExecStart=\nExecStart=/usr/bin/dockerd -H fd:// --experimental&quot; | sudo tee /etc/systemd/system/docker.service.d/override.conf &amp;gt; /dev/null
    sudo systemctl daemon-reload
    sudo service docker start
  SHELL
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;building-images-using-run-mount&quot;&gt;Building images using RUN –mount&lt;/h2&gt;

&lt;p&gt;Buildkit is not yet default Docker’s builder. To use it you need set &lt;code class=&quot;highlighter-rouge&quot;&gt;DOCKER_BUILDKIT&lt;/code&gt; flag when running &lt;code class=&quot;highlighter-rouge&quot;&gt;docker build&lt;/code&gt;.
Additionally as new &lt;code class=&quot;highlighter-rouge&quot;&gt;RUN --mount&lt;/code&gt; syntax is still in experimental phase &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; has to be prepended with a directive:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# syntax = tonistiigi/dockerfile:runmount20180618
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To check that it works correctly create a &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; with content:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-Dockerfile&quot;&gt;# syntax = tonistiigi/dockerfile:runmount20180618
FROM alpine

RUN --mount=type=bind,source=.,target=/mounted-source cp /mounted-source/Dockerfile /copied-Dockerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Build it:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;DOCKER_BUILDKIT=1 docker build .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You will notice slightly different output of buildkit compared to standard Docker build engine.&lt;/p&gt;

&lt;h2 id=&quot;cached-dockerfile-update&quot;&gt;Cached Dockerfile update&lt;/h2&gt;

&lt;p&gt;Finally, with everything set up, we are able to tune &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile.production.simplified.cached&lt;/code&gt; from &lt;a href=&quot;https://blog.jedrychowski.org/2018/caching-docker-rails-images/&quot;&gt;the previous post&lt;/a&gt;.
Old &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; can be replaced with:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Dockerfile&quot;&gt;RUN apk add --no-cache --update rsync
RUN --mount=type=bind,source=.,target=/mounted-app rsync -ra /mounted-app/ /app
&lt;/code&gt;&lt;/pre&gt;</content><author><name></name></author><summary type="html">In my previous post, Caching Docker Rails images. Please read it first. This post is a continuation describing new exciting Docker (aka Moby) addition: volume mounts during build, added in buildkit#442. It’s part of Buildkit project, future replacement for current Docker build engine. You can learn more about Buildkit by reading Tõnis Tiigi’s post, Introducing BuildKit.</summary></entry><entry><title type="html">Caching Docker Rails images</title><link href="http://blog.jedrychowski.org/2018/caching-docker-rails-images/" rel="alternate" type="text/html" title="Caching Docker Rails images" /><published>2018-08-26T00:25:00+00:00</published><updated>2018-08-26T00:25:00+00:00</updated><id>http://blog.jedrychowski.org/2018/caching-docker-rails-images</id><content type="html" xml:base="http://blog.jedrychowski.org/2018/caching-docker-rails-images/">&lt;p&gt;This post shows a simple way of improving Docker images built process.
Using this technique reduces both required storage and build time.&lt;/p&gt;

&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;

&lt;p&gt;Docker images are built using layers, each command in Dockerfile creates a layer. Read more about images, layers and storage drivers in &lt;a href=&quot;https://docs.docker.com/storage/storagedriver/&quot;&gt;Docker docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Usually all Rails Dockerfiles have 3 common parts as layers:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;installing gems in &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle install&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;RUN bundle install&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;copying all files&lt;/li&gt;
  &lt;li&gt;precompiling assets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exemplary Dockerfile (&lt;a href=&quot;https://github.com/gogiel/rails-example-app/blob/master/Dockerfile.production.simplified&quot;&gt;Github&lt;/a&gt;):&lt;/p&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; ruby:2.5.1-alpine&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; /app&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; Gemfile Gemfile.lock ./&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle &lt;span class=&quot;nt&quot;&gt;--deployment&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--without&lt;/span&gt; development &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; . .&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; RAILS_ENV production&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rake assets:precompile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this setup when there’s a change then &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt; and all higher layers, including &lt;code class=&quot;highlighter-rouge&quot;&gt;rake assets:precompile&lt;/code&gt; have to be rebuild from scratch.
It is even worse when a &lt;code class=&quot;highlighter-rouge&quot;&gt;Gemfile&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;Gemfile.lock&lt;/code&gt; is changed, as &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle install&lt;/code&gt; starts from scratch with no cache.&lt;/p&gt;

&lt;p&gt;This is unacceptable that adding a blank line to the code produces big artifact and restarts time consuming tasks from scratch.&lt;/p&gt;

&lt;p&gt;What we want to achieve is to have a way to add incremental changes as small diffs - patches, not replacements.&lt;/p&gt;

&lt;h2 id=&quot;master-image&quot;&gt;Master image&lt;/h2&gt;

&lt;p&gt;The simplest solution is to create a &lt;em&gt;master&lt;/em&gt;/&lt;em&gt;bootstrap&lt;/em&gt; image, that incremental images are be built on.&lt;/p&gt;

&lt;p&gt;Firstly create and tag the master build. In &lt;a href=&quot;https://github.com/gogiel/rails-example-app/&quot;&gt;my repository&lt;/a&gt; I use Dockerfile &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile.production.simplified&lt;/code&gt; and tag it as &lt;code class=&quot;highlighter-rouge&quot;&gt;rails-example-app-master&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker build -f Dockerfile.production.simplified . -t rails-example-app-master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then create a copy of your Dockerfile and change the first line with &lt;code class=&quot;highlighter-rouge&quot;&gt;FROM&lt;/code&gt; command to:&lt;/p&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; rails-example-app-master&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I saved it as &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile.production.simplified.cached&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; rails-example-app-master&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; /app&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; Gemfile Gemfile.lock ./&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle &lt;span class=&quot;nt&quot;&gt;--deployment&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--without&lt;/span&gt; development &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; . .&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; RAILS_ENV production&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rake assets:precompile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s try it by buildinga a new image with newly added assets.
I use 1.4MB picture from NASA:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ wget https://apod.nasa.gov/apod/image/1808/heic1404b1920.jpg -P app/assets/images
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my case I build the image using:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker build -f Dockerfile.production.simplified.cached . -t rails-example-app-cached-image
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;what-happens&quot;&gt;What happens?&lt;/h2&gt;

&lt;p&gt;All commands from Dockerfile are executed again. However this time we already have a lot of data loaded in the image.
Let’s take a look in details at some of them:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;RUN bundle --deployment --without development test&lt;/code&gt; - all gems are already installed in the parent image. Because there’s nothing to do it &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle&lt;/code&gt; quickly exits with no changes.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; - internally Docker is able to store a difference of changes when calling &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt;. All files, except for the new asset, are already in the image. The only the diffrence (new image file) is stored in this layer.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;RUN bundle exec rake assets:precompile&lt;/code&gt; - old assets are already precompiled so there’s no need to do that again. The image that was added is precompiled and the result is stored.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can inspect layers using &lt;code class=&quot;highlighter-rouge&quot;&gt;docker history&lt;/code&gt; command.
In my case it looks like this:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker history rails-example-app-cached-image
IMAGE      CREATED             CREATED   SIZE
6450cf8b0  18 seconds ago      /bin/sh -c #(nop)  ENV RAILS_LOG_TO_STDO   0B
d0f791c49  20 seconds ago      /bin/sh -c #(nop)  ENV RAILS_SERVE_STATI   0B
a4a7779e4  22 seconds ago      /bin/sh -c bundle exec rake assets:preco   1.74MB
2b77d3b1a  28 seconds ago      /bin/sh -c #(nop)  ENV RAILS_ENV=product   0B
6d9f380d3  30 seconds ago      /bin/sh -c #(nop) COPY dir:370f291eb553b   1.51MB
6892c696a  32 seconds ago      /bin/sh -c bundle --deployment --without   90B
24d2f3c52  35 seconds ago      /bin/sh -c #(nop) COPY multi:5f12d01bf90   0B
e5a2749c0  37 seconds ago      /bin/sh -c #(nop) WORKDIR /app             0B
3bf1c6640  39 seconds ago      /bin/sh -c apk add --no-cache --update b   853kB
869f40cb4  4 minutes ago       /bin/sh -c #(nop)  ENV RAILS_LOG_TO_STDO   0B
947876927  4 minutes ago       /bin/sh -c #(nop)  ENV RAILS_SERVE_STATI   0B
3c9a21fc8  4 minutes ago       /bin/sh -c bundle exec rake assets:preco   26.3MB
e4248596b  5 minutes ago       /bin/sh -c #(nop)  ENV RAILS_ENV=product   0B
32639ec16  5 minutes ago       /bin/sh -c #(nop) COPY dir:bcef82e4178ba   52.8MB
f4a48e1f7  About an hour ago   /bin/sh -c bundle --deployment --without   121MB
993849839  About an hour ago   /bin/sh -c #(nop) COPY multi:5f12d01bf90   6.93kB
1b0cd9df5  About an hour ago   /bin/sh -c #(nop) WORKDIR /app             0B
7e368ce8c  About an hour ago   /bin/sh -c apk add --no-cache --update b   202MB
d82225343  3 days ago          /bin/sh -c #(nop)  CMD [&quot;irb&quot;]             0B
&amp;lt;missing&amp;gt;  3 days ago          /bin/sh -c mkdir -p &quot;$GEM_HOME&quot; &amp;amp;&amp;amp; chmod   0B
&amp;lt;missing&amp;gt;  3 days ago          /bin/sh -c #(nop)  ENV PATH=/usr/local/b   0B
&amp;lt;missing&amp;gt;  3 days ago          /bin/sh -c #(nop)  ENV BUNDLE_PATH=/usr/   0B
&amp;lt;missing&amp;gt;  3 days ago          /bin/sh -c #(nop)  ENV GEM_HOME=/usr/loc   0B
&amp;lt;missing&amp;gt;  3 days ago          /bin/sh -c set -ex   &amp;amp;&amp;amp; apk add --no-cac   57.8MB
&amp;lt;missing&amp;gt;  3 days ago          /bin/sh -c #(nop)  ENV BUNDLER_VERSION=1   0B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c #(nop)  ENV RUBYGEMS_VERSION=   0B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c #(nop)  ENV RUBY_DOWNLOAD_SHA   0B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c #(nop)  ENV RUBY_VERSION=2.5.   0B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c #(nop)  ENV RUBY_MAJOR=2.5      0B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c mkdir -p /usr/local/etc  &amp;amp;&amp;amp; {   45B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c #(nop)  CMD [&quot;/bin/sh&quot;]         0B
&amp;lt;missing&amp;gt;  7 weeks ago         /bin/sh -c #(nop) ADD file:6ee19b92d5cb1   4.2MB
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Layer &lt;code class=&quot;highlighter-rouge&quot;&gt;869f40cb4&lt;/code&gt; and all the lower layers 
(including &amp;lt;missing&amp;gt; layers from 
&lt;code class=&quot;highlighter-rouge&quot;&gt;ruby:2.5.1-alpine&lt;/code&gt; image) come from the master build.&lt;/p&gt;

&lt;p&gt;Layers &lt;code class=&quot;highlighter-rouge&quot;&gt;3bf1c664&lt;/code&gt; and higher were created during child image build.
To verify that only necessarry diff was stored in the layers take a look at the last column of the listing, which represents layer size:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;6450cf8b 18 seconds ago /bin/sh -c #(nop)  ENV RAILS_LOG_TO_STDOUT=t…   0B
d0f791c4 20 seconds ago /bin/sh -c #(nop)  ENV RAILS_SERVE_STATIC_FI…   0B
a4a7779e 22 seconds ago /bin/sh -c bundle exec rake assets:precompile   1.74MB
2b77d3b1 28 seconds ago /bin/sh -c #(nop)  ENV RAILS_ENV=production     0B
6d9f380d 30 seconds ago /bin/sh -c #(nop) COPY dir:370f291eb553b0623…   1.51MB
6892c696 32 seconds ago /bin/sh -c bundle --deployment --without dev…   90B
24d2f3c5 35 seconds ago /bin/sh -c #(nop) COPY multi:5f12d01bf9056d0…   0B
e5a2749c 37 seconds ago /bin/sh -c #(nop) WORKDIR /app                  0B
3bf1c664 39 seconds ago /bin/sh -c apk add --no-cache --update build…   853kB
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Some noop operations like &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;apk add&lt;/code&gt; are not 0B size and add some small trace, but it’s totally negligible.&lt;/p&gt;

&lt;p&gt;The most important part is that the size of  &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY . .&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;6d9f380d&lt;/code&gt;, 1.51MB) and &lt;code class=&quot;highlighter-rouge&quot;&gt;rake assets:precompile&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;a4a7779e&lt;/code&gt;, 1.74MB) layers
are small, reflecting newly added files size.&lt;/p&gt;

&lt;h2 id=&quot;issue-with-incremental-copy&quot;&gt;Issue with incremental &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;For this moment there’s &lt;a href=&quot;https://github.com/moby/moby/issues/21950&quot;&gt;an open bug #21950 in Docker&lt;/a&gt; that describes a problem with incremental &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt; on some drivers.&lt;/p&gt;

&lt;p&gt;It affects new storage drivers such as &lt;code class=&quot;highlighter-rouge&quot;&gt;aufs&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;overlay2&lt;/code&gt;. Old &lt;code class=&quot;highlighter-rouge&quot;&gt;overlay&lt;/code&gt; works fine.&lt;/p&gt;

&lt;p&gt;I suggest using &lt;code class=&quot;highlighter-rouge&quot;&gt;overlay&lt;/code&gt; storage driver on the build server until this gets fixed.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Using master image improves both speed and storage usage.&lt;/p&gt;

&lt;p&gt;The only challenge left is to keep the master build up to date to keep the diff cached images small.
It’s a good idea to automate this process.&lt;/p&gt;

&lt;h2 id=&quot;edit-1112018&quot;&gt;EDIT 1/11/2018&lt;/h2&gt;

&lt;p&gt;In &lt;a href=&quot;/2018/caching-docker-rails-supplement/&quot;&gt;the next blog post I wrote about
improved solution using new, experimental &lt;code class=&quot;highlighter-rouge&quot;&gt;RUN --mount&lt;/code&gt; feature.&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><summary type="html">This post shows a simple way of improving Docker images built process. Using this technique reduces both required storage and build time.</summary></entry><entry><title type="html">Rails on Kubernetes - basic deployment setup</title><link href="http://blog.jedrychowski.org/2018/rails-kubernetes-basic-deployment/" rel="alternate" type="text/html" title="Rails on Kubernetes - basic deployment setup" /><published>2018-06-24T11:00:00+00:00</published><updated>2018-06-24T11:00:00+00:00</updated><id>http://blog.jedrychowski.org/2018/rails-kubernetes-basic-deployment</id><content type="html" xml:base="http://blog.jedrychowski.org/2018/rails-kubernetes-basic-deployment/">&lt;p&gt;This post is a part of the bigger series.
The guide presents an opinionated evolution of the Kubernetes deployment,
starting with a very simple setup described here.&lt;/p&gt;

&lt;p&gt;It will guide you through all necessary, small steps towards fully automated
CI/CD pipeline.&lt;/p&gt;

&lt;h2 id=&quot;example-application&quot;&gt;Example application&lt;/h2&gt;

&lt;p&gt;Most Rails applications use a similar stack:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;web application itself&lt;/li&gt;
  &lt;li&gt;background job processor (e.g. Sidekiq or Resque)&lt;/li&gt;
  &lt;li&gt;external services
    &lt;ul&gt;
      &lt;li&gt;RDBMS (e.g. PostgreSQL or MySQL)&lt;/li&gt;
      &lt;li&gt;Redis&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This guides is based on an example Rails 5.2 application available &lt;a href=&quot;https://github.com/gogiel/rails-example-app&quot;&gt;on Github &lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It implements the stack, using Sidekiq as the background jobs processor and PostgreSQL as the RDBMS.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;To make this post and the guide concise it assumes that
the reader is familiar with basic Docker and Kubernetes concepts.&lt;/p&gt;

&lt;p&gt;Software requirements not covered in this post:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Kuberentes cluster&lt;/li&gt;
  &lt;li&gt;PostgreSQL and Redis databases, accessible in the cluster&lt;/li&gt;
  &lt;li&gt;Docker image repository -  public or private, accessible in the cluster&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;kubernetes&quot;&gt;Kubernetes&lt;/h3&gt;

&lt;p&gt;There are plenty of guides and tutorials online describing Kubernetes setup.
Official documentation is fantastic too.&lt;/p&gt;

&lt;p&gt;For beginners looking for a hassle-free cloud solution I recommend Google Cloud.
Kubernetes setup is much easier and cheaper than on AWS.&lt;/p&gt;

&lt;h3 id=&quot;services---postgres-and-redis&quot;&gt;Services - Postgres and Redis&lt;/h3&gt;

&lt;p&gt;The application requires two external services: PostgreSQL and Redis.
They don’t necessarily have to be installed on Kubernetes.&lt;/p&gt;

&lt;p&gt;I used Helm charts to simplify the setup.
Charts available in the repository &lt;a href=&quot;https://github.com/gogiel/rails-example-app/tree/basic-kubernetes/deployment/databases&quot;&gt;deployment/databases/&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;docker-image-repository&quot;&gt;Docker image repository&lt;/h2&gt;

&lt;p&gt;The most popular is repository is Docker Hub. It has a free plan with 1 private repository.&lt;/p&gt;

&lt;p&gt;Each major cloud providers such as GCP, AWS, Azure also provide the service.&lt;/p&gt;

&lt;h2 id=&quot;production-image&quot;&gt;Production image&lt;/h2&gt;

&lt;p&gt;Rails production image differs form the local image
(&lt;a href=&quot;/2018/rails-docker-development/&quot;&gt;read more about local image&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;There are few features that characterize production image:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;assets are precompiled&lt;/li&gt;
  &lt;li&gt;image has the code &lt;em&gt;baked in&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;optionally - ignores unnecessary repository files like docs, specs&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;deployment&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;test&lt;/code&gt; Gemfile groups are omitted&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of these differences it’s a good idea to have separate Dockerfiles
for both local and production environments.&lt;/p&gt;

&lt;p&gt;Production Dockerfile I created:&lt;/p&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; ruby:2.5.1-alpine&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;apk add &lt;span class=&quot;nt&quot;&gt;--no-cache&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--update&lt;/span&gt; build-base &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;                                postgresql-dev &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;                                nodejs &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;                                tzdata

&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; /app&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; Gemfile Gemfile.lock ./&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle &lt;span class=&quot;nt&quot;&gt;--deployment&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--without&lt;/span&gt; development &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; app/ ./app/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; bin/ ./bin/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; config ./config/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; db ./db/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; lib ./lib/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; public ./public/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; config.ru package.json Rakefile ./&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; RAILS_ENV production&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;rake assets:precompile
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; tmp/

&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; RAILS_SERVE_STATIC_FILES true&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; RAILS_LOG_TO_STDOUT true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It uses multiple &lt;code class=&quot;highlighter-rouge&quot;&gt;COPY&lt;/code&gt; statements to
explicitly whitelist included files and directories.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rm -rf tmp/&lt;/code&gt; removes unnecessary caches
created by assets precompliation.&lt;/p&gt;

&lt;p&gt;In this setup reverse proxy like nginx is not used.
Rails application is responsible for serving static content.
In default Rails 5.2 setup &lt;code class=&quot;highlighter-rouge&quot;&gt;RAILS_SERVE_STATIC_FILES&lt;/code&gt; controls the feature,
&lt;a href=&quot;https://github.com/gogiel/rails-example-app/blob/fa186d12d5e9c085a0e75e59f2c77364343f3f83/config/environments/production.rb#L23&quot;&gt;configuration code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The same applies to &lt;code class=&quot;highlighter-rouge&quot;&gt;RAILS_LOG_TO_STDOUT&lt;/code&gt;.
We want to log directly to STDOUT as this is a default
and the simplest solution for containers.&lt;/p&gt;

&lt;p&gt;Production Dockerfile can be kept in the repository.&lt;/p&gt;

&lt;p&gt;I recommend keeping &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt; filename for the local image definition.
I decided to call production definition &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile.production&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It’s now time to build the image.
Use &lt;code class=&quot;highlighter-rouge&quot;&gt;-f&lt;/code&gt; parameter of &lt;code class=&quot;highlighter-rouge&quot;&gt;docker build&lt;/code&gt; to specify custom Dockerfile path.
Example command:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker build -f Dockerfile.production -t rails-example-app-production .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After building push the image to your Docker registry.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://hub.docker.com/r/janjedrychowski/rails-example-app-production/&quot;&gt;Docker repository for the test app&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;secrets-and-configuration-setup&quot;&gt;Secrets and configuration setup&lt;/h2&gt;

&lt;p&gt;Test app secrets:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;master.key&lt;/code&gt; to decode Rails 5.2 &lt;code class=&quot;highlighter-rouge&quot;&gt;credentials.yml.enc&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Postgres connection credentials&lt;/li&gt;
  &lt;li&gt;Redis connection URL&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;masterkey&quot;&gt;master.key&lt;/h3&gt;

&lt;p&gt;By design &lt;code class=&quot;highlighter-rouge&quot;&gt;master.key&lt;/code&gt; shouldn’t be available publicly.
However because it’s an example application it’s included in the repository.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create secret generic master.key --from-file=./config/master.key
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;generates secret called &lt;code class=&quot;highlighter-rouge&quot;&gt;master.key&lt;/code&gt; with one key, &lt;code class=&quot;highlighter-rouge&quot;&gt;master.key&lt;/code&gt; with the file content.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl describe secret master.key

Name:         master.key
Namespace:    default
Labels:       &amp;lt;none&amp;gt;
Annotations:  &amp;lt;none&amp;gt;

Type:  Opaque

Data
====
master.key:  32 bytes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;databases-connections-credentials&quot;&gt;Databases connections credentials&lt;/h4&gt;

&lt;p&gt;Redis and Postgres connection credential secrets are defined in &lt;a href=&quot;https://github.com/gogiel/rails-example-app/tree/basic-kubernetes/deployment/secrets&quot;&gt;deployment/secrets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To load them to Kubernetes run:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create -f deployment/secrets/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s worth noting that values are stored in base64 form.
&lt;a href=&quot;https://github.com/gogiel/rails-example-app/blob/basic-kubernetes/deployment/secrets/redis_secret.yml#L7&quot;&gt;redis.url&lt;/a&gt;
value in the config YAML is stored as &lt;code class=&quot;highlighter-rouge&quot;&gt;cmVkaXM6Ly9yZWRpcy1zYW5kYm94LW1hc3Rlcg==&lt;/code&gt;.
It can be decoded using &lt;code class=&quot;highlighter-rouge&quot;&gt;base64&lt;/code&gt; util:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ echo &quot;cmVkaXM6Ly9yZWRpcy1zYW5kYm94LW1hc3Rlcg==&quot; | base64 --decode
redis://redis-sandbox-master%
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;database-creation&quot;&gt;Database creation&lt;/h2&gt;

&lt;p&gt;It’s time to create a production database.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/gogiel/rails-example-app/blob/basic-kubernetes/deployment/migration.yml&quot;&gt;deployment/migration.yml&lt;/a&gt;
defines Kubernetes Job that runs &lt;code class=&quot;highlighter-rouge&quot;&gt;bin/rake db:create db:migrate&lt;/code&gt;. Full definition:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;batch/v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Job&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;generateName&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app-migration-&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;docker.io/janjedrychowski/rails-example-app-production:v1&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bin/rake&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;db:create&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;db:migrate&quot;&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_HOST&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;production-database&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;host&lt;/span&gt;
          &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_PASSWORD&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;production-database&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;restartPolicy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Never&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As before to run it execute:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create -f deployment/migration.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Created Job has randomized name that starts with &lt;code class=&quot;highlighter-rouge&quot;&gt;rails-example-app-migration-&lt;/code&gt;.
This way we can create the job more than once without deleting the old one.&lt;/p&gt;

&lt;p&gt;In my case command printed:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;job.batch &quot;rails-example-app-migration-hxptn&quot; created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run describe on this job to check what Pod it created. We want to check its log
using &lt;code class=&quot;highlighter-rouge&quot;&gt;kubectl log &amp;lt;POD_ID&amp;gt;&lt;/code&gt;. Exemplary &lt;a href=&quot;https://gist.github.com/gogiel/6f7f76ced5cd9d44616ea8af5d3f4f62&quot;&gt;execution log&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;kubernetes-job-alternatives&quot;&gt;Kubernetes Job alternatives&lt;/h3&gt;

&lt;p&gt;It is possible to use &lt;code class=&quot;highlighter-rouge&quot;&gt;kubectl run&lt;/code&gt; to run the migration but it’s much more complicated.
See &lt;a href=&quot;https://github.com/kubernetes/kubernetes/pull/48684&quot;&gt;kubernetes/kubernetes#48684&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;Running new Pods with preconfigured variables can be simplified with
&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/pods/podpreset/&quot;&gt;PodPresets&lt;/a&gt;.
This is an alpha feature and it shouldn’t be used in production.
Examples available at
&lt;a href=&quot;https://kubernetes.io/docs/tasks/inject-data-application/podpreset/&quot;&gt;the official tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;first-deployment&quot;&gt;First Deployment&lt;/h2&gt;

&lt;p&gt;Web app definition in &lt;code class=&quot;highlighter-rouge&quot;&gt;deployment/web.yml&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app-web&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app-web&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;docker.io/janjedrychowski/rails-example-app-production:v1&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;3000&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bin/docker/web_start&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_HOST&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;production-database&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;host&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_PASSWORD&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;production-database&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;RAILS_MASTER_KEY&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;master.key&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;master.key&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;REDIS_URL&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;redis&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;url&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Sidekiq Deployment definition in &lt;code class=&quot;highlighter-rouge&quot;&gt;deployment/sidekiq.yml&lt;/code&gt; is nearly the same.
The main difference is the command and lack of external ports:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app-sidekiq&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;matchLabels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;sidekiq&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;sidekiq&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app-web&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;docker.io/janjedrychowski/rails-example-app-production:v1&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bin/docker/worker_start&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_HOST&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;production-database&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;host&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;DB_PASSWORD&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;production-database&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;RAILS_MASTER_KEY&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;master.key&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;master.key&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;REDIS_URL&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;redis&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;url&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To create Deployments run:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create -f deployment/web.yml -f deployment/sidekiq.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To verify that it’s working forward port &lt;code class=&quot;highlighter-rouge&quot;&gt;3000&lt;/code&gt; from one of the pods.&lt;/p&gt;

&lt;p&gt;Run &lt;code class=&quot;highlighter-rouge&quot;&gt;kubectl get pods&lt;/code&gt; and find a pod that starts with &lt;code class=&quot;highlighter-rouge&quot;&gt;rails-example-app-web-&lt;/code&gt;.
In my case the full name is &lt;code class=&quot;highlighter-rouge&quot;&gt;rails-example-app-web-c475bdbb4-k72m7&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To forward the port I run:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl port-forward rails-example-app-web-c475bdbb4-k72m7 3000:3000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Rails app should be now available at &lt;a href=&quot;http://localhost:3000/&quot;&gt;http://localhost:3000/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To verify that Sidekiq workers are working correctly visit &lt;a href=&quot;http://localhost:3000/admin/sidekiq/busy&quot;&gt;http://localhost:3000/admin/sidekiq/busy&lt;/a&gt;.
In Processes section there should be 2 Sidekiq processes running.&lt;/p&gt;

&lt;h2 id=&quot;kubernetes-service&quot;&gt;Kubernetes Service&lt;/h2&gt;

&lt;p&gt;The last step is creating a Service.
This way Deployment Pods will be accessible on the Internet.&lt;/p&gt;

&lt;p&gt;In real production environment usually &lt;code class=&quot;highlighter-rouge&quot;&gt;LoadBalancer&lt;/code&gt; Service type is used.
In Cloud environment that setup automatically starts a Load Balancer (e.g. ELB on AWS).&lt;/p&gt;

&lt;p&gt;They are usually not cheap, so &lt;code class=&quot;highlighter-rouge&quot;&gt;NodePort&lt;/code&gt; can be used instead. Exemplary config
uses very high port, 30000, because by default Kuberenetes NodePorts have very strict limitations.&lt;/p&gt;

&lt;p&gt;Service definition in &lt;code class=&quot;highlighter-rouge&quot;&gt;deployment/service.yml&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app-web&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example-app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;tier&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NodePort&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;80&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;nodePort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;30000&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;3000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This can be created as any other Kubernetes object with &lt;code class=&quot;highlighter-rouge&quot;&gt;kubectl create -f&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now the webapp should be available on ther inernet at &lt;code class=&quot;highlighter-rouge&quot;&gt;http://&amp;lt;NODE_ID&amp;gt;:30000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To find Node external IP use &lt;code class=&quot;highlighter-rouge&quot;&gt;kubectl describe nodes&lt;/code&gt;. You can connect to any Node
in the cluster. All of them have the Service available at port 30000.&lt;/p&gt;

&lt;h2 id=&quot;deploying-new-version&quot;&gt;Deploying new version&lt;/h2&gt;

&lt;p&gt;Kubernetes support built-in rolling restart updates of Deployments.
To deploy new code new image is needed.&lt;/p&gt;

&lt;p&gt;New image can be created and pushed to the repository using the same steps as before.&lt;/p&gt;

&lt;p&gt;To update the images on both Web and Sidekiq deployments run:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl set image deployment/rails-example-app-web rails-example-app-web=docker.io/janjedrychowski/rails-example-app-production:v1-updated
kubectl set image deployment/rails-example-app-sidekiq rails-example-app-web=docker.io/janjedrychowski/rails-example-app-production:v1-updated
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;next-steps&quot;&gt;Next steps&lt;/h2&gt;

&lt;p&gt;This setup is very basic and has few big flaws:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;multiple moving parts&lt;/li&gt;
  &lt;li&gt;big duplicated code sections (e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;env&lt;/code&gt; section)&lt;/li&gt;
  &lt;li&gt;no automation&lt;/li&gt;
  &lt;li&gt;no logging and monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First two issues can be solved by using &lt;a href=&quot;https://helm.sh/&quot;&gt;Helm&lt;/a&gt;.
It allows creating packages, called &lt;em&gt;charts&lt;/em&gt;, that encapsulate multiple
Kubernetes objects.&lt;/p&gt;

&lt;p&gt;This topic will be covered in a next post in the series.&lt;/p&gt;</content><author><name></name></author><summary type="html">This post is a part of the bigger series. The guide presents an opinionated evolution of the Kubernetes deployment, starting with a very simple setup described here.</summary></entry><entry><title type="html">Rails and Docker - local development</title><link href="http://blog.jedrychowski.org/2018/rails-docker-development/" rel="alternate" type="text/html" title="Rails and Docker - local development" /><published>2018-06-11T16:56:00+00:00</published><updated>2018-06-11T16:56:00+00:00</updated><id>http://blog.jedrychowski.org/2018/rails-docker-development</id><content type="html" xml:base="http://blog.jedrychowski.org/2018/rails-docker-development/">&lt;p&gt;Most Rails application use similar stack: Rails + Postgres + Redis + worker (Sidekiq, Resque, Delayed::Job).&lt;/p&gt;

&lt;p&gt;In this post I want to share and explain my simple Docker Compose setup.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/gogiel/rails-example-app/tree/local-docker&quot;&gt;Repository with an example application&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;scripts&quot;&gt;Scripts&lt;/h2&gt;

&lt;p&gt;Dockerfile:&lt;/p&gt;
&lt;div class=&quot;language-dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; ruby:2.5.1-alpine&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;apk add &lt;span class=&quot;nt&quot;&gt;--no-cache&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--update&lt;/span&gt; build-base &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;                                postgresql-dev &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;                                nodejs &lt;span class=&quot;se&quot;&gt;\
&lt;/span&gt;                                tzdata
&lt;span class=&quot;k&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; APP_PATH /app&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Different layer for gems installation&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WORKDIR&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; $APP_PATH&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;COPY&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; Gemfile Gemfile.lock $APP_PATH/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;bundle
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;docker-compose.yml:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;3'&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bin/docker/web_start&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.:/app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;3000:3000&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;depends_on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;db&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;redis&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env_file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;dev.env&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;worker&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rails-example&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bin/docker/worker_start&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.:/app&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;depends_on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;db&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;redis&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;web&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env_file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;dev.env&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;redis&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;redis:4.0-alpine&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;redis_data:/data&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;postgres:alpine&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;postgres_data:/var/lib/postgresql/data&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;postgres_data&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;redis_data&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;dev.env:&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;REDIS_URL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;redis://redis
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;bin/docker/web_start:&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;
bundle
&lt;span class=&quot;nb&quot;&gt;rm &lt;/span&gt;tmp/pids/server.pid
bin/rake db:migrate &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; bin/rake db:setup
bin/rails server &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 3000 &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'0.0.0.0'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;bin/docker/web_start:&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/sh&lt;/span&gt;
bundle
sidekiq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;details&quot;&gt;Details&lt;/h2&gt;

&lt;p&gt;Important features that distinguish this setup from examples on the internet:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Lean &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;.
    &lt;ul&gt;
      &lt;li&gt;no &lt;code class=&quot;highlighter-rouge&quot;&gt;mkdir /app&lt;/code&gt;. &lt;code class=&quot;highlighter-rouge&quot;&gt;WORKDIR&lt;/code&gt; automatically creates it.&lt;/li&gt;
      &lt;li&gt;no &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPOSE&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD&lt;/code&gt;. Docker Compose handles it&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;bundle&lt;/code&gt; (shorcut for &lt;code class=&quot;highlighter-rouge&quot;&gt;bundle install&lt;/code&gt;) executed when starting container.
This way you don’t have to rebuild the image every time you change your gems.&lt;/li&gt;
  &lt;li&gt;In &lt;code class=&quot;highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; image build from local directory is marked with its own tag/label using &lt;code class=&quot;highlighter-rouge&quot;&gt;image: rails-example&lt;/code&gt; entry.
Thanks to this &lt;code class=&quot;highlighter-rouge&quot;&gt;web&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;worker&lt;/code&gt; reuse the same image. If you remove this line the same image is built twice by Docker Compose for no reason.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;test-it&quot;&gt;Test it&lt;/h2&gt;

&lt;p&gt;I’ve created an example Rails application so you can easily test it yourself.&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/gogiel/rails-example-app.git
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;rails-example-app
git checkout local-docker 
docker-compose up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><summary type="html">Most Rails application use similar stack: Rails + Postgres + Redis + worker (Sidekiq, Resque, Delayed::Job).</summary></entry><entry><title type="html">Docker host volumes - fix permissions with user namespaces</title><link href="http://blog.jedrychowski.org/2018/linux-user-namespaces/" rel="alternate" type="text/html" title="Docker host volumes - fix permissions with user namespaces" /><published>2018-06-10T16:16:00+00:00</published><updated>2018-06-10T16:16:00+00:00</updated><id>http://blog.jedrychowski.org/2018/linux-user-namespaces</id><content type="html" xml:base="http://blog.jedrychowski.org/2018/linux-user-namespaces/">&lt;h2 id=&quot;problem-description&quot;&gt;Problem description&lt;/h2&gt;

&lt;h3 id=&quot;tldr&quot;&gt;tl;dr&lt;/h3&gt;
&lt;p&gt;Files written in host volume created by root user in Docker containers are also owned by root on the host.
Use Docker namespaces to map your host user to the container root.&lt;/p&gt;

&lt;h3 id=&quot;docker-host-volumes&quot;&gt;Docker host volumes&lt;/h3&gt;

&lt;p&gt;In local development using languages and frameworks that support hot code reloading it is common to share a directory on your host system with the Docker container.
This can be achieved using built-in Docker feature called &lt;em&gt;host volumes&lt;/em&gt;.
This way you don’t have to rebuild the image every time you change make a change.&lt;/p&gt;

&lt;p&gt;Docker is a Linux only solution based on &lt;a href=&quot;https://en.wikipedia.org/wiki/LXC&quot;&gt;Linux Containers&lt;/a&gt;.
It is possible to run Docker on Mac OS or Windows hosts but underneath it uses a virtual machine running Linux with docker deamon.
In these systems Docker host volumes are implemented using &lt;abbr title=&quot;Network File System&quot;&gt;NFS&lt;/abbr&gt; or some other customer solutions.&lt;/p&gt;

&lt;p&gt;When Docker runs natively on a Linux host then it can directly access files on the host filesystem.&lt;/p&gt;

&lt;h3 id=&quot;host-volumes-on-linux-hosts&quot;&gt;Host volumes on Linux hosts&lt;/h3&gt;

&lt;p&gt;By default there’s no mapping of permissions and ownership between Linux host and Docker container.
In a Docker container you see the same permissions of files on host volumes as on a host.
What’s more interesting and problematic is that it works the other way too.&lt;/p&gt;

&lt;h4 id=&quot;example&quot;&gt;Example&lt;/h4&gt;

&lt;p&gt;Let’s create a directory with 2 files. All commands in this example are executed on the Linux host.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mkdir example
$ cd example
$ touch file1 file2
$ chmod 777 file2
$ ls -l
total 0
-rw-rw-r-- 1 jan jan 0 Jun 10 10:22 file1
-rwxrwxrwx 1 jan jan 0 Jun 10 10:22 file2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now check how Docker container sees them.&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker run --rm -v $PWD:/example -w /example alpine /bin/ls -l
total 0
-rw-rw-r--    1 1000     1000             0 Jun 10 17:22 file1
-rwxrwxrwx    1 1000     1000             0 Jun 10 17:22 file2
#
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can see that while permissions (&lt;code class=&quot;highlighter-rouge&quot;&gt;-rw-rw-r--&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;-rwxrwxrwx&lt;/code&gt;) are unchanged the ownership columns has changed from &lt;code class=&quot;highlighter-rouge&quot;&gt;jan&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;1000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While the output is different the actual ownership hasn’t really changed. &lt;code class=&quot;highlighter-rouge&quot;&gt;1000&lt;/code&gt; is my &lt;abbr title=&quot;User ID&quot;&gt;UID&lt;/abbr&gt; and &lt;abbr title=&quot;Group ID&quot;&gt;GID&lt;/abbr&gt;&lt;/p&gt;

&lt;p&gt;It can be verified using &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; command:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ id
uid=1000(jan) gid=1000(jan) groups=1000(jan),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(docker)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the Docker container my user and group &lt;code class=&quot;highlighter-rouge&quot;&gt;jan&lt;/code&gt; doesn’t exist thus &lt;code class=&quot;highlighter-rouge&quot;&gt;ls&lt;/code&gt; is not able to map the files ownership IDs to user labels.&lt;/p&gt;

&lt;p&gt;As I said earlier most Docker images use root default user by default. Alpine is no exception here:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker run --rm alpine whoami
root
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So what happens if we create a file in the host volume?&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker run --rm -v $PWD:/example -w /example alpine touch file3
$ ls -l
total 0
-rw-rw-r-- 1 jan  jan  0 Jun 10 10:22 file1
-rwxrwxrwx 1 jan  jan  0 Jun 10 10:22 file2
-rw-r--r-- 1 root root 0 Jun 10 10:40 file3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The file has root:root permissions!&lt;/p&gt;

&lt;p&gt;This behaviour is usually unexpected. For a practical example please see &lt;a href=&quot;/2018/rails-docker-bootstrap/&quot;&gt;the previous post about bootstrapping Rails with Docker&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;

&lt;p&gt;Thanks to &lt;a href=&quot;https://docs.docker.com/engine/security/userns-remap/&quot;&gt;user namespace&lt;/a&gt; it is possible to remap UID and GIDs in the containers.&lt;/p&gt;

&lt;p&gt;Namespacing isolates Docker files such as images and container files in &lt;code class=&quot;highlighter-rouge&quot;&gt;/var/lib/docker/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;All existing images and containers will not be available after enabling user namespace&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;From the docs:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Enabling userns-remap effectively masks existing image and container layers, as well as other Docker objects within /var/lib/docker/. This is because Docker needs to adjust the ownership of these resources and actually stores them in a subdirectory within /var/lib/docker/. It is best to enable this feature on a new Docker installation rather than an existing one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;User namespace and all the other namespaces were created to protect hosts, limiting processes in containers from accessing host resources.&lt;/p&gt;

&lt;p&gt;It is usually done by assigning some very high UID and GID to be sure that they don’t interfere with IDs on the host.&lt;/p&gt;

&lt;p&gt;In this scenario we want to use (abuse?) this system to map root (with ID &lt;code class=&quot;highlighter-rouge&quot;&gt;0&lt;/code&gt;) to our local Linux user.&lt;/p&gt;

&lt;h3 id=&quot;step-1---create-user-namespace&quot;&gt;Step 1 - create user namespace&lt;/h3&gt;

&lt;p&gt;Using &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; find out what’s your UID and the effective (primary) GID. Usually on popular distributions like Ubuntu both are equal &lt;code class=&quot;highlighter-rouge&quot;&gt;1000&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ id
uid=1000(jan) gid=1000(jan) groups=1000(jan),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(docker)
$ id -u
1000
$ id -g
1000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/subuid&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/subgid&lt;/code&gt; to set user and group namespace respectively.
In my setup on Ubuntu these files exist by default defining namespace equal to my username.&lt;/p&gt;

&lt;p&gt;Namespace definition format is described in more details in &lt;a href=&quot;https://docs.docker.com/engine/security/userns-remap/#about-remapping-and-subordinate-user-and-group-ids&quot;&gt;the docs&lt;/a&gt;.
It is a colon seperated triplet describing:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;namespace name&lt;/li&gt;
  &lt;li&gt;first ID in the namespace&lt;/li&gt;
  &lt;li&gt;number of possible IDs in the namespace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my case both &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/subuid&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/subgid&lt;/code&gt; have the same content:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jan:1000:65536
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;65536 doesn’t mean anything, it’s just a very high value.&lt;/p&gt;

&lt;h3 id=&quot;step-2---enable-namespace-in-docker&quot;&gt;Step 2 - enable namespace in Docker&lt;/h3&gt;

&lt;p&gt;Edit &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/docker/daemon.json&lt;/code&gt; using the following template (you might need to create this file if it doesn’t exist):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
  &quot;userns-remap&quot;: &quot;NAMESPACE_NAME&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my case it is:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
  &quot;userns-remap&quot;: &quot;jan&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now restart Docker. On Ubuntu by default it can be done using &lt;code class=&quot;highlighter-rouge&quot;&gt;service docker restart&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As I mentioned earlier all your previous containers are images are not available after enabling namespace.
They are masked, not deleted. You can go back to the default namespace by reverting this change.&lt;/p&gt;

&lt;h3 id=&quot;test&quot;&gt;Test!&lt;/h3&gt;

&lt;p&gt;Given the file structure from the previous section:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ls -l 
total 0
-rw-rw-r-- 1 jan  jan  0 Jun 10 10:22 file1
-rwxrwxrwx 1 jan  jan  0 Jun 10 10:22 file2
-rw-r--r-- 1 root root 0 Jun 10 10:40 file3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s check how Docker perceives the files now:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run --rm -v $PWD:/example -w /example alpine /bin/ls -l
total 0
-rw-rw-r--    1 root     root             0 Jun 10 17:22 file1
-rwxrwxrwx    1 root     root             0 Jun 10 17:22 file2
-rw-r--r--    1 nobody   nobody           0 Jun 10 17:40 file3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Great! Now my user and group are properly mapped inside the container to root.&lt;/p&gt;

&lt;p&gt;Notice that container root’s &lt;code class=&quot;highlighter-rouge&quot;&gt;file3&lt;/code&gt; is marked as &lt;code class=&quot;highlighter-rouge&quot;&gt;nobody&lt;/code&gt;’s.&lt;/p&gt;

&lt;p&gt;Final check - write inside the container.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker run --rm -v $PWD:/example -w /example alpine touch file4
$ ls -l
total 0
-rw-rw-r-- 1 jan  jan  0 Jun 10 10:22 file1
-rwxrwxrwx 1 jan  jan  0 Jun 10 10:22 file2
-rw-r--r-- 1 root root 0 Jun 10 10:40 file3
-rw-r--r-- 1 jan  jan  0 Jun 10 11:52 file4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;file4&lt;/code&gt; was mapped to &lt;code class=&quot;highlighter-rouge&quot;&gt;jan&lt;/code&gt; in the host as expected.&lt;/p&gt;

&lt;h3 id=&quot;resources&quot;&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Docker Github issues:
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/moby/moby/issues/2259&quot;&gt;Add ability to mount volume as user other than root #2259&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/moby/moby/issues/2969&quot;&gt;empty VOLUME is very restricted: root:root 700 #2969&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/moby/moby/issues/3124&quot;&gt;Volumes files have root owner when running docker with non-root user. #3124&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/engine/security/userns-remap/&quot;&gt;https://docs.docker.com/engine/security/userns-remap/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lwn.net/Articles/532593/&quot;&gt;https://lwn.net/Articles/532593/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><summary type="html">Problem description</summary></entry><entry><title type="html">Bootstrapping new Rails application without installing Ruby locally</title><link href="http://blog.jedrychowski.org/2018/rails-docker-bootstrap/" rel="alternate" type="text/html" title="Bootstrapping new Rails application without installing Ruby locally" /><published>2018-06-07T18:28:31+00:00</published><updated>2018-06-07T18:28:31+00:00</updated><id>http://blog.jedrychowski.org/2018/rails-docker-bootstrap</id><content type="html" xml:base="http://blog.jedrychowski.org/2018/rails-docker-bootstrap/">&lt;p&gt;This guide shows and explains bootstrapping new Rails application using Docker. This way you don’t have to install Ruby and other tools on your host.&lt;/p&gt;

&lt;p&gt;In my case I want to use PostgreSQL as a default database. The magic command is:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run --rm -v $PWD:/app -w /app ruby:2.5.1-alpine /bin/sh -c 'apk add --update build-base postgresql-dev &amp;amp;&amp;amp; gem install rails -v &quot;5.2.0&quot; &amp;amp;&amp;amp; rails new . --database=postgresql'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;how-does-it-work&quot;&gt;How does it work?&lt;/h2&gt;

&lt;p&gt;Let’s break the command down into parts:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;docker run&lt;/code&gt; - this creates a new Docker container&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--rm&lt;/code&gt; - remove the image after running &lt;code class=&quot;highlighter-rouge&quot;&gt;rails new&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;-v $PWD:/app&lt;/code&gt; - mount current directory in &lt;code class=&quot;highlighter-rouge&quot;&gt;/app&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;-w /app&lt;/code&gt; - set default work directory to &lt;code class=&quot;highlighter-rouge&quot;&gt;/app&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ruby:2.5.1-alpine&lt;/code&gt; - image identifier of a &lt;a href=&quot;https://hub.docker.com/_/ruby/&quot;&gt;ruby official repository&lt;/a&gt; image tagged as &lt;code class=&quot;highlighter-rouge&quot;&gt;2.5.1-alpine&lt;/code&gt;.
2.5.1 refers to the Ruby version. &lt;code class=&quot;highlighter-rouge&quot;&gt;alpine&lt;/code&gt; is a lightweight Linux distribution that weights only around 5MB.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;/bin/sh -c '...'&lt;/code&gt; - a command to run in the Docker container. We need to run multiple commands (combined with &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt;).
The only way to achieve that is to run them in a shell. We use &lt;code class=&quot;highlighter-rouge&quot;&gt;sh&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;bash&lt;/code&gt; because by default Alpine doesn’t have bash installed.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;apk add --update build-base postgresql-dev&lt;/code&gt; - installs Rails and pg gem (used for PostgreSQL) dependencies&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;gem install rails -v &quot;5.2.0&quot;&lt;/code&gt; - installs Rails&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rails new . --database=postgresql&lt;/code&gt; - bootstraps Rails application using Postgres as default DB. Finally!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;interactive-rails-new&quot;&gt;Interactive rails new&lt;/h2&gt;

&lt;p&gt;If you want to experiment with &lt;code class=&quot;highlighter-rouge&quot;&gt;rails new&lt;/code&gt; parameters you can use an interactive shell instead.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run --rm -it -v $PWD:/app -w /app ruby:2.5.1-alpine /bin/sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Please notice new docker option: &lt;code class=&quot;highlighter-rouge&quot;&gt;-it&lt;/code&gt;. These are two separate options: &lt;code class=&quot;highlighter-rouge&quot;&gt;-i&lt;/code&gt; (also&lt;code class=&quot;highlighter-rouge&quot;&gt;--interactive&lt;/code&gt;) and &lt;code class=&quot;highlighter-rouge&quot;&gt;-t&lt;/code&gt; (also &lt;code class=&quot;highlighter-rouge&quot;&gt;--tty&lt;/code&gt;). Usually you want to use them together when running anything interactive.&lt;/p&gt;

&lt;h2 id=&quot;custom-application-name&quot;&gt;Custom application name&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rails new&lt;/code&gt; required &lt;code class=&quot;highlighter-rouge&quot;&gt;PATH&lt;/code&gt; argument not only determines the path but also is used to set the application name.
The app name is used in &lt;code class=&quot;highlighter-rouge&quot;&gt;config/application.rb&lt;/code&gt; to define a top module.
Unfortunately for some reason it’s not possible to provide application name as an option.
To work around this issue use custom volume mountpoint and absolute path of current directory instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;.&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run --rm -v $PWD:/$(basename $PWD) -w /$(basename $PWD) ruby:2.5.1-alpine /bin/sh -c 'apk add --update build-base postgresql-dev &amp;amp;&amp;amp; gem install rails -v &quot;5.2.0&quot; &amp;amp;&amp;amp; rails new $PWD --database=postgresql'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;file-permissions-in-linux-host-volumes&quot;&gt;File permissions in Linux host volumes&lt;/h2&gt;

&lt;p&gt;If you are using a Linux host to run Docker all files created by &lt;code class=&quot;highlighter-rouge&quot;&gt;rails new&lt;/code&gt; are owned by root on your local machine.&lt;/p&gt;

&lt;p&gt;The problem is fixable using &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo chown&lt;/code&gt; but it’s just a temporary solution. The only permit solution is to use user namespace that will help you with remapping your host user to Docker.&lt;/p&gt;

&lt;p&gt;My post about &lt;a href=&quot;/2018/linux-user-namespaces/&quot;&gt;Linux user namespaces&lt;/a&gt; shows how to fix this properly.&lt;/p&gt;

&lt;p&gt;This problem doesn’t exist on Mac OS or Windows where NFS is used to mount local volumes.&lt;/p&gt;</content><author><name></name></author><summary type="html">This guide shows and explains bootstrapping new Rails application using Docker. This way you don’t have to install Ruby and other tools on your host.</summary></entry></feed>