Skip to content

Instantly share code, notes, and snippets.

@tomohiro
Last active January 10, 2019 08:19
Show Gist options
  • Save tomohiro/d727b161fd3b9f6fe96dd9da23a01833 to your computer and use it in GitHub Desktop.
Save tomohiro/d727b161fd3b9f6fe96dd9da23a01833 to your computer and use it in GitHub Desktop.
(Experimental) Ruby Container size reducing
FROM ruby:2.6.0-slim-stretch
RUN set -ex \
&& apt-get update -qq && apt-get install -y --no-install-recommends \
build-essential=12.3 \
libxml2-dev=2.9.4+dfsg1-2.2+deb9u2 \
libxslt1-dev=1.1.29-2.1 \
libsqlite3-dev=3.16.2-5+deb9u1 \
&& apt-get clean \
&& apt-get autoclean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY Gemfile .
COPY Gemfile.lock .
RUN gem update bundler
RUN bundle install --frozen --jobs=4 --clean
COPY . .
CMD ["ruby", "./url.rb"]
# Build for install dependency RubyGems
FROM ruby:2.6.0-stretch AS bundle
COPY Gemfile .
COPY Gemfile.lock .
RUN gem update bundler
RUN bundle install --frozen --jobs=4 --clean
# Build for run the Ruby
FROM ruby:2.6.0-slim-stretch
COPY --from=bundle /usr/local/bundle /usr/local/bundle
RUN set -ex \
&& apt-get update -qq && apt-get install -y --no-install-recommends \
libxml2=2.9.4+dfsg1-2.2+deb9u2 \
libxslt1.1=1.1.29-2.1 \
libsqlite3-0=3.16.2-5+deb9u1 \
&& apt-get clean \
&& apt-get autoclean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY . .
CMD ["ruby", "./url.rb"]
source "https://rubygems.org"
gem "sqlite3"
gem "nokogiri"
GEM
remote: https://rubygems.org/
specs:
mini_portile2 (2.4.0)
nokogiri (1.10.0)
mini_portile2 (~> 2.4.0)
sqlite3 (1.3.13)
PLATFORMS
ruby
DEPENDENCIES
nokogiri
sqlite3
BUNDLED WITH
2.0.1
require 'sqlite3'
require 'nokogiri'
require 'open-uri'
URLS = [
'https://www.google.co.jp',
'https://www.apple.com'
]
# Initialize Database
db = SQLite3::Database.new 'test.db'
db.results_as_hash = true
db.execute('CREATE TABLE urls (id INTEGER PRIMARY KEY, url TEXT, title TEXT);')
# Create site information from URL list
URLS.each do |url|
html = Nokogiri::HTML(open(url))
title = html.search('title').text
db.execute('INSERT INTO URLS (url, title) values (?, ?)', url, title)
end
# Show URL information
db.execute('SELECT * FROM urls') do |row|
puts "ID: #{row['id']} | #{row['url']} | #{row['title']}"
end
@tomohiro
Copy link
Author

tomohiro commented Jan 10, 2019

Compare

$ docker images | grep tomohiro/ruby
tomohiro/ruby               2.6.0-slim-stretch-large   6ffd2344aea4        4 minutes ago       557MB
tomohiro/ruby               2.6.0-slim-stretch-small   cb7d7a51ce43        14 minutes ago      243MB

Build

$ docker build -f ./Dockerfile.small . -t tomohiro/ruby:2.6.0-slim-stretch-small
$ docker build -f ./Dockerfile.large . -t tomohiro/ruby:2.6.0-slim-stretch-large

Result

Large:

$ docker run -it --rm tomohiro/ruby:2.6.0-slim-stretch-large
ID: 1 | https://www.google.co.jp | Google
ID: 2 | https://www.apple.com | Apple

Small:

$ docker run -it --rm tomohiro/ruby:2.6.0-slim-stretch-small
ID: 1 | https://www.google.co.jp | Google
ID: 2 | https://www.apple.com | Apple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment