Dockerizing a Ruby on Rails Application

In this tutorial, you will learn how to dockerize a Ruby on Rails application. The application we’re going to build will make use of PostgreSQL, Redis, and Sidekiq.

We’ll also be using Unicorn and Nginx in both development and production. If you would prefer to use Puma or something else, this shouldn’t be an issue.

After reading this article:

  • You will have a basic idea of what Docker is.
  • How Docker can help you to streamline development.
  • How you can use Continuous Integration and Delivery (CI/CD) to build and test your Ruby projects.

You can find the complete code for this tutorial in this repository.

It looks like the author accidentally don’t add the one line of code for the docker-compose.yml file.
Namely, the volumes for the drkiq service are not specified and because of this, rails generates files inside the container, and not in a directory on the local host. But the volumes should be in docker-compose.yml and the files should be generated in a directory on the local host because as the author said

Right now the source code is on your work station, and that source code is being mounted into the Docker container in real-time through a volume.

To solve the problem, I added the lines in docker-compose.yml in services: in drkiq:
- ./drkiq:/opt/app

Hi @ComradeSukhov thank you for reporting this. I’ll check the post, but I think you’re right.

Hello @TomFern. I was really happy to help

To solve the problem, I added the lines in `docker-compose.yml` in `services:` in `drkiq:`
`- ./drkiq:/opt/app`

@ComradeSukhov You were completely right. Sorry for the mistake here. I’ve updated the blog post with your suggestion. Thank you again.

Thanks for the brilliant tutorial. Incredibly useful

1 Like

Following this tutorial on macOS 11.3 and encountered this issue:

 => ERROR [5/6] RUN chown -R user:user /opt/app                                                                          0.4s
 > [5/6] RUN chown -R user:user /opt/app:
#9 0.357 chown: invalid group: ‘user:user’

Anyone else encounter and overcome this issue?

@johnpitchko I’m pretty sure this is caused by the same thing I encountered when running build from the Dockerfile

 => ERROR [2/7] RUN addgroup --gid 20 user
 > [2/7] RUN addgroup --gid 20 user:
#5 0.227 addgroup: The GID `20' is already in use.

But I’m not sure how to fix it yet.

@johnpitchko @maneKozaroo I’m sorry you’re having trouble. I don’t have a Mac at hand to test what’s happening but it seems that the user or group id on Mac clashes with the one inside the docker container. You could try running the container as root and using the File Sharing tab on Docker Desktop to mount the volume.

Let me know if a Dockerfile like this helps:

# Dockerfile.rails
FROM ruby:2.7.2 AS rails-toolbox



CMD ["/bin/sh"]