# Ruby on Rails Deployments

**URL:** <https://community.fly.io/t/ruby-on-rails-deployments/341>\
**Category:** General\
**Created:** [October 29, 2020, 6:14pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341 "2020-10-29T18:14:41Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 29, 2020, 6:14pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/1 "2020-10-29T18:14:42Z")

</div>

Hey everyone,

I am looking for an example Dockerfile / CircleCI config for deploying a Rails app on Fly (migrating from Heroku)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![michael](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/michael/32/16_2.png) [@michael](https://community.fly.io/u/michael)\
**Post date:** [October 30, 2020, 6:04am UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/2 "2020-10-30T06:04:51Z")

</div>

Here’s an example rails app: [https://github.com/superfly/rails-example/blob/master/Dockerfile](https://github.com/superfly/rails-example/blob/master/Dockerfile)

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 6:16am UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/3 "2020-10-30T06:16:45Z")

</div>

Thanks @michael - Anything special we need to do on the circle side of things?

---

<div class="post-metadata">

**Author:** ![michael](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/michael/32/16_2.png) [@michael](https://community.fly.io/u/michael)\
**Post date:** [October 30, 2020, 3:50pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/4 "2020-10-30T15:50:15Z")

</div>

@danwetherald I don’t think so, though I don’t have experience with circle’s latest config format. Here’s a few posts that might help though:

> [@Deploying to Fly via CircleCI](https://community.fly.io/t/deploying-to-fly-via-circleci/48):
>
> We’ve been using Fly for a while, and deploying commits on our main branch to a staging environment. Up until now, we had been doing this via a multi-stage Dockerfile, but builds had been taking a while since we weren’t doing them locally. So, we opted to build the Docker image locally in CI and deploy to Fly. [More info in the Circle docs here](https://circleci.com/docs/2.0/building-docker-images/). Figured I would post what we learned. To achieve this, we simplified our Dockerfile to just deploy, rather than to build and deploy, and moved our build…

> [@Continuous Deployment with Buildkite](https://community.fly.io/t/continuous-deployment-with-buildkite/78/4):
>
> Here’s my pipeline.yml file. The rest is straight from this tutorial [https://buildkite.com/docs/tutorials/docker-containerized-builds](https://buildkite.com/docs/tutorials/docker-containerized-builds). steps: - command: sh scripts/test.sh plugins: - docker-compose#v3.3.0: run: test config: - docker-compose.yml - docker-compose.test.yml - label: ":docker: Build" branches: master plugins: - docker-compose#v3.3.0: build: - web - worker - sync - …

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [October 30, 2020, 3:53pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/5 "2020-10-30T15:53:19Z")

</div>

We also have an example for CircleCI: [https://github.com/fly-examples/flygreeting/tree/main/.circleci](https://github.com/fly-examples/flygreeting/tree/main/.circleci)

The README has pretty good instructions, but you basically use CircleCI like you normally would, then run `flyctl deploy` after tests pass.

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 4:07pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/6 "2020-10-30T16:07:21Z")

</div>

Thanks guys, question, It was a bit confusing when using the heroku buildpack - I am not sure where the server startup command comes from when implementing this.

We are also receiving errors when deploying locally with this buildpack vs the first deploy from the Turboku one click migration.

 ![image](https://global.discourse-cdn.com/fly1/original/3X/d/6/d6a8dd01d20dd8e97f88ff6951bae28a0128de9f.png)

---

<div class="post-metadata">

**Author:** ![michael](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/michael/32/16_2.png) [@michael](https://community.fly.io/u/michael)\
**Post date:** [October 30, 2020, 4:13pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/7 "2020-10-30T16:13:16Z")

</div>

The heroku buildpack looks for `web` in the Procfile to discover the startup command.

That error makes me think either `tmp` or `tmp/pids` doesn’t exist in the image. Are you trying to migrate your deploy from turboku to flyctl+buildpacks or a Dockerfile?

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 4:33pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/8 "2020-10-30T16:33:26Z")

</div>

Gotcha, I had a feeling it was using the Procfile - but I was not 100% sure.

I would like to keep it as simple as possible and use the buildpack rather than a long winded Dockerfile. But I can’t seem to get around that `tmp/pids` issue when deploying with `flyctl`.

---

<div class="post-metadata">

**Author:** ![michael](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/michael/32/16_2.png) [@michael](https://community.fly.io/u/michael)\
**Post date:** [October 30, 2020, 4:34pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/9 "2020-10-30T16:34:36Z")

</div>

Which buildpack(s) are you using?

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 4:44pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/10 "2020-10-30T16:44:36Z")

</div>

```auto
[build]
  builder = "heroku/buildpacks:18"

```

This was the default when using the Turboku tool.

---

<div class="post-metadata">

**Author:** ![michael](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/michael/32/16_2.png) [@michael](https://community.fly.io/u/michael)\
**Post date:** [October 30, 2020, 4:47pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/11 "2020-10-30T16:47:20Z")

</div>

Ah I found something in the heroku docs that says you need to create the `tmp/pids` directory for Rails 6

 ![image](https://global.discourse-cdn.com/fly1/original/3X/2/f/2f203a6e12561ee43bb3ab040a581ce8613ef0b5.png)

> **[Getting Started on Heroku with Rails 6.x | Heroku Dev Center](https://devcenter.heroku.com/articles/getting-started-with-rails6#create-a-procfile)**
>
> Creating, configuring, deploying and scaling Rails 6.x applications on Heroku, using Bundler dependency management.

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 4:49pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/12 "2020-10-30T16:49:10Z")

</div>

We actually have already created these dirs locally and are in the repo after talking to @kurt yesterday, still seeing the error.

![image](https://global.discourse-cdn.com/fly1/original/3X/f/1/f16231322a3bc06849063722410a47e0c7aeb914.png)

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [October 30, 2020, 5:31pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/13 "2020-10-30T17:31:07Z")

</div>

Is that directoy in `.gitignore`? If it’s not actually in Git our builder won’t pick it up.

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 5:45pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/14 "2020-10-30T17:45:37Z")

</div>

Ah, yes the tmp directory is indeed in the `.gitignore` - I will make the ignore statements a bit more specific to only ignore cache and some other items in the tmp dir and give it another go.

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 5:59pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/15 "2020-10-30T17:59:38Z")

</div>

Boom 🎉 - That was the answer @kurt - Looks like local builds are now working.

Now that we have a better understanding, we might think about moving to Dockerfile implementation as the build time is pretty slow when running remote builds, this might eat up time on circle - unless we can start the build and then drop the local process?

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [October 30, 2020, 6:08pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/16 "2020-10-30T18:08:51Z")

</div>

The trick to good build times on Rails is to do it in two parts and cache docker images. We have a an asset + Rubygems build stage (since our JS and CSS changes less frequently than everything else) and then a leaner, Rails only build stage that works on top of that.

If you get to a good Dockerfile + cache, the `flyctl deploy` will be quick. You can also build the Docker image separately and then deploy it with `-i` for slightly more control.

Either way, getting CircleCI to cache Docker image layers is the best first step. Our remote builder doesn’t cache layers so it’s always worst case.

---

<div class="post-metadata">

**Author:** ![danwetherald](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/danwetherald/32/37600_2.png) [@danwetherald](https://community.fly.io/u/danwetherald)\
**Post date:** [October 30, 2020, 7:24pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/17 "2020-10-30T19:24:16Z")

</div>

Makes sense, this rails app is only configured to be an API so not sure how much would cache on our end would do as we really do not have much assets to compile, etc.

My question would be what is different from when the image is created on Fly vs Heroku - On Heroku this happens in ~10 seconds.

Thanks

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [October 30, 2020, 10:16pm UTC](https://community.fly.io/t/ruby-on-rails-deployments/341/18 "2020-10-30T22:16:27Z")

</div>

Oh let’s get you a Dockerfile then. Rails is easy if you’re not using assets.

See if this works:

```auto
######################
# Stage: Gem Builder
FROM ruby:2.6.5 as Builder

ARG BUNDLE_WITHOUT="development test" 
ARG RAILS_ENV="production"

ENV BUNDLE_WITHOUT=${BUNDLE_WITHOUT}

RUN apt-get update && apt-get install -y \
  build-essential \
  postgresql-client \
  git \
  npm \
  tzdata \
  brotli \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install gems
ADD Gemfile* /app/
RUN bundle config --global frozen 1 \
  && bundle install -j4 --retry 3 \
  # Remove unneeded files (cached *.gem, *.o, *.c)
  && rm -rf /usr/local/bundle/cache/*.gem \
  && find /usr/local/bundle/gems/ -name "*.c" -delete \
  && find /usr/local/bundle/gems/ -name "*.o" -delete

###############################
# Stage Final
FROM ruby:2.6.5

RUN apt-get update && apt-get install -y \
  file \
  postgresql-client \
  tzdata \
  && rm -rf /var/lib/apt/lists/*

# Add user
RUN groupadd -r app && useradd -r -g app app

USER app

# Copy app with gems from former build stage
COPY --from=Builder /usr/local/bundle/ /usr/local/bundle/

USER root
RUN mkdir -p /app/tmp/pids && chown -R app:app /app/tmp
ADD --chown=app:app . /app

# Set Rails env
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_ENV production

USER app
WORKDIR /app

# Start up
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

```
