Ruby with Bundler
Best practices for Dockerfile for Ruby with Bundler
🐳 Annotated Dockerfile for Ruby with Bundler:
🔍 Why these are best practices:
✅ Multi-stage builds
- Separates build environment (with compilers and dev dependencies) from runtime environment.
- Significantly reduces final image size by not including build tools in production.
- Improves security by having fewer binaries in the production image.
✅ Bundler optimization
- BUNDLE_WITHOUT excludes development and test gems from production.
- BUNDLE_DEPLOYMENT enables deployment mode for consistent environments.
- BUNDLE_JOBS accelerates gem installation by using multiple cores.
- BUNDLE_RETRY adds resilience to network issues during installation.
✅ Cleanup operations
- Removes build artifacts and temporary files to reduce image size.
- Deletes gem caches, source files, and object files that aren't needed at runtime.
- Uses
apt-get clean
andrm -rf /var/lib/apt/lists/*
to remove package metadata.
✅ Asset precompilation
- Precompiles assets during build phase for Rails applications.
- Ensures assets are optimized and ready to serve in production.
- Uses a dummy SECRET_KEY_BASE for precompilation to avoid environment variable requirements.
✅ Non-root user
- Runs the application as a non-privileged user for enhanced security.
- Follows container security best practices to minimize potential damage from vulnerabilities.
🚀 Additional Dockerfile best practices you can adopt:
Configure Redis/Sidekiq
For applications using background processing with Sidekiq:
Add health checks
Monitor application health and enable automatic container recovery:
Use .dockerignore
Exclude unnecessary files from your Docker build context:
Configure database migrations
For Rails applications, consider adding an entrypoint script to handle migrations:
With entrypoint.sh
:
Optimize for boot time
For faster application startup:
Consider using jemalloc for better memory management
For high-traffic Ruby applications:
By following these practices, you'll create Docker images for your Ruby applications that are efficient, secure, and optimized for production environments. These approaches will help ensure consistent deployment, faster performance, and better resource utilization for your Ruby applications.
Last updated on