Ruby in Docker

Contents
The ruby image
Run one-liners
Mount your script
Install gems inside

The ruby image

Docker lets you run Ruby without installing it on the host. The official ruby image on the hub carries ready builds for each release line.

The ruby:3 tag follows the Ruby 3 line, so it is a good default for this course. You do not need to remember digests or variants yet.

Run one-liners

The fastest check is a one-liner that prints the Ruby version from inside a fresh container. The container is removed automatically after it runs:

Print Ruby version from Docker

docker run --rm ruby:3 ruby -v

ruby 3.3.0 (2023-12-25 revision 7b0c32346b) [x86_64-linux]

The first run may download the image, so it can take a while. Later runs reuse the cached image and start instantly.

Mount your script

To run your own file, mount the current folder into the container and set it as the working folder. Then ask the container Ruby to run your script by name:

Run main.rb from the current folder

docker run --rm -v "$PWD":/app -w /app ruby:3 ruby main.rb

hello from Docker

The mount flag shares files both ways, so edits on the host are visible inside the container at once. Keep script names simple while you learn the flags.

Install gems inside

Gems installed inside a plain run command live only as long as that container lives. That behavior is handy for quick trials and keeps the host clean.

For a repeatable setup, write a small Dockerfile that starts from the ruby image and installs the gems you need. Rebuild the image when the gem list changes and run scripts from the new image.

Next: Ruby course

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: