The Definitive Solution to Your Rails + Docker + Time Zone Headaches!
April 30, 2025
Tired of debugging timestamps that don’t make any sense? You’re not alone — but the fix is easier than you think.
Want to Improve Your Application’s Performance?
If you’re looking to optimize your Ruby applications and boost performance, get in touch with us! Whether it’s refining your code, enhancing efficiency, or troubleshooting time zone nightmares, we’re here to help.
Local vs Docker vs Server: Why Your Timestamps Are All Over the Place
Let’s start simple: Your local system runs on your configured OS time zone, but if Docker or your production server isn’t aligned, your Ruby on Rails app might be storing or displaying times that make no sense to your users.
A typical mistake? Using:
DateTime.now
Instead of:
Time.zone.now # or
Time.current
Even worse, check what’s being stored in your DB:
created_at: "2025-04-30 13:44:44.539479000 +0000"
Looks normal? Maybe not. That’s UTC — and without proper setup, your app may show a different time than expected depending on context.
First Fix: Point Rails to the Right Zone
Update your config/application.rb:
config.time_zone = 'America/Argentina/Buenos_Aires'
config.active_record.default_timezone = :local
Now Rails will know your intentions — and Time.zone.now, Time.current, and database timestamps will play nice together.
Second Fix: Docker Time Alignment
Docker containers often ignore your host machine’s time zone unless explicitly told.
Second Fix: Docker Time Alignment
Add to your docker-compose.yml:
environment:
- TZ=America/Argentina/Buenos_Aires
volumes:
- /etc/localtime:/etc/localtime:ro
This makes sure the container runs in sync with your host’s time zone — no more “Where did 3 hours go?” moments.
Third Fix: Servers (Heroku, AWS, DigitalOcean)
Heroku
You can’t directly set the OS time zone, but Rails will respect your config.time_zone. Always use Time.zone.now — never DateTime.now.
AWS / DigitalOcean
SSH into your server and run:
sudo timedatectl set-timezone America/Argentina/Buenos_Aires
Restart your app, and now even your logs will respect the correct time zone.
Summary
-
Don’t use DateTime.now — it bypasses your time zone config.
-
Use Time.zone.now or Time.current — they respect your app’s setting.
-
Set config.time_zone and config.active_record.default_timezone = :local in Rails.
-
Align Docker containers with TZ and localtime volume.
-
Adjust server time zones where possible.
Final Thought
Time zones are one of those things that seem trivial… until they quietly ruin your app’s UX, analytics, or billing system. Fixing them takes 10 minutes and can save you hours of confusion.
And remember:
“The only people who like time zones are the people who don’t have to deal with them.”