How to create a Rails 6 image in Docker — Ubuntu 18.04

Arthur Nakao
The Startup
Published in
5 min readJun 18, 2020
How to Create a Rails 6 Image in Docker

Introduction

In this post I will show you what I did to create a Rails 6 Image based on Ubuntu container. Just for information, I am doing this tutorial in Ubuntu 18.04. If you are using other operacional system, it’s important to know the differences on docker usage.

This is a simple tutorial, but I will start from this part, because I intend to show in next post how can you integrate a Rails 6 container with Postgres container WITHOUT use docker compose (there is a lot of tutorials about this USING DOCKER COMPOSE! And it was hard to me find a way to learn how can I create containers without this tool)

I don’t know you, but I’m a person that like to know exatly what I’m doing. Using docker compose I had the sensation that I was only copying and pasting a lot of informations without know exatly what was hapenning.

Before anything, I am assuming that you have docker installed on your machine.

Starting Ubuntu Container

Enough of idle, let’s start. In the first place, let’s run a container based on a Ubuntu image. To make this, you need to run the follow command:

docker run -it ubuntu 
Explanationdocker run is a command used when you want to start a new container based on an image
-it are flags used when you need a container that need to be interactive, in other words, it enables you to use the terminal inside the container
ubuntu is the name of the image

Installing Prerequisites

After this, we are already connected with container and the next step is install prerequisites and rails 6.

Prerequisites:
- Git
- Curl
- Yarn
- Nodejs
- Npm
- Ruby

To start installations, we need to update the system.

apt update

Git

apt install git

To verify if git was correctly installed:

git --version

The output is something like this:

git version 2.25.1

Curl

apt install curl

Running the follow command, it’s possible to know if curl is installed:

curl

The output is something like this:

curl: try 'curl --help' or 'curl --manual' for more information

Nodejs

apt install nodejs

You have to set some informations to correct node installation.

Use this command to verify node version:

node -v

You will see something like this:

v10.19.0

Npm

apt install npm

To verify:

npm -v

The return is a version number:

6.14.4

Yarn

Yarn is installed with curl command, by this, it’s important that you have already installed curl on your container:

curl -o- -L https://yarnpkg.com/install.sh | bash

Trying to see yarn version:

To use this command, you will need to reopen terminal.

yarn --version

If it return a version number, everything is correct

1.22.4

Ruby

Take care, there are so many commands here, and they need to be used one by one.

git clone https://github.com/rbenv/rbenv.git ~/.rbenvecho 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrcexec $SHELLgit clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-buildapt-get install -y libreadline-dev zlib1g-dev libpq-devrbenv install 2.7.1rbenv global 2.7.1

To verify:

ruby -v

And the terminal will show you ruby version.

You need to install bundler:

gem install bundler

And run the command:

rbenv rehash

Installing Rails 6

To install Rails 6, we will use gem.

gem install rails -v 6.0.0rbenv rehash

To know if Rails 6 is already installed:

rails -v

If the version appear like the follow, Ruby on Rails is already installed on your container.

Rails 6.0.0

Now, just one more step…

Create an Image

To create an image, you will only need to run one command.

docker commit container_id image_name:tagExample:
docker commit 83f5accb104a rails6:1.0

To know your container_id, you can run the following command on terminal:

docker ps -a

And you will search some id as in the picture.

Explanationdocker commit - command used to commit a change or create an image
container_id - identifier that references a container
image_name - name that you want to put on your image
tag - your image version

--

--