Dockerize New Rails5 Application

Ayesha Kaleem
4 min readOct 16, 2019

This Quickstart guide shows you how to use Docker Compose to set up and run a Rails/PostgreSQL app.

Prerequisite: Docker and docker-compose should be installed.

Start by setting up the files needed to build the app. The app will run inside a Docker container containing its dependencies. Defining dependencies is done using a file called Dockerfile. To begin with, the Dockerfile consists of:

FROM ruby:2.5
MAINTAINER ayesha306
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
# 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"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

Now, create Gemfile and Gemfile.lock

touch Gemfile
touch Gemfile.lock

Next, create a bootstrap Gemfile that just loads Rails. It’ll be overwritten in a moment by rails new.

source 'https://rubygems.org'
gem 'rails', '~>5'

Next, provide an entrypoint script to fix a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists. This script will be executed every time the container gets started. entrypoint.sh consists of:

entrypoint.sh

#!/bin/bashset -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 “$@”

Finally, docker-compose.yml it is where the magic happens.

version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3001:3000"
depends_on:
- db

Build the project

With those files in place, you can now generate the Rails skeleton app within the same directory (we are using dot to represent the same directory) using docker-compose run:

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

First, Compose builds the image for the web service using the Dockerfile. Then it runs rails new inside a new container, using that image. Once it’s done, you should have generated a fresh app.

List the files.

$ ls -l

Now that you’ve got a new Gemfile, you need to build the image again. (This, and changes to the Gemfile or the Dockerfile should be the only times you’ll need to rebuild.)

docker-compose build

Connect the database

Replace the contents of config/database.yml with the following:

default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test

You can now boot the app with docker-compose up:

docker-compose up

If all’s well, you should see some PostgreSQL output.

Finally, you need to create the database. In another terminal, run:

docker-compose run web rake db:create

View the Rails welcome page!

That’s it. Your app should now be running on port 3001 on your Docker daemon.

On Docker Desktop for Mac and Docker Desktop for Windows, go to http://localhost:3001 on a web browser to see the Rails Welcome.

Now, you can play with the rails application within a container or can develop a whole application by using docker-compose run web as a prefix command.

for eg: docker-compose run web rail c

Above command will take you in the rails console

OR you can get into the container itself by running following commands:

docker ps #this command will show the running container

copy the container id and run following command:

docker exec -it <container id> bash

This will take you in the container. Now you can work with rails by running any rails commands. for example:

rails console

to exit the container without stopping it :

 ctrl+q

To stop the application, run

docker-compose down

in your project directory.

Restart the application

To restart the application run docker-compose up in the project directory as you don’t need to rebuild the docker-compose.

Rebuild the application

If you make changes to the Gemfile or the Compose file to try out some different configurations, you need to rebuild. Some changes require only docker-compose up --build, but a full rebuild requires a re-run of docker-compose run web bundle install to sync changes in the Gemfile.lock to the host, followed by docker-compose up --build.

Here is an example of the first case, where a full rebuild is not necessary. Suppose you simply want to change the exposed port on the localhost from 3000 in our first example to 3001. Make the change to the Compose file to expose port 3000 on the container through a new port, 3001, on the host, and save the changes:

ports: - "3001:3000"

Now, rebuild and restart the app with docker-compose up --build.

Inside the container, your app is running on the same port as before, but the Rails Welcome is now available on your localhost.

finally

You can push your image into the DockerHub,

From this stage, you must have a docker hub account with a unique username.

docker login

and then if authenticated you can use push command like

docker push [image-name]:tag name

docker push ayesha306/myapp:latest

So this is how I configured docker with rails and made my development and deployments easy.

Start yours today.

Happy coding :)

Cheers.

Github

https://github.com/ayesha54/dockerized_rails_app

--

--