top of page
Search

Dockerizing my first Rails application from point zero

Writer: Andres Ehrenpreis
Andres Ehrenpreis
Jun 20, 2020
4 min read

Updated: Jun 21, 2020


For the introduction - I intended to keep the setup short and trim the configuration files as much as possible.

Btw this GIF here was the first result when searching "docker" from the gif-library :)


I'm using the following software versions on my Mac OS Catalina v 10.15.5:

$ rails -v
Rails 6.0.3.2
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
$ docker -v
Docker version 19.03.8, build afacb8b

I also have Docker Desktop (v 2.3.0.3) for Mac, which includes a tool called Docker Compose.


All the code examples in this article are prefixed with either `$` or `#` depending of if the command is correspondingly meant to be run as unprivileged user or root.


I must state that I removed the webpacker gem (a tool which wraps webpack - the popular JavaScript tool used for managing and bundling JavaScript code) from the default Gemfile which ships with Rails setup since it requires presence of yarn (JavaScript package manager). These two gave me a big headache while working on this setup since webpacker didn't recognize the presence of yarn package in the system, this is a popular issue and I couldn't fix it.

If somebody is able to provide a solution for this, then please let me know.


To begin with...


I will do it from the point 0- that is from new empty Rails application which I set up before I set up the docker container. Start off with initializing new rails application:

$ rails new dock

Inside the dock directory in Gemfile remove the following lines to avoid webpacker installation-related problems with yarn:

# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'

Run bundle install again to remove webpacker from Gemfile.lock:

$ bundle install

From now you should have a fully working rails application with default sqlite database attached to it in your localhost. You can access it by initiating the rails server command in your application directory:

$ bundle exec rails server

and visiting localhost:3000 in your browser- you should see Rails welcome screen there:




Next up: the Docker


Define the Dockerfile:

I used the Rails-specific Dockerfile example provided by docker-docs and edited it to match my little-bit tweaked Rails setup. The differences in Dockerfile are:

  1. Since I removed the webpacker gem, then no yarn or webpacker installation is needed

  2. Since we already initialized new Rails application and installed the gem-bundle then I removed the corresponding lines from the docker-docs example

My Dockerfile looks like this:

FROM ruby:2.7.1

# Nodejs must be installed separately for rails to work out of the box.
RUN apt-get update -qq && apt-get install -y nodejs

RUN gem install rails

# Copy the application directory in your localhost.
COPY . /dock

# Set the default working directory to the freshly
# copied application directory.
WORKDIR /dock

# Install rails libraries (install everything that is noted down
# in Gemfile).
RUN bundle install

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh

ENTRYPOINT ["entrypoint.sh"]

# The EXPOSE instruction informs Docker that the container listens
# on the specified network ports at runtime. You can specify
# whether the port listens on TCP or UDP, and the default is TCP
# if the protocol is not specified.
# Look more at https://docs.docker.com/engine/reference/builder/#expose
EXPOSE 3000

# Start the main process: bind the rails server to 0.0.0.0 address
# in the container
CMD ["rails", "server", "-b", "0.0.0.0"]

As you can see I used the entrypoint.sh file. This is a file I made based on the instructions given here. For the clarity, the file contents in my project are:

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"


Docker-compose:


Compose is a tool for defining and running multi-container Docker applications. See further information from the docker documentation.

I used the Rails-specific docker-compose file example given here. I made edits to it regarding me using sqlite db instead of postgres as in the example. My final compose-file is the following:


version: '3'
services:
 web:
 build: .
 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
 volumes:
      - .:/dock
 ports:
      - "3000:3000"

It doesn't probably make sense to use the compose-file, since now we are not using multiple services to build the application, as in the docker-example, but only one. Since in the future I want to define additional services though then I wanted to include the file still.



Light it up:


Inside the project directory enter:

$ docker-compose up --build

This will build the container and start it up. It attaches the rails server to 0.0.0.0 port 3000 in the container and matches the 127.0.0.1:3000 inside your localhost to access the service in broswer.


That was it. This was the first time for me so probably could do some things better. To stop the container just hit Ctrl+c or type

$ docker stop dock_web_1

in another terminal window. As you can see docker automatically named the container based on the application name and service name in docker-compose file.


You can also get the repository done during this walk-through from here.


PS! If you liked the article you can buy me a coffee to support me going! :)


 
 
 

Recent Posts

See All

Comments


bottom of page