Rails on docker

2020-11-15

I am still struggling how to create rails env on docker..

alpine or ubuntu?

People say "alpine is small!". Yes, actually it is small but I realized that ubuntu images are not so big compare to alpines.

ubuntu                    focal               d70eaf7277ea        3 weeks ago         72.9MB
ubuntu                    bionic              56def654ec22        7 weeks ago         63.2MB
ruby                      2.5-alpine          835f96a1199b        3 weeks ago         53.1MB
ruby                      2.7-alpine          79f5adf3c887        3 weeks ago         51.7MB

Just for the note. The following is the dockerfile for alpine. By adding build-base package, it's always needed to add lots of libraries to install rubygems.

# did not work on my env. Just for memo.
FROM ruby2.5-alpine

RUN apk update && apk add --no-cache build-base imagemagick-dev \
  mysql-client mysql-dev \
  && gem install bundler -v "~> 2.1" -N \
  && gem install rails -v "~> 5.2" -N

WORKDIR /home
COPY ./railsapp /home
RUN bundle install

Alpine doesn't have bash in the default installation. Use ash instead.

docker-compose run web ash

how to use ubuntu images.

I've created an example Dockerfile for the latest ubuntu image.

FROM ubuntu:focal

ENV DEBIAN_FRONTEND="noninteractive"
RUN apt-get -y update \
  && apt-get -y install build-essential nodejs patch ruby-dev libmysqlclient-dev libmagick++-dev libcurl4-openssl-dev liblzma-dev libssl-dev libxml2-dev zlib1g-dev \
  && gem install rails -v "~> 5.2" -N \
  && gem install bundler -v "~> 2.1" -N \
  && gem install mysql2 rmagick -N

WORKDIR /home
# COPY ./railsapp /home
# RUN bundle install

The commands for build and run.

docker build --tag myubuntu:latest .
docker run -it myubuntu bash

TODO: looking for how to set up postgresql driver and yarn for newer rails(6.x).

apt install yarnpng