Ask A Question

Notifications

You’re not receiving notifications from this thread.

Docker Basics for Rails Discussion

Discussion for Docker Basics for Rails

If I run rails inside of Docker, does this set-up require more resources from my my machine?

Reply

Hey Chris, when I downloaded the Source Code GitHub repo and ran docker-compose up, it couldn't build sqlite3.

I tried adding a dependency for sqlite-dev in the Dockerfile, then it worked.
I opened a corresponding PR in your repo to add the dependency.

Reply

Great episode! I already use Docker for local development and I love it. Are you planning to do an episode to explain how to deploy a dockerized rails application? It seems very obscure to me.

Reply

Thanks for the great tutorial! I am a beginner in dockerizing rails app and get started with this tutorial. I realized some optimization may have been omitted to keep the tutorial concise. Following are some optimization tricks I found useful if starting from the Dockerfile :)

  1. Add (at least) node_modules and .git into a .dockerignore file, so the COPY . /app/ step could skip these files. (This could save you a log of time/docker image size when .git is huge)

  2. Avoid the error: 'A server is already running. Check /project/tmp/pids/server.pid. Exiting' with an entrypoint.sh script.
    Refer to Docker docs - Sample applications - Rails and PostgreSQL for the complete script, and don't forget to change #!/bin/bash to #!/bin/sh since we are using Alpine in this tutorial.

  3. Avoid needing to run yarn install again after change the source code and re-build image.
    Instead of:

COPY . /app/

ENV BUNDLE_PATH /gems
RUN yarn install
RUN bundle install

do:

ENV BUNDLE_PATH /gems
COPY package.json yarn.lock /app/
RUN yarn install
COPY Gemfile Gemfile.lock /app/
RUN bundle install
 # Steps above could be cached most of the time when re-building image

 # Copy source code after dependencies installed
COPY . /app/
Reply

I'm using this solution and it save a lot of time.
I strongly encourage using this technique with Selective disable caching
http://dev.im-bot.com/docker-select-caching/

So you can put this step befor copy your source code from local to image

Reply

Would this prevent gems from being re-installed every time you build the image, even when you did not make any changes to Gemfile?

Reply

This is great, please more docker + rails tutorials. I would like to see how to not re-install all gems if you rebuild the image but have not changed the Gemfile at all.

Reply

I agree with Drazen. More Docker+Rails tutorials. Especially on how to deploy to production. Also, how to add sidekiq.

Reply

Second this! Probably needs to be tweaked but here's my docker-compose.yml that currently works for local dev with sidekiq

services:
  postgres:
    image: postgres:10.4-alpine
    env_file:
      - ./env/common.env
      - ./env/dev.env
    ports:
      - 5432:5432
    volumes:
      - postgres:/var/lib/postgresql/data

  redis:
    image: redis:4.0-alpine
    ports:
      - 6379:6379
    volumes:
      - redis:/data

  app: &app_base
    build:
      context: .
      args:
        - BUNDLE_WITHOUT=production
    env_file:
      - ./env/common.env
      - ./env/dev.env
    command: bundle exec puma -C config/puma.rb
    ports:
      - 3000:3000
    volumes:
      - .:/app
    depends_on:
      - postgres
      - redis
    stdin_open: true
    tty: true

  sidekiq:
    <<: *app_base
    command: bundle exec sidekiq -C config/sidekiq.yml
    ports: []
    stdin_open: false
    tty: false

volumes:
  redis:
  postgres:

Note - in the docker file you can't have the last 2 lines of the tutorial triggering rails s

Reply

What if I have a windows computer and dont want to install ruby on the computer. Can I create a docker environment that has ruby and rails installed and where I do all the development, editing, rails new inside the image. Even run vscode outside and ssh into the image. Is that possible, and do you have a pointer for an article of yours or elsewhere to help me set that up?

Reply

You can make docker-compose create the project for you if you already have defined a Dockerfile and a docker-compose.yml:

docker-compose run --no-deps web rails new . --force --database=postgresql

See more at https://docs.docker.com/samples/rails/#build-the-project

Reply

Hello, I am getting this error "Don't know how to build task 'bundle' (See the list of available tasks with rails --tasks)" after running docker-compose run web bundle. Any idea why it is? Thanks

Reply

Sweet thanks for the tutorial.

Does anyone know how to deploy AN UPDATE to your rails app: for example, I've made some bug fixes.

I want to deploy these change to an already running docker container hosted on a Digital Ocean droplet.

Any ideas?

Reply

Great episode thanks Chris!
Is there another one on Docker for deploying in production?

Reply

Note to others, running Rails 7.0.4.1 with dartsass-rails for your SASS compiling, you may run into segmentation violation (SEGV) errors when running the dartsass:watch command in your Procfile.dev (valled via bin/dev by default in Rails 7+) on ALPINE Linux base images.

I tested on the standard ruby:3.1.2 vs ruby:3.1.2-alpine, and the standard image works fine but Alpine has SEGV errors. Not sure if there's an Alpine dependency that's missing that may resolve, but since I was working on local development, I didn't investigate too much and just went with the base ruby:3.1.2 image. Maybe I'll circle back once the Rails 7.1+ updates implement a default Docker setup, hopefully some folks more knowledgeable about Docker and images have resolved it for us all :)

Reply

I had to fork & tweak this a little to get it running on my M1 Mac, I'll post the repo link here once I think its usable for others.

Reply

Nice video. Coming back to docker after a couple years and you did a great job explaining what matters for rails setups.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.