All threads / Docker Basics for Rails Discussion

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Docker Basics for Rails Discussion

Dicussion 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/

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

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.

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?

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
Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.